设计模式(8)-工厂模式(Factory)

【描述】工厂模式有3种类型:简单工厂,工厂方法,抽象工厂。这3种设计模式都属于创建型的设计模式。简单工厂的工厂是具象的,没有抽象的工厂类。工厂方法,定义了一个创建对象的接口,让子类来决定具体实例化哪一个类。抽象工厂模式面对的问题是多产品等级结构的系统设计。相对于简单工厂,工厂方法和抽象工厂都定义了抽象的工厂类。工厂方法与抽象工厂的区别在于:工厂方法的“工厂”只制造单一的“产品”,而抽象工厂可以制造一族“产品”。

1 简单工厂(Simple Factory)

【UML图】

图1 简单工厂

1 定义了一个具象的工厂Factory;

2 定义了一个抽象的产品Product以及两个具象的产品ProductA,ProductB,实现了产品的方法operation;

3 Factory将根据需要选择制造具象的产品。(注意是具象的产品,策略模式以及建造模式Client调用的是抽象的接口,这是工厂模式与策略模式、建造模式关键区别,下同)

 

【代码清单】

common.h

#ifndef COMMON_H
#define COMMON_H

enum {Product_A = 0, Product_B = 1};

#endif // COMMON_H

product.h

#ifndef PRODUCT_H
#define PRODUCT_H

class Product
{
public:
    Product();

public:
    virtual void operation();
};

#endif // PRODUCT_H


producta.h

#ifndef PRODUCTA_H
#define PRODUCTA_H

#include "product.h"

class ProductA : public Product
{
public:
    ProductA();

public:
    void operation();
};

#endif // PRODUCTA_H


productb.h

#ifndef PRODUCTB_H
#define PRODUCTB_H

#include "product.h"

class ProductB : public Product
{
public:
    ProductB();

public:
    void operation();
};

#endif // PRODUCTB_H


simplefactory.h

#ifndef SIMPLEFACTORY_H
#define SIMPLEFACTORY_H

#include "product.h"

class SimpleFactory
{
public:
    SimpleFactory();

public:
    Product* createProduct(int type);
};

#endif // SIMPLEFACTORY_H


product.cpp

#include <QDebug>
#include "product.h"

Product::Product()
{
    qDebug()<<"construct Product";
}

void Product::operation()
{
    qDebug()<<"Product operation";
}


producta.cpp

#include <QDebug>
#include "producta.h"

ProductA::ProductA()
{
    qDebug()<<"construct ProductA";
}

void ProductA::operation()
{
    qDebug()<<"ProductA::operation";
}


productb.cpp

#include <QDebug>
#include "productb.h"

ProductB::ProductB()
{
    qDebug()<<"construct ProductB";
}

void ProductB::operation()
{
    qDebug()<<"ProductA::operation";
}


simplefactory.cpp

#include <QDebug>
#include "common.h"
#include "simplefactory.h"
#include "producta.h"
#include "productb.h"

SimpleFactory::SimpleFactory()
{
}

Product* SimpleFactory::createProduct(int type)
{
    Product *p = NULL;

    switch(type)
    {
    case Product_A:
        p = new ProductA;
        break;
    case Product_B:
        p = new ProductB;
        break;
    default:;
    }

    return p;
}

 

main.cpp

#include "simplefactory.h"
#include "product.h"
#include "common.h"

int main(int argc, char **argv)
{
    SimpleFactory *factory = new SimpleFactory;
    Product * product = factory->createProduct(Product_A);
    product->operation();

    return 0;
}


【运行结果】

construct Product 
construct ProductA 
ProductA::operation 


2 工厂方法(Factory Method)

【UML图】

图2 工厂方法

1 定义了一个抽象的工厂Factory以及两个具象的工厂,实现了产品制造的方法createProduct();

2 定义了一个抽象的产品Product以及两个具象的产品ProductA,ProductB,实现了产品的方法operation;

3 Factory将根据需要选择制造具象的产品。

【代码清单】

factory.h

#ifndef FACTORY_H
#define FACTORY_H

#include "product.h"

class Factory
{
public:
    Factory();

public:
    virtual Product *createProduct();
};

#endif // FACTORY_H


factorya.h

#ifndef FACTORYA_H
#define FACTORYA_H

#include "factory.h"
#include "producta.h"

class FactoryA : public Factory
{
public:
    FactoryA();

public:
    ProductA *createProduct();
};

#endif // FACTORYA_H


factoryb.h

#ifndef FACTORYB_H
#define FACTORYB_H

#include "factory.h"
#include "productb.h"

class FactoryB : public Factory
{
public:
    FactoryB();

public:
    ProductB *createProduct();
};

#endif // FACTORYB_H


product.h

#ifndef PRODUCT_H
#define PRODUCT_H

class Product
{
public:
    Product();

public:
    virtual void operation();
};

#endif // PRODUCT_H


producta.h

#ifndef PRODUCTA_H
#define PRODUCTA_H

#include "product.h"

class ProductA : public Product
{
public:
    ProductA();

public:
    void operation();
};

#endif // PRODUCTA_H


productb.h

#ifndef PRODUCTB_H
#define PRODUCTB_H

#include "product.h"

class ProductB : public Product
{
public:
    ProductB();

public:
    void operation();
};

#endif // PRODUCTB_H


factory.cpp

#include <QDebug>
#include "factory.h"

Factory::Factory()
{
    qDebug()<<"construct Factory";
}

Product *Factory::createProduct()
{
    return new Product;
}


factorya.cpp

#include <QDebug>
#include "factorya.h"

FactoryA::FactoryA()
{
     qDebug()<<"construct FactoryA";
}

ProductA *FactoryA::createProduct()
{
    return new ProductA;
}


factoryb.cpp

#include <QDebug>
#include "factoryb.h"


