Design mode-Creator mode

Source: Internet
Author: User

The core idea is to separate a "construction algorithm of complex object" from its "component and assembly mode", so that the component algorithm and assembly can be independently changed, and the same building algorithm can be used to create different representations, and the different construction processes can reuse the same assembly method.

Main advantages and disadvantages in the creator mode, the client is no longer responsible for object creation and assembly, but rather the responsibility of the object creation to its specific creator class, the Assembly of responsibility to the Assembly class, the client is only responsible for the invocation of the object, thus clarifying the responsibilities of each class. While it is possible to create different types of products with the creator pattern, if the differences between the products are huge, you need to write multiple creator classes to implement if this is a better combination of Factory mode. Part

The creator pattern consists of five parts: The Assembly class, the abstract creator class, the concrete creator class that implements the abstract creator class, the abstract product, and the specific product class that implements the abstract product.

code example
Personpublic class Person {private String head;private string body;private string Foot;public string GetHead () {return Head;} public void Sethead (String head) {this.head= head;} Public String GetBody () {return body;} Public String Getfoot () {return foot;} public void Setfoot (String foot) {this.foot= foot;} public void Setbody (String body) {this.body= body;}} Builder Interface Public Interface Personbuilder
{void Buildhead (); void Buildbody (); void Buildfoot (); Person Buildperson ();} Builder implementation: Human Publicclass Humanbuilder implements Personbuilder
{Private person person; Public Humanbuilder ()
{this.person=new person (); } public void Buildbody ()
{This.person.setBody ("Create human body! "); }publicvoid Buildfoot ()
{This.person.setFoot ("Create human feet! ");} Publicvoid Buildhead () {This.person.setHead ("Create Human head!") ");} Public Person Buildperson () {return person;}} Builder implementation: Transformers Publicclass Transformerbuilder implements Personbuilder {private person person;public Transformerbuilder () {This.person=newperson ();} Publicvoid Buildbody () {this.person.setBody ("Create Transformers body! ");} public void Buildfoot () {this.person.setFoot ("Create Transformers feet! ");} Publicvoid Buildhead () {this.person.setHead ("Create Transformers head! ");} Public Person Buildperson () {return person;}} Director Build Wizard Publicclass Persondirector
{Public person Constructperson (Personbuilder pb)
{Pb.buildhead (); Pb.buildbody (); Pb.buildfoot (); return Pb.buildperson (); }}//Testing Publicclass Test
{publicstaticvoid main (string[] args)
{persondirector PD = new Persondirector (); Person person = Pd.constructperson (new Humanbuilder ()); System.out.println (Person.getbody ()); System.out.println (Person.getfoot ()); System.out.println (Person.gethead () + "\ n"); Person transformer = Pd.constructperson (Newtransformerbuilder ()); System.out.println (Transformer.getbody ()); System.out.println (Transformer.getfoot ()); System.out.println (Transformer.gethead ()); }}

Design mode-Creator mode

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.