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()"); } }
第二個具體產品 ConcreteProduct2.java :objective-c
public class ConcreteProduct2 implements Product { @Override public void fun() { System.out.println("ConcreteProduct2 -> fun()"); } }
抽象工廠 Factory.java :ide
/** * 抽象工廠 * @author hejinlai * */ public interface Factory { public Product getProduct(); }
第一個具體工廠 ConcreteFactory.java :測試
public class ConcreteFactory implements Factory { @Override public Product getProduct() { return new ConcreteProduct(); } }
第二個具體工廠 ConcreteFactory2.java :spa
public class ConcreteFactory2 implements Factory { @Override public Product getProduct() { return new ConcreteProduct2(); } }
測試 Test.java :
get
/** * 測試類 * @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()it
ConcreteProduct2 -> fun()
2、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__ #include <iostream> class 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 <iostream> #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 <iostream> #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 <iostream> #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 <iostream> #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 <iostream> #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 <iostream> #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()
3、objective-c 版
抽象產品 Procuct.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.m :
// // 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 // 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 <Foundation/Foundation.h> #import "Product.h" @interface ConcreteProduct2 : NSObject <Product> @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 <Foundation/Foundation.h> #import "Product.h" @protocol Factory <NSObject> -(id<Product>) getProduct; @end
第一個具體工廠 ConcreteFactory.h & ConcreteFactory.m :
// // ConcreteFactory.h // ObcPattern // // Created by hejinlai on 13-8-6. // Copyright (c) 2013年 yunzhisheng. All rights reserved. // #import <Foundation/Foundation.h> #import "Factory.h" @interface ConcreteFactory : NSObject <Factory> @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<Product>) 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 <Foundation/Foundation.h> #import "Factory.h" @interface ConcreteFactory2 : NSObject <Factory> @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<Product>) 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 <Foundation/Foundation.h> #import "SimpleFactory.h" #import "ConcreteFactory.h" #import "ConcreteFactory2.h" int main(int argc, const char * argv[]) { @autoreleasepool { /*id<Product> product = [SimpleFactory getProduct]; [product fun];*/ id<Factory> factory; id<Product> 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()