简单工厂模式(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()

相关文章
相关标签/搜索