一、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__
#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()



三、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()