Design Pattern 05_ Creator mode

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source: http://blog.csdn.net/pistolove/article/details/46489147



Creator mode: Separates the construction of a complex object from its representation so that the same build process can create different representations.


For example, we need to build a building, we need workers to build walls, we need designers to design the house how to cover, and the designers here do not work, it is only responsible for the order to let the workers work on the line, and the real creator is the workers, they finally cover up the building. Therefore, the last is to the migrant workers to House rather than to the designer to the house. Here, the homeowner (client) only needs the worker (new Builder ()) and the designer (new Desinger), and the designer directs the Worker to work (Order2design ()), and finally the homeowner wants the House (Getroom2clinet ()) to the worker.


For more information, please see the code below to help you. Interested can download the source code on my github: Https://github.com/pistolove



The builder interface, which is responsible for the specific things that are built:

<span style= "FONT-SIZE:14PX;" >package com.design.builder;//Builder Interface, which is responsible for building what public interface Ibulider {public void Makedoor ();p ublic void Makewindow ( );p ublic void Makefloor ();p ublic void Makewall ();p ublic hostel getroom2clinet (); </span>


Concrete builders, built on the basis of design:

<span style= "FONT-SIZE:14PX;" >package com.design.builder;//Worker is responsible for building the House public class Concreatebuilder implements Ibulider {private String door = null; private string window = null;private String floor = null;private string wall = null, @Overridepublic void Makedoor () {//To Do auto-generated method StubSystem.err.println ("Start Makedoor");d Oor = "door is OK";} @Overridepublic void Makewindow () {//TODO auto-generated method StubSystem.err.println ("Start Makewindow"); window = " window is OK ";} @Overridepublic void Makefloor () {//TODO auto-generated method StubSystem.err.println ("Start Makefloor"); floor = "floor is OK ";} @Overridepublic void Makewall () {//TODO auto-generated method StubSystem.err.println ("Start Makewall"); wall = "Wall is OK ";}  @Overridepublic getroom2clinet () {if (door! = null && window! = NULL && floor! = NULL && wall ! = null) {return new (door, window, floor, wall);} return null;}} </span> 


Designers guide workers to build:

<span style= "FONT-SIZE:14PX;" >package com.design.builder;//Designer knows what the house does, but does not do it, he directs the workers to do public class Desinger {private Ibulider Bulider;public Desinger (Ibulider bulider) {this.bulider = Bulider;} public void Order2design () {Bulider.makedoor (); Bulider.makefloor (); Bulider.makewall (); Bulider.makewindow ();}} </span>


Client testing:

<span style= "FONT-SIZE:14PX;" >package com.design.builder;//homeowner to collect public class Client {public static void main (string[] args) {Ibulider Bulider = new Concreatebuilder ();D Esinger desinger = new Desinger (bulider);d esinger.order2design (); Bulider.getroom2clinet ();}} </span>


Test results:

Start Makedoor
Start Makefloor
Start Makewall
Start Makewindow
The hostel is create Ok. It contanins door is ok window are ok floor are OK wall is ok


Design Pattern 05_ 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.