Design mode - creation mode

xiaoxiao2021-03-06  61

1. Factory mode

a) Structure:

Note: 1) Product A and Product B have the same parent class, but have different implementations.

2) Creator can determine the required instantiated class according to different input variables.

b) Advantages:

i. Separate the method and processing, and processing can be present independently of the method name.

c) Use the environment:

i. When a class cannot static determination of the class of the object she must create. (Client can get the desired Product subclass by Creator when running.

II. When a class wishes to achieve the behavior she defined by her subclass. (From Product A and Product B to define the behavior of Products)

Iii. When you want to create a class of information, you will be partized. (You can put the IF-ELSE code, put it in Creator)

d) Code instance:

Public interface product {

Public void method ();

}

Public Class Producta Implements Product {

Public void method () {

Do Some

}

}

Public Class ProductB IMPLEments Product {

Public void method () {

Do Another Thing;

}

}

Public class creator {

Public product getProduct () {

IF (some condition) {

Return new producta ();

} else {

Return new productb ();

}

}

}

e) Summary:

i. You can select one of the similar classes according to the input conditions and instantiate it.

Ii. The current trend is that a Creator is only responsible for instantizing a product, on the one hand, can make the code of the Creator short, and on the other hand, the IF-ELSE structure can be avoided.

2. Abstract Factory mode

a) Structure:

Note: 1) Factory and Producta, ProductB form a level.

2) Factorya and Factoryb inherit Factory and instantiate the subclasses of ProductA and ProductB.

b) Advantages:

i. Alternative to the entire product can be implemented by replacing the Concrete Factory.

c) Use the environment:

i. A system to be independent of her product creation, combination, and representation. (For example, the UI in Java, the Client's LookandFeel can be independent of the creation and representation of the control.)

II. A system is configured by one of multiple product lines. (A system can be configured by Product A1 and Product B1 or Product A2 and Product B2)

Iii. When you want to emphasize a series of related product objects to be used in combination.

Iv. When you provide a product class library, just want to display their interfaces instead of implementation (Client brings only PRODUCT A and PRODUCT B interface through Factory)

d) Code instance

Public interface factory {

Public producta getProducta ();

Public productB getProductB ();

}

Public Class Factorya Implements Factory {

public

PO

Rducta getProducta () {

Return new producta1 ();

}

Public productb getProductB () {

Return new productb1 ();

}

}

Public Class Factoryb IMPLEments Factory {

public

PO

Rducta getProducta () {

Return new producta2 ();

}

Public productb getProductB () {

Return new productb2 ();

}

}

Public interface productA {

}

Public Class Producta1 Implements Producta {}

Public Class Producta2 Implements Producta {}

Public interface productb {

}

Public Class ProductB1 Implements ProductB {} PUBLIC CLASS ProductB2 Implements ProductB {}

e) Summary:

i. Similar to Factory, but returning a set of classes, not only a class, from some extent, is Abstract in the Factory mode at a higher level.

II. AbstractFactory delays the creation of the product object to his implementation class (Factorya and Factoryb)

III. AbstractFactory mode helps you control the class of an object created. Because a factory package creates the responsibility and process of the product object. She separates the customer with the class. The customer manipulates an example through their abstract interface. Class names are also separated in the implementation of specific factories, they do not appear in customer code.

IV. It is difficult to produce new types of products because the AbstractFactory interface limits the collection of products that can be created, supporting new types of products that require expansion of plant interfaces.

The UI section in SWING is an application of Abstract Factory.

3. Singleton mode

a) Structure:

Note: There is only one Singleton instance in the overall situation.

b) Advantages:

i. Controlled access to the unique instance.

II. Reduce the namespace. (It is an alternative to global variables to avoid global variables of the only instance of the unique instance.

III. Allows variable objectives. (Example count can be performed in the Singleton class)

c) Use the environment:

i. When the class can only have one instance and the customer can access her from a well-known access point.

II. When this unique instance should be expandable through subclassization, and the customer should use an extension instance without changing the code. (Simply modify the method in Singleton)

d) Code instance

i. Throw an exception:

Public class singleton {

Static Boolean

FLA

g = false;

Public Singleton () THROWS SINGETONEXCEPTION {IF (! Flag) {

FLA

g = true;

} else {

Throw new startnexception ();

}

}

}

Advantages: can be subclassified.

Disadvantages: You cannot share an instance, the client is to capture an exception.

Ii. Static class:

Public class singleton {

Public static methoda () {}

Public static methodb () {}

}

Advantages: No processing exception.

Disadvantages: If you want to change this class to support multi-instance access, it is more troublesome.

Iii. Static method:

Public class singleton {

Private static singleton instance = null;

PRIVATE SINGLETON () {...}

Public static singleleton getsingleton () {

IF (instance == null)

Instance = new singleleton ();

Return Instance;

}

}

Advantages: Don't worry that it will throw an exception when multiple instances are needed, you can easily support shared instances and instance multiple instances.

Disadvantages: No subclass. (Because the constructor is private)

e) Summary:

i. Ensure that the entire system is available and there is only one example, and the instance can be accessed in various parts of the system.

Ii. The MATH class in Java is a Singleton application.

4. Builder mode

a) Structure:

Note: 1) Similar to Factory, instantiate the object to the Builder.

2) According to the tag identified by Director, Builder can construct a complex object step by step.

3) Compared with Factory, programmers can have more participation in the construction process.

b) Advantages:

i. Programmers can participate more, construct more complex objects. (Define complex implementation logic in Concretebuilder)

II. Separate a complex object with his representation, making the same construct process differently. (Ie Director and Builder can be changed separately)

c) Use the environment:

i. When the algorithm for creation complex objects should be independent of the component of the object and their assembly methods.

II. When the construction process must allow the object to be constructed to have different representations.

d) Code instance:

Public class director {

Private Builder Builder = New Builder ();

Public void build () {

IF (Condition a) {

Builder.buildparda ();

} else if (condition b) {

Builder.buildpardb ();

} else {

Builder.buildpardc ();

}

}

}

Public interface builder {

Public void buildparta ();

Public void buildpartb ();

Public void buildpartc ();

}

Public class concretebuilder imports builder {public void buildparta () {...}

Public void buildpartb () {...}

Public void buildpartc () {...}

}

e) Summary:

i. She first assumes that an object needs to be composed of several other objects, but how these objects are combined and layout.

Ii. Director By generating events, Builder responds to events, thereby constructing a step by step. This is the so-called construction and representation of the steps that are constructed or specified by Director, and the specific representation is performed by Builder.

III. Analysis of XML files is an application of Builder mode. Parser is Director, defaulthandler is Builder, which can customize the resolution results of the XML file by extending DefaultHandler.

IV. XSLT is also an application of a Builder mode. By providing different XSL files (equivalent to Builder), different outputs can be obtained from the same XML file, and Parser analyzing the XML file is equivalent to Director.

5. Prototype mode

转载请注明原文地址:https://www.9cbs.com/read-88455.html

New Post(0)