Design Patterns: abstract factory pattern

Meaning: abstract plant will be "abstract parts" assembled into "abstract products"

Understood: compared to the factory method pattern, according to different interface to create different product, it means the interface into two interfaces, each returning different abstract products

example:

class Car //抽象产品
{
public:
	virtual void name() = 0;
};

class BenciCar: public Car
{
public:
	void name()
	{
		cout << "BenCi Car" << endl;
	}
};

class BaomaCar: public Car
{
public:
	void name()
	{
		cout << "Baoma Car" << endl;
	}
};
class Bus //抽象产品
{
public:
	virtual void name() = 0;
};

class BenciBus: public Bus
{
public:
	void name()
	{
		cout << "Benci Bus" << endl;
	}
};

class BaomaBus: public Bus
{
public:
	void name()
	{
		cout << "Baoma Bus" << endl;
	}
};
abstract factory class Factory // 
{ 
public: 
	Virtual the createCar Car * () = 0; // hides specific complex object creation 
	virtual Bus * createBus () = 0 ; // hides the specific process of creating complex object 
}; 

class BenciFactory: public Factory's 
{ 
public: 
	Car * the createCar () 
	{ 
		return new new BenciCar; 
	} 
	
	Bus * createBus () 
	{ 
		return new new BenciBus; 
	} 
}; 

class BaomaFactory: public Factory's 
{ 
public: 
	Car * the createCar () 
	{ 
		return new new BaomaCar; 
	} 
	
	* createBus Bus () 
	{ 
		return new new BaomaBus; 
	} 
};
int main() 
{
	Factory* bmf = new BaomaFactory();
	Car* bmc = bmf->createCar();
	Bus* bmb = bmf->createBus();
	bmc->name();
	bmb->name();
	
	Factory* bcf = new BenciFactory();
	Car* bcc = bcf->createCar();
	Bus* bcb = bcf->createBus();
	bcc->name();
	bcb->name();
	
	return 0;
}

Guess you like

Origin www.cnblogs.com/chusiyong/p/11433204.html