1、 Generator pattern
  Generator mode is also called builder mode . The intention of the generator pattern is to separate a complex build from its representation , So that the same build process can create different representations .
  The programming steps of generator mode :
 (1) Define a product class ;
   Because it is not done in this class Product Class object creation , So there's no need to show the definition construction method .
 (2) Definition n A generator Build class ;
   Generators are used to generate Product Object's , So in general ,Product Is a member variable of the generator class ; For every one created Product object , Essentially create first Unit1,Unit2,...UnitN, And combine them into what you need Product object , Therefore need n individual createUnit() Method and a combination method composite(); because createUnit() And composite() It's commonness , So you can define a common generator class interface ,n All generator classes are derived from this interface .
 (3) Define a unified scheduling class , It's also called the commander class . It's the interface to the generator IBuild Encapsulation .
  Generator design patterns involve 4 A key role : product (Product)、 Abstract generator (IBuild)、 Concrete generator (Builder)、 commander (Director). In generator mode , Abstract generators and concrete generators are required , Commanders need to be seriously considered in practical problems , Make a choice .

[ Code ]java Code :

?
1
2
3
public class Unit1 {}
public class Unit2 {}
public class Unit3 {}

[ Code ]java Code :

?
1
2
3
4
5
6
// Define a product class
public class Product {
     Unit1 u1;
     Unit2 u2;
     Unit3 u3;
}

[ Code ]java Code :

?
1
2
3
4
5
6
7
// Define the generator class interface IBuild
public interface IBuild {
     public void createUnit1();
     public void createUnit2();
     public void createUnit3();
     public Product composite(); // The return value is Product object
}

[ Code ]java Code :

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class BuildProduct implements IBuild { // Generate the first Product
     Product p= new Product(); //Product Is a member variable
 
     @Override
     public void createUnit1() {
         //p.u1=...          // establish Unit1
     }
 
     @Override
     public void createUnit2() {
         //p.u2=...          // establish Unit2
     }
 
     @Override
     public void createUnit3() {
         //p.u3=...          // establish Unit3
     }
 
     @Override
     public Product composite() {
         //...               // relation Unit1,Unit2,Unit3
         return p;           // return Product object p
     }
 
}

[ Code ]java Code :

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
public class Director {
     private IBuild build;
     public Director(IBuild build){
         this .build=build;
     }
     public Product build(){
         build.createUnit1();
         build.createUnit2();
         build.createUnit3();
         return build.composite();
     }
     public static void main(String[] args) {
         IBuild build= new BuildProduct();
         Director direct= new Director(build);
         Product p=direct.build();
     }
}

2、 utilize Product Derived class methods , Similar generation functions can also be realized .

[ Code ]java Code :

?
1
2
3
public class Unit1 {}
public class Unit2 {}
public class Unit3 {}

[ Code ]java Code :

?
01
02
03
04
05
06
07
08
09
10
// Define the abstract generator
public abstract class Product {
     Unit1 u1;
     Unit2 u2;
     Unit3 u3;
     abstract void createUnit1();    // Indicates that the subclass is to be created Unit1
     abstract void createUnit2();    // Indicates that the subclass is to be created Unit2
     abstract void createUnit3();    // Indicates that the subclass is to be created Unit3
     abstract void composite();      // Indicates that subclasses should be combined Unit1、Unit2、Unit3
}

[ Code ]java Code :

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Define concrete generators
public class BuildProduct extends Product{
 
     @Override
     void createUnit1() {
         
     }
 
     @Override
     void createUnit2() {
         
     }
 
     @Override
     void createUnit3() {
         
     }
 
     @Override
     void composite() {
         
     }
 
}

[ Code ]java Code :

?
01
02
03
04
05
06
07
08
09
10
11
12
13
// Define the conductor class
public class Director {
     Product p;
     public Director(Product p){
         this .p=p;
     }
     void build(){
         p.createUnit1();
         p.createUnit2();
         p.createUnit3();
         p.composite();
     }
}

