Design mode: Abstract Factory mode (C + +) "Factory method Upgrade" __c++

Source: Internet
Author: User
Introduction to Abstract factory model

Design pattern in the previous episode: Factory method Mode (c + +) We use the factory method model to bring a series of benefits, we will build the product of the factory class is abstracted, a product with a dedicated factory production . We also know that when the product variety is very many, our factory class will become very much, the product has how many kinds, then we produce the corresponding product the factory row to have how many. There is no better way of difficulty. Of course, there are better design patterns to solve this problem. Just imagine a factory can only produce a product. Of course not, can produce a series of products. Then we can further abstract, the factory can produce a series of products.

Our abstract factory model is to abstract the factory, but it is the production of a series of products, it is for the product family. products corresponding to a number of product lines. For example, we use car factories for example, abstract car factories can produce BMW, Mercedes-Benz, Harvard car. Our specific factory-> China factory production of domestic BMW, domestic Mercedes Benz, domestic Harvard this series of products. BMW product family can have domestic BMW, Nissan BMW, the United States-produced BMW. Take a look at the model diagram below.

Abstract Factory pattern Model diagram
Abstract Factory Pattern Code Car.h Auto Type related code

#define _crt_secure_no_warnings #include <iostream> #include <string> using namespace std;
	BMW auto base class abstract BMW class Bmwcar {Public:bmwcar (string name) {this->name = name;
	virtual void run () = 0;//pure virtual function void SetName (string name) {this->name = name;
	String GetName () {return this->name;
} private:string name;
};
	Mercedes Benz car base class abstract Mercedes class Benzcar {Public:benzcar (string name) {this->name = name;
	virtual void run () = 0;//pure virtual function void SetName (string name) {this->name = name;
	String GetName () {return this->name;
} private:string name;
};
	Harvard Car Class abstract Havalcar {public:havalcar (string name) {this->name = name;
	virtual void run () = 0;//pure virtual function void SetName (string name) {this->name = name;
	String GetName () {return this->name;
} private:string name;
}; Domestic BMW class CHINABMW:p ublic bmwcar {public:chinabmw (string name): Bmwcar (name) {} void Run () {cout << This->getname() << "has been started. "<<" domestic BMW car welcome you.
	"<< Endl;
}
}; Domestic Benz Class Chinabenz:p ublic benzcar {Public:chinabenz (string name): Benzcar (name) {} void Run () {cout <& Lt This->getname () << "has been started. "<<" domestic Mercedes-Benz cars welcome you.
	"<< Endl;
}
}; Domestic Harvard class Chinahaval:p ublic havalcar {public:chinahaval (string name): Havalcar (name) {} void Run () {cout & lt;< this->getname () << "has been started. "<<" Domestic Harley car welcome you.
	"<< Endl;
}
}; BMW class AMERICABMW:p ublic bmwcar {public:americabmw (string name): Bmwcar (name) {} void Run () {cout <& Lt This->getname () << "has been started. "<<" The United States BMW car welcome you.
	"<< Endl;
}
}; Mercedes class Americabenz:p ublic benzcar {Public:americabenz (string name): Benzcar (name) {} void Run () {cout & lt;< this->getname () << "has been started. "<<" American Mercedes Benz welcomes you.
	"<< Endl;
}
}; US Harvard class Americahaval:p ublic havalcar {public:americahaval (string name): Havalcar (name) {} voidRun () {cout << this->getname () << "has started. "<<" The American Harley Car welcomes you.
	"<< Endl;
}
}; A small Japanese-made BMW class JAPANBMW:p ublic bmwcar {public:japanbmw (string name): Bmwcar (name) {} void Run () {cout &LT;&L T This->getname () << "has been started. "<<" small Japanese-made BMW welcome you.
	"<< Endl;
}
}; Small Japanese Mercedes Benz class Japanbenz:p ublic benzcar {Public:japanbenz (string name): Benzcar (name) {} void Run () {cout &L t;< this->getname () << "has been started. "<<" small Japanese-made Mercedes Benz welcome you.
	"<< Endl;
}
}; The little Japanese-made Harvard class Japanhaval:p ublic havalcar {public:japanhaval (string name): Havalcar (name) {} void Run () {Cou T << this->getname () << "has been started. "<<" Little Japanese-built Harley Car welcomes you.
	"<< Endl; }
};

AbstractFactory.cpp Abstract Factory code
#define _crt_secure_no_warnings #include <iostream> #include <string> #include "Car.h" using namespace std
; /* Abstract factory Model for the product family each specific factory will produce different series of products/class Abstractcarfactory {public:virtual bmwcar* createbmwcar () = 0;//pure virtual function vir
	Tual benzcar* createbenzcar () = 0;
Virtual havalcar* createhavalcar () = 0;
}; Chinese factories in China production of series of products class Chinacarfactory:p ublic abstractcarfactory {public:bmwcar* Createbmwcar () {return new Chinese
	BMW ("China BMW");
	} benzcar* Createbenzcar () {Return to New Chinabenz ("China Benz");
	} havalcar* Createhavalcar () {Return to New Chinahaval ("Harvard China");
}
}; American Factory class Americacarfactory:p ublic abstractcarfactory {public:bmwcar* Createbmwcar () {return new AMERICABMW ("Beauty
	National brand BMW ");
	} benzcar* Createbenzcar () {Return to New Americabenz ("American brand Mercedes");
	} havalcar* Createhavalcar () {Return to New Americahaval ("American Licensing Harvard");
}
}; Japanese factory class Japancarfactory:p ublic abstractcarfactory {public:bmwcar* Createbmwcar () {Return to New JAPANBMW ("Little Japanese-made BMW
	"); } Benzcar* Createbenzcar () {Return to New Japanbenz ("Small Japanese Mercedes Benz");
	havalcar* Createhavalcar () {Return to New Japanhaval ("Little Japanese built Harvard");
}
};
	int main (int argc, char *argv[]) {abstractcarfactory *chinafactroy = new Chinacarfactory ();
	Abstractcarfactory *americafactroy = new Americacarfactory ();
	Abstractcarfactory *japanfactroy = new Japancarfactory ();
	Chinese factory-produced series of cars bmwcar* CHINABMW = Chinafactroy->createbmwcar ();
	benzcar* Chinabenz = Chinafactroy->createbenzcar ();
	havalcar* chinahaval = Chinafactroy->createhavalcar ();
	American factory-produced series of cars bmwcar* AMERICABMW = Americafactroy->createbmwcar ();
	benzcar* Americabenz = Americafactroy->createbenzcar ();
	Havalcar*americahaval = Americafactroy->createhavalcar ();
	Small Japanese factory production of a series of cars bmwcar* JAPANBMW = Japanfactroy->createbmwcar ();
	benzcar* Japanbenz = Japanfactroy->createbenzcar ();

	Havalcar*japanhaval = Japanfactroy->createhavalcar ();
	The car ran up ...
	CHINABMW->run ();
	Chinabenz->run ();

	Chinahaval->run (); AmeRicabmw->run ();
	Americabenz->run ();

	Americahaval->run ();
	Japanbmw->run ();
	Japanbenz->run ();

	Japanhaval->run ();
return exit_success; }
Code Run Results



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.