一、java版
抽象产品 Product.java :
/** * 抽象产品 * @author hejinlai * */public interface Product { public void fun();}
第一个具体产品 ConcreteProduct.java :
/** * 具体产品 * @author hejinlai * */public class ConcreteProduct implements Product { @Override public void fun() { System.out.println("ConcreteProduct -> fun()"); }}
第二个具体产品 ConcreteProduct2.java :
public class ConcreteProduct2 implements Product { @Override public void fun() { System.out.println("ConcreteProduct2 -> fun()"); }}
抽象工厂 Factory.java :
/** * 抽象工厂 * @author hejinlai * */public interface Factory { public Product getProduct();}
第一个具体工厂 ConcreteFactory.java :
public class ConcreteFactory implements Factory { @Override public Product getProduct() { return new ConcreteProduct(); }}
第二个具体工厂 ConcreteFactory2.java :
public class ConcreteFactory2 implements Factory { @Override public Product getProduct() { return new ConcreteProduct2(); }}
测试 Test.java :
/** * 测试类 * @author hejinlai * */public class Test { public static void main(String[] args) { /*Product product = SimpleFactory.getProduct(); product.fun();*/ Factory factory; Product product; factory = new ConcreteFactory(); product = factory.getProduct(); product.fun(); factory = new ConcreteFactory2(); product = factory.getProduct(); product.fun(); }}
测试结果:
ConcreteProduct -> fun()
ConcreteProduct2 -> fun()
二、c++版
抽象产品 Product.h & Procudt.cpp :
//// Product.h// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#ifndef __Pattern__Product__#define __Pattern__Product__#includeclass Product { public: Product(); virtual ~Product(); virtual void fun() = 0;};#endif /* defined(__Pattern__Product__) */
//// 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.cpp :
//// ConcreteProduct.h// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#ifndef __Pattern__ConcreteProduct__#define __Pattern__ConcreteProduct__#include#include "Product.h"using namespace std;class ConcreteProduct : public Product{ public: virtual void fun(); ConcreteProduct(); ~ConcreteProduct(); };#endif /* defined(__Pattern__ConcreteProduct__) */
//// ConcreteProduct.cpp// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#include "ConcreteProduct.h"void ConcreteProduct::fun(){ cout << "ConcreteProduct -> fun()\n";}ConcreteProduct::ConcreteProduct(){ }ConcreteProduct::~ConcreteProduct(){ }
第二个具体产品 ConcreteProduct2.h & ConcreteProduct2.cpp :
//// ConcreteProduct2.h// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#ifndef __Pattern__ConcreteProduct2__#define __Pattern__ConcreteProduct2__#include#include "Product.h"using namespace std;class ConcreteProduct2 : public Product{ public: virtual void fun(); ConcreteProduct2(); ~ConcreteProduct2(); };#endif /* defined(__Pattern__ConcreteProduct2__) */
//// ConcreteProduct2.cpp// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#include "ConcreteProduct2.h"void ConcreteProduct2::fun(){ cout << "ConcreteProduct2 -> fun()\n";}ConcreteProduct2::ConcreteProduct2(){ }ConcreteProduct2::~ConcreteProduct2(){ }
抽象工厂 Factory.h & Factory.cpp :
//// Factory.h// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#ifndef __Pattern__Factory__#define __Pattern__Factory__#include#include "Product.h"class Factory { public: Factory(); virtual ~Factory(); virtual Product * getProduct() = 0; };#endif /* defined(__Pattern__Factory__) */
//// Factory.cpp// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#include "Factory.h"Factory::Factory(){ }Factory::~Factory(){ }
第一个具体工厂 ConcreteFactory.h & ConcreteFactory.cpp :
//// ConcreteFactory.h// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#ifndef __Pattern__ConcreteFactory__#define __Pattern__ConcreteFactory__#include#include "Factory.h"class ConcreteFactory : public Factory { public: ConcreteFactory(); ~ConcreteFactory(); virtual Product * getProduct(); };#endif /* defined(__Pattern__ConcreteFactory__) */
//// ConcreteFactory.cpp// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#include "ConcreteFactory.h"#include "ConcreteProduct.h"ConcreteFactory::ConcreteFactory(){ }ConcreteFactory::~ConcreteFactory(){ }Product * ConcreteFactory::getProduct(){ return new ConcreteProduct();}
第二个具体工厂 ConcreteFactory2.h & ConcreteFactory2.cpp :
//// ConcreteFactory2.h// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#ifndef __Pattern__ConcreteFactory2__#define __Pattern__ConcreteFactory2__#include#include "Factory.h"class ConcreteFactory2 : public Factory { public: ConcreteFactory2(); ~ConcreteFactory2(); virtual Product * getProduct(); };#endif /* defined(__Pattern__ConcreteFactory2__) */
//// ConcreteFactory2.cpp// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#include "ConcreteFactory2.h"#include "ConcreteProduct2.h"ConcreteFactory2::ConcreteFactory2(){ }ConcreteFactory2::~ConcreteFactory2(){ }Product * ConcreteFactory2::getProduct(){ return new ConcreteProduct2();}
测试类 main.cpp :
//// main.cpp// Pattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#include#include "SimpleFactory.h"#include "Factory.h"#include "ConcreteFactory.h"#include "ConcreteFactory2.h"int main(int argc, const char * argv[]){ /*Product * product = SimpleFactory::getProduct(); product->fun(); delete product;*/ Factory *factory; Product *product; factory = new ConcreteFactory(); product = factory->getProduct(); product->fun(); delete factory; delete product; factory = new ConcreteFactory2(); product = factory->getProduct(); product->fun(); delete factory; delete product; return 0;}
测试结果:
ConcreteProduct -> fun()
ConcreteProduct2 -> fun()
三、objective-c 版
抽象产品 Procuct.h :
//// Product.h// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import@protocol Product - (void) fun;@end
第一个具体产品 ConcreteProduct.h & ConcreteProduct.m :
//// ConcreteProduct.h// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import#import "Product.h"@interface ConcreteProduct : NSObject @end
//// 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
第二个具体产品 ConcreteProduct2.h & ConcreteProduct2.m :
//// ConcreteProduct2.h// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import#import "Product.h"@interface ConcreteProduct2 : NSObject @end
//// ConcreteProduct2.m// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import "ConcreteProduct2.h"@implementation ConcreteProduct2- (void)fun{ NSLog(@"ConcreteProduct2 -> fun()");}@end
抽象工厂 Factory.h :
//// Factory.h// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import#import "Product.h"@protocol Factory -(id ) getProduct;@end
第一个具体工厂 ConcreteFactory.h & ConcreteFactory.m :
//// ConcreteFactory.h// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import#import "Factory.h"@interface ConcreteFactory : NSObject @end
//// ConcreteFactory.m// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import "ConcreteFactory.h"#import "ConcreteProduct.h"@implementation ConcreteFactory-(id) getProduct{ return [[[ConcreteProduct alloc] init] autorelease];}@end
第二个具体工厂 ConcreteFactory2.h & ConcreteFactory2.m :
//// ConcreteFactory2.h// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import#import "Factory.h"@interface ConcreteFactory2 : NSObject @end
//// ConcreteFactory2.m// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import "ConcreteFactory2.h"#import "ConcreteProduct2.h"@implementation ConcreteFactory2-(id) getProduct{ return [[[ConcreteProduct2 alloc] init] autorelease];}@end
测试 main.m :
//// main.m// ObcPattern//// Created by hejinlai on 13-8-6.// Copyright (c) 2013年 yunzhisheng. All rights reserved.//#import#import "SimpleFactory.h"#import "ConcreteFactory.h"#import "ConcreteFactory2.h"int main(int argc, const char * argv[]){ @autoreleasepool { /*id product = [SimpleFactory getProduct]; [product fun];*/ id factory; id product; factory = [[ConcreteFactory alloc] init]; product = [factory getProduct]; [product fun]; [factory release]; factory = [[ConcreteFactory2 alloc] init]; product = [factory getProduct]; [product fun]; [factory release]; } return 0;}
测试结果:
2013-08-06 18:46:38.409 ObcPattern[3211:303] ConcreteProduct -> fun()
2013-08-06 18:46:38.410 ObcPattern[3211:303] ConcreteProduct2 -> fun()