Design patterns Day02 More articles about

  1. IOC Preliminary understanding of design patterns (day02)

    IOC(Inversion of Control): Inversion of control . * Other explanations : Dependency injection . dependency inversion …… Design objectives : simplify JEE R & D of , Provide IOC Containers , control bean Generation . Inject , decoupling . I read some posts on the Internet ...

  2. ordinary Dao Design patterns

    ordinary DAO Design patterns These two days I learned DAO(Data Access Object Data access object ) Design patterns . I want to talk about my feelings , It's a little hard to get in touch at first , I don't think I have a clear logic , The main reason is that the previous knowledge is relatively scattered, not very ...

  3. MVVM Design patterns and WPF In the implementation of ( Four ) Event binding

    MVVM Design patterns and in WPF In the implementation of ( Four ) Event binding Series catalog : MVVM Pattern analysis and in WPF In the implementation of ( One )MVVM Model introduction MVVM Pattern analysis and in WPF In the implementation of ( Two ) Data binding MVVM Pattern analysis and in WPF in ...

  4. java EE Introduction to design mode

    1. What is design pattern Design patterns provide solutions to common application design problems . In object-oriented programming , Design patterns are often used to solve problems related to object creation and interaction , Rather than the large-scale problems faced by the overall software architecture , They provide a common solution in the form of boilerplate code ...

  5. The thinking logic of computer programs (54) - analyse Collections - Design patterns

    Last time we mentioned , class Collections There are probably two types of functions in , The first is to operate on container interface objects , The second is to return a container interface object , In the last section, we introduced the first kind of , In this section, we introduce the second kind of . The second kind of methods can be divided into two groups : Accept other ...

  6. 《JavaScript Design patterns Zhang 》 Arrangement

    Recently, I was reading another book about design patterns <JavaScript Design patterns >, More design patterns are described in this book . One . Create design patterns Including simple factories . Factory method . Abstract factory . builder . Prototypes and singleton patterns . 1) Simple ...

  7. 《JavaScript Design patterns and development practices 》 Arrangement

    I've been studying a book recently <JavaScript Design patterns and development practices >, Advanced . One . Higher order function Higher order functions are functions that satisfy at least one of the following conditions . 1. Functions can be passed as arguments . 2. Function can be output as a return value ...

  8. The behavior pattern of design pattern is big PK

                                        The behavior pattern is big PK The behavior pattern includes the responsibility chain pattern . Command mode . Interpreter mode . Iterator pattern . Intermediary model . Memo mode . Observer mode . The state pattern . Strategy ...

  9. .NET Design pattern visitor pattern

    One . The definition of visitor pattern : Represents an operation on each element of an object structure . It allows you to define new operations on elements without changing their classes . Two . Structure and role of visitor pattern : 1.Visitor Abstract visitor role , For the sake of ...

Random recommendation

  1. 【MySQL】 Query how many tables there are in a database

    SELECT COUNT(*) TABLES, table_schema FROM information_schema.TABLES WHERE table_schema = 'performanc ...

  2. python2.X and 3.X Some of the differences 【 Sorting 】

    1. performance Py3.0 function pystone benchmark Faster than Py2.5 slow 30%.Guido Think Py3.0 There's a lot of room for optimization , It can be used in string and integer operations   To achieve good optimization results .  Py3.1 Performance ratio P ...

  3. Simulation game 1102d1

    cannon (cannon)[ Title Description ] as everyone knows , Double gun stack will be a very powerful skill in Chinese chess . When cannons eat pieces, they have to jump every other piece , It's commonly known as " Bombard the septum ". Guns and guns obviously can't fight together , therefore rly I borrowed it one day ...

  4. cf E. George and Cards

    http://codeforces.com/contest/387/problem/E The question : Here you are. n Number , And then input k Number , this k All the numbers are n In the number of , Every time you do it, you're doing it n Select the length as w A continuous sequence of ...

  5. Javascript Encapsulate the pop-up control

    1. First, define the pop-up box HTML structure <div class="g-dialog-contianer"> <div class="dialog-windo ...

  6. JS Advanced programming learning notes —— Inherit

    We know , stay OO In language , Inheritance can be divided into interface inheritance and implementation inheritance . and ECMAScript The function for is not signed , Can't achieve “ Interface inheritance ”, It can only be realized through a prototype chain “ Implementation inheritance ”. After learning all kinds of inheritance patterns , A brief summary of the various inheritance patterns ...

  7. Recent interview Java Backend development feel !

    It is necessary to read this article 4.3 minute . First episode :cnblogs.com/JavaArchitect/p/10011253.html Last week, , Several people were interviewed intensively Java Back end candidates , Work experience is in 3 To 5 Year . My standard is actually ...

  8. Interview question summary (PS: It's just the interview questions I met , It's not a classic interview question )

    One . The difference between class and structure , What are the applications .( Reference resources :https://blog.csdn.net/yikeshu19900128/article/details/40400479) 1) Class is a reference type , Data stored in ...

  9. Use Visual Studio Team Services Continuous integration ( One )—— structure ASP.NET Core

    Use Visual Studio Team Services Continuous integration ( One )-- structure ASP.NET Core summary Continuous integration (CI) It's the process of integrating code into a shared repository as often as possible . During code integration , Build interrupt or test ...

  10. axios send out json data

    var qs = require('qs'); axios.post(ajaxurl,qs.stringify({ username:'zhangsan', age:'18' })).then(fun ...