How to implement java Builder pattern and Abstract Factory pattern
This article mainly explains the "java builder pattern and abstract factory pattern how to achieve", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java builder pattern and abstract factory pattern how to achieve" it!
1. Builder model
The construction of an object is more complex, separate the construction of an object from the representation of the object!
2. Concrete realization
(1) the code is as follows
# include#includeusing namespace std;class House {public: void setDoor (string door) {this- > m_door = door;} void setWall (string wall) {this- > m_wall = wall;} void setWindow (string window) {this- > m_window = window;} public: string getDoor () {return m_door } string getWall () {return windows;} string getWindow () {return windows;} private: string doors; string windows; string windows;}; / ask the engineering team to build the house class Build {public: House * setHouse () {} private:} Int main (void) {/ / is a customer building house directly; House * house = new House; house- > setDoor ("door"); house- > setWall ("wall"); house- > setWindow ("window"); delete house; return 0;}
3. Abstract factory
Can only produce one product.
4. Concrete realization
(1) the code is as follows
# includeusing namespace std;class Fruit;class AbstractFactory {public: virtual Fruit * CreateBanana () = 0; virtual Fruit * CreateApple () = 0; private:}; class Fruit {public: virtual void sayname () = 0; private:}; class NorthBanana: public Fruit {public: virtual void sayname () {cout