Java classic 23 design modes: creative mode (2)
This article records the remaining two types of five creation modes: Builder mode and PROTOTYPE mode ).
1. Builder mode (alias: builder Mode)
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. A complete builder model includes the following concepts:
1. ProductProduct
public 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 void setBody(String body) { this.body = body; } public String getFoot() { return foot; } public void setFoot(String foot) { this.foot = foot; }}
2. Abstract builder
public interface PersonBuilder { void buildHead(); void buildBody(); void buildFoot(); Person buildPerson();}
3. Specific builder ConcreteBuilder
Public class ManBuilder implements PersonBuilder {Person person; public ManBuilder () {person = new Man ();} public void buildbody () {Person. setBody ("build a man's body");} public void buildFoot () {person. setFoot ("building man's feet");} public void buildHead () {person. setHead ("build a man's head");} public Person buildPerson () {reture person ;}}
If there are multiple specific products, there will be multiple specific builders who implement common interfaces. For example, if you are building a woman, you can define WomanBuilder.
4. The Director class ctor acts as an encapsulation function to prevent high-level modules from going deep into the builder's internal implementation. If the builder is complicated, multiple Director classes are required.
public class PersonDirector { public Person constructPerson(PersonBuilder pb) { pb.buildHead(); pb.buildBody(); pb.buildFoot(); return pb.buildPerson(); }}
Test code:
public class Test{ public static void main(String[] ar*s) { PersonDirector pd = new PersonDirector(); Person person = pd.constructPerson(new ManBuilder()); System*out.println(person.getBody()); System.out.println(person.getFoot()); System.out.println(person.getHead()); }}
Applicability:
1. When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
2. When the constructor must allow different representations of the object to be constructed.
Note: To put it bluntly, the builder mode is to build an object and a general process needs to be executed. Different objects are created and encapsulated into a specific Builder as needed. To obtain the final object, you only need to input the corresponding Builder to the director class.
Reference: http://www.cnblogs.com/muzongyan/archive/2010/08/27/1810307.html
Ii. prototype mode
The purpose is to clone an existing object without creating a new () object. The cloned object can be divided into shortest copy and deep copy. The objects copied from the shortest are the same as the original object memory address. The object copied in depth is different from the original object memory address. The steps are as follows:
1. Declare a clone interface Cloneable
public class Prototype implements Cloneable { private String name; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public Object clone(){ try { return super.clone(); } catch (Exception e) { e.printStackTrace(); return null; } }}
2. Specific prototype
publ*c class ConcretePrototype extend* Prototype { public ConcretePrototype(String name) { setName(name); }}
Test code:
public clas* Test { public static void main(String[] args) { Prototype pro = new ConcretePrototy*e("prototype"); Prototype pro2 = (Prototype)pro.clone(); *ystem.out.println(pro.getName()*; System.out.println(pro2.getName()); }}
Applicability:
1. When a system should be independent of its product creation *, composition, and representation.
2. When the class to be instantiated is specified at the runtime, for example, through dynamic loading.
3. To avoid creating a factory * level parallel to the product class level.
4. When an instance of a class can only have one of several combinations of different States.
Creating prototype and cloning them may be more convenient than manually instantiating this type with the appropriate state each time. In short, the original model mode specifies the type of the object to be created by giving a prototype object, and then creates more objects of the same type by copying the prototype object. The original model mode allows you to dynamically add or remove product classes. The product class does not need to have any predefined level structure. The original model mode applies to any level structure. The disadvantage is that each class must have a clone method. For more information, see link 1. Link 2.