簡單工廠模式(java,c++,objective-c)

1、java版
java


抽象產品 Product.java :ios


/**
 * 抽象產品
 * @author hejinlai
 *
 */
public interface Product {
    public void fun();
}


具體產品 ConcreteProduct.java :c++


/**
 * 具體產品
 * @author hejinlai
 *
 */
public class ConcreteProduct implements Product {
    @Override
    public void fun() {
        System.out.println("ConcreteProduct -> fun()");
    }
}



簡單工廠 SimpleFactory.java :ide


/**
 * 簡單工廠
 * @author hejinlai
 *
 */
public class SimpleFactory {
    public static Product getProduct(){
        return new ConcreteProduct();
    }
}



測試 Test.java :測試


/**
 * 測試類
 * @author hejinlai
 *
 */
public class Test {
    public static void main(String[] args) {
                                                                                                                                                                                                                                                                       
        Product product = SimpleFactory.getProduct();
        product.fun();
                                                                                                                                                                                                                                                                       
    }
}



測試結果:spa


      ConcreteProduct -> fun()get



2、c++版產品


抽象產品 Product.h :it


//
//  Product.h
//  Pattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __Pattern__Product__
#define __Pattern__Product__
#include <iostream>
class Product {
                                                                                                                                                                                    
public:
                                                                                                                                                                                    
    Product();
    virtual ~Product();
                                                                                                                                                                                    
    virtual void fun() = 0;
};
#endif /* defined(__Pattern__Product__) */



抽象產品 Product.cpp :io


//
//  Product.cpp
//  Pattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "Product.h"
Product::Product()
{
                                                                                                                                                                          
}
Product::~Product()
{
                                                                                                                                                                          
}



具體產品 ConcreteProduct.h :


//
//  ConcreteProduct.h
//  Pattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __Pattern__ConcreteProduct__
#define __Pattern__ConcreteProduct__
#include <iostream>
#include "Product.h"
using namespace std;
class ConcreteProduct : public Product{
                                                                                                                                                         
                                                                                                                                                         
public:
                                                                                                                                                         
    virtual void fun();
                                                                                                                                                         
    ConcreteProduct();
    ~ConcreteProduct();
                                                                                                                                                         
};
#endif /* defined(__Pattern__ConcreteProduct__) */


具體產品 ConcreteProduct.cpp:


//
//  SimpleFactory.cpp
//  Pattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "SimpleFactory.h"
#include "ConcreteProduct.h"
Product * SimpleFactory::getProduct()
{
    return new ConcreteProduct();
}



簡單工廠 SimpleFactory.h :


//
//  SimpleFactory.h
//  Pattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#ifndef __Pattern__SimpleFactory__
#define __Pattern__SimpleFactory__
#include <iostream>
#include "Product.h"
class SimpleFactory {
                                                                                                                         
public:
    static Product * getProduct();
};
#endif /* defined(__Pattern__SimpleFactory__) */


簡單工廠 SimpleFactory.cpp :


//
//  SimpleFactory.cpp
//  Pattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include "SimpleFactory.h"
#include "ConcreteProduct.h"
Product * SimpleFactory::getProduct()
{
    return new ConcreteProduct();
}


測試 main.cpp :


//
//  main.cpp
//  Pattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#include <iostream>
#include "SimpleFactory.h"
int main(int argc, const char * argv[])
{
    Product * product = SimpleFactory::getProduct();
    product->fun();
    delete product;
                                                                                                  
    return 0;
}



測試結果:


ConcreteProduct -> fun()



3、Objective-c 版


抽象產品 Product.h :


//
//  Product.h
//  ObcPattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol Product <NSObject>
- (void) fun;
@end



具體產品 ConcreteProduct.h :


//
//  ConcreteProduct.h
//  ObcPattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import "Product.h"
@interface ConcreteProduct : NSObject <Product>
@end


具體產品 ConcreteProduct.m :


//
//  ConcreteProduct.m
//  ObcPattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "ConcreteProduct.h"
@implementation ConcreteProduct
- (void)fun
{
    NSLog(@"ConcreteProduct -> fun()");
}
@end



簡單工廠 SimepleFactory.h :


//
//  SimpleFactory.h
//  ObcPattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Product.h"
@interface SimpleFactory : NSObject
+(id<Product>) getProduct;
@end


簡單工廠 SimepleFactory.m :


//
//  SimpleFactory.m
//  ObcPattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import "SimpleFactory.h"
#import "ConcreteProduct.h"
@implementation SimpleFactory
+(id<Product>) getProduct
{
    return [[[ConcreteProduct alloc] init] autorelease];
}
@end


測試 main.m :


//
//  main.m
//  ObcPattern
//
//  Created by hejinlai on 13-8-6.
//  Copyright (c) 2013年 yunzhisheng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SimpleFactory.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool {
               
        id<Product> product = [SimpleFactory getProduct];
        [product fun];
    }
           
    return 0;
}


測試結果:


2013-08-06 14:22:32.186 ObcPattern[9932:303] ConcreteProduct -> fun()

相關文章
相關標籤/搜索