FactoryB::FactoryB()
{
     qDebug()<<"construct FactoryB";
}

ProductB* FactoryB::createProduct()
{
    return new ProductB;
}


product.cpp

#include <QDebug>
#include "product.h"

Product::Product()
{
    qDebug()<<"construct Product";
}

void Product::operation()
{
    qDebug()<<"Product Operation";
}


producta.cpp

#include <QDebug>
#include "producta.h"

ProductA::ProductA()
{
    qDebug()<<"construct ProductA";
}

void ProductA::operation()
{
    qDebug()<<"ProductA operation";
}


productb.cpp

#include <QDebug>
#include "productb.h"

ProductB::ProductB()
{
    qDebug()<<"construct ProductB";
}

void ProductB::operation()
{
    qDebug()<<"ProductB operation";
}


main.cpp

#include "factory.h"
#include "factorya.h"
#include "factoryb.h"
#include "product.h"

int main(int argc, char **argv)
{
    Factory *factory = new FactoryA;
    Product *product = factory->createProduct();
    product->operation();

    return 0;
}


【运行结果】

construct Factory 
construct FactoryA 
construct Product 
construct ProductA 
ProductA operation 


3 抽象工厂(Abstract Factory)

【UML图】

图3 抽象工厂

1 定义了一个抽象的工厂Factory以及两个具象的工厂,实现了产品制造的方法createProductA(),createProductB;

2 定义了一个抽象的产品Product以及两个具象的产品ProductA,ProductB,实现了产品的方法operationA,operationB;

3 Factory将根据需要选择制造一族具象的产品。

 

【代码清单】

factory.h

#ifndef FACTORY_H
#define FACTORY_H

#include "product.h"

class Factory
{
public:
    Factory();

public:
    virtual Product *createProductA();
    virtual Product *createProductB();
};

#endif // FACTORY_H


factorya.h

#ifndef FACTORYA_H
#define FACTORYA_H

#include "factory.h"
#include "producta.h"
#include "productb.h"

class FactoryA : public Factory
{
public:
    FactoryA();

public:
    ProductA *createProductA();
    ProductB *createProductB();
};

#endif // FACTORYA_H


factoryb.h

#ifndef FACTORYB_H
#define FACTORYB_H

#include "factory.h"
#include "producta.h"
#include "productb.h"

class FactoryB : public Factory
{
public:
    FactoryB();

public:
    ProductA *createProductA();
    ProductB *createProductB();
};

#endif // FACTORYB_H


product.h

#ifndef PRODUCT_H
#define PRODUCT_H

class Product
{
public:
    Product();

public:
    virtual void operationA();
    virtual void operationB();
};

#endif // PRODUCT_H


producta.h

#ifndef PRODUCTA_H
#define PRODUCTA_H

#include "product.h"

class ProductA : public Product
{
public:
    ProductA();

public:
    void operationA();
};

#endif // PRODUCTA_H


productb.h

#ifndef PRODUCTB_H
#define PRODUCTB_H

#include "product.h"

class ProductB : public Product
{
public:
    ProductB();

public:
    void operationB();
};

#endif // PRODUCTB_H

 

factory.cpp

#include <QDebug>
#include "factory.h"

Factory::Factory()
{
    qDebug()<<"construct Factory";
}

Product *Factory::createProductA()
{
    return new Product;
}

Product *Factory::createProductB()
{
    return new Product;
}


factorya.cpp

#include <QDebug>
#include "factorya.h"

FactoryA::FactoryA()
{
     qDebug()<<"construct FactoryA";
}

ProductA *FactoryA::createProductA()
{
    return new ProductA;
}

ProductB *FactoryA::createProductB()
{
    return new ProductB;
}


factoryb.cpp

#include <QDebug>
#include "factoryb.h"


FactoryB::FactoryB()
{
     qDebug()<<"construct FactoryB";
}

ProductA *FactoryB::createProductA()
{
    return new ProductA;
}

ProductB *FactoryB::createProductB()
{
    return new ProductB;
}


product.cpp

#include <QDebug>
#include "product.h"

Product::Product()
{
    qDebug()<<"construct Product";
}

void Product::operationA()
{
    qDebug()<<"Product OperationA";
}

void Product::operationB()
{
    qDebug()<<"Product OperationB";
}


producta.cpp

#include <QDebug>
#include "producta.h"

ProductA::ProductA()
{
    qDebug()<<"construct ProductA";
}

void ProductA::operationA()
{
    qDebug()<<"ProductA operation";
}


productb.cpp

#include <QDebug>
#include "productb.h"

ProductB::ProductB()
{
    qDebug()<<"construct ProductB";
}

void ProductB::operationB()
{
    qDebug()<<"ProductB operation";
}


main.cpp

#include "factory.h"
#include "factorya.h"
#include "factoryb.h"
#include "product.h"

int main(int argc, char **argv)
{
    Factory *factory = new FactoryA;
    Product *product = factory->createProductA();
    product->operationA();
    product = factory->createProductB();
    product->operationB();

    return 0;
}


【运行结果】

construct Factory 
construct FactoryA 
construct Product 
construct ProductA 
ProductA operation 
construct Product 
construct ProductB 
ProductB operation 


 

【资源下载】

1 Qt设计模式1-8测试源码:http://download.csdn.net/detail/tandesir/4984275

*之所以发到第8篇博文,才放代码,是希望大家能动手实践。如利用VC平台实现设计模式。如果只是照搬,没有什么意义的。代码可能存在错误,设计模式理解可能也有偏差,恳请指正。
 声明:该源码仅供学习交流,勿用于商业目的。

 

 

转载请标明出处,仅供学习交流,勿用于商业目的

Copyright @ http://blog.csdn.net/tandesir

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值