[设计模式]<1>. C++与工厂模式

原文地址:

http://www.cnblogs.com/hebaichuanyeah/p/5459869.html

工厂模式是指,定义一个创建对象的类(factory),利用这个来实例化指定子类。

1.简单工厂模式

例子,假如有一个code代码类,java,C#,C艹等等是该类的子类,通过codeFactory来实例化子类。并在子类中重写virtual函数print。

 

注:C艹中,如果用父类的指针释放子类的资源,并且父类中的析构函数不是virtual函数,那么子类的析构函数不会调用,子类的资源不会释放,从而造成内存泄露。

#include "iostream"
using namespace std;

class Code
{
public :
    Code(){};
    virtual void print(string str)=0;
    virtual ~Code(){};
};

class CSharp : public Code
{
public :
    CSharp(){}
    void print(string str)
    {
        cout<<"Console.WriteLine(\""<<str<<"\");"<<endl;
    }
};

class CFuck : public Code
{
public :
    CFuck(){}
    void print(string str)
    {
        cout<<"cout<<\""<<str<<"\"<<endl;"<<endl;
    }
   
};

class CProgram : public Code
{
public :
    CProgram(){}
    void print(string str)
    {
        cout<<"printf(\""<<str<<"\");"<<endl;
    }

};

class Java :public Code
{
public :
    Java(){}
    void print(string str)
    {
        cout<<"System.out.printf(\""<<str<<"\");"<<endl;
    }
};

class CodeFactory
{
public :
    Code * createWhatCode(string name)
    {

        if(name == "CSharp")
            return new CSharp();
        if(name == "Java")
            return new Java();
        if(name == "CProgram")
            return new CProgram();
        if(name == "CFuck")
            return new CFuck();
    }
};
main()
{
    CodeFactory factory;
    string programName[4]={"CSharp","CProgram","CFuck","Java"};

    for(int i=0;i<4;i++)
    {
        Code * code = factory.createWhatCode(programName[i]);
        code->print("hello world");
        delete code;
    }

}

 运行结果

 

2.工厂方法模式

相对于简单工厂模式,工厂方法模式值每一个子类都有一个工厂类。如下代码:

#include "iostream"
using namespace std;

class Code
{
public :
    Code(){};
    virtual void print(string str)=0;
    virtual ~Code(){}
};

class CSharp : public Code
{
public :
    CSharp(){}
    void print(string str)
    {
        cout<<"Console.WriteLine(\""<<str<<"\");"<<endl;
    }
};

class CFuck : public Code
{
public :
    CFuck(){}
    void print(string str)
    {
        cout<<"cout<<\""<<str<<"\"<<endl;"<<endl;
    }

};

class CProgram : public Code
{
public :
    CProgram(){}
    void print(string str)
    {
        cout<<"printf(\""<<str<<"\");"<<endl;
    }

};

class Java :public Code
{
public :
    Java(){}
    void print(string str)
    {
        cout<<"System.out.printf(\""<<str<<"\");"<<endl;
    }
};



class CodeFactory
{
public :
    virtual Code * createCode() = 0;
    virtual ~CodeFactory(){}
};

class CProgramFactory :public CodeFactory
{
public :
    Code * createCode()
    {
        return new CProgram();
    }
};

class CFuckFactory:public CodeFactory
{
public :
    Code * createCode()
    {
        return new CFuck();
    }
};

class JavaFactory:public CodeFactory
{
public :
    Code * createCode()
    {
        return new Java();
    }
};

class CSharpFactory:public CodeFactory
{
public :
    Code * createCode()
    {
        return new CSharp();
    }
};

main()
{
    CSharpFactory factory1;
    JavaFactory   factory2;
    CFuckFactory  factory3;
    CProgramFactory factory4;

    Code * code;

    code = factory1.createCode();
    code->print("hello world");
    delete code;

    code = factory2.createCode();
    code->print("hello world");
    delete code;

    code = factory3.createCode();
    code->print("hello world");
    delete code;

    code = factory4.createCode();
    code->print("hello world");
    delete code;
}

 运行结果:

 

3.抽象工厂模式

抽象工厂模式可以多个抽象产品类。

情景:假设工厂A生产诺基亚的手机和平板,工厂B生产苹果的手机和平板。而诺基亚的手机和苹果的手机继承自手机类,同理诺基亚的平板和苹果的平板继承自平板类。

#include <iostream>
using namespace std;

class Tablet
{
public:
    virtual void showMessage() =0;
    virtual ~Tablet(){}
};

class MobilePhone
{
public:
    virtual void showMessage() =0;
    virtual ~MobilePhone(){}
};

class NokiaPhone : public MobilePhone
{
public:
    void showMessage()
    {
        cout<<"Nokia Mobile Phone"<<endl;
    }
};

class ApplePhone : public MobilePhone
{
public:
    void showMessage()
    {
        cout<<"Apple Mobile Phone"<<endl;
    }
};

class NokiaTablet :public Tablet
{
public:
    void showMessage()
    {
        cout<<"Nokia Tablet"<<endl;
    }
};

class AppleTablet :public Tablet
{
public:
    void showMessage()
    {
        cout<<"Apple Tablet"<<endl;
    }
};

class Factory
{
public :
    virtual MobilePhone * creatPhone()=0;
    virtual Tablet * creatTablet()=0;
	virtual ~Factory(){}
};

class NokiaFactory :public Factory
{
public :
    MobilePhone * creatPhone()
	{
		return new NokiaPhone();
	}
    Tablet * creatTablet()
	{
		return new NokiaTablet();
	}
};

class AppleFactory :public Factory
{
public :
    MobilePhone * creatPhone()
	{
		return new ApplePhone();
	}
    Tablet * creatTablet()
	{
		return new AppleTablet();
	}
};


main()
{

    Tablet* tablet;
	MobilePhone* mobilePhone;
	Factory* factory;

	factory = new NokiaFactory();
	tablet = factory->creatTablet();
	mobilePhone = factory->creatPhone();

	tablet->showMessage();
	mobilePhone->showMessage();

	delete factory;
	delete tablet;
	delete mobilePhone;

	factory = new AppleFactory();
	tablet = factory->creatTablet();
	mobilePhone = factory->creatPhone();

	tablet->showMessage();
	mobilePhone->showMessage();

	delete factory;
	delete tablet;
	delete mobilePhone;

}

 运行:

posted @ 2016-05-04 21:33  默默地EEer  阅读(670)  评论(0编辑  收藏  举报