Create mode ----- Creation Pattern
The creation mode is an abstraction of the class instantiation process.
Some systems require dynamic decisions when creating objects, how to create objects, create which objects, and how to combine, indicate these objects. Creating a mode describes how to construct and encapsulate these dynamic decisions.
Creating a mode and creating a creation mode for creating modes and objects.
The Class L Class: Create mode use inheritance, delay the creation of the class to the subclass, and encapsulate what specific class information will get the client, and hide how these classes are created and put Together. ,
l The creation mode of the object: The creation mode of the object will give the object's creation process to another object, so that the dynamic decision which specific classes of the client will get, and how these class instances are created and combined.
Factory model
Factory model is responsible for instantiating a large number of common interfaces. Factory model can decide which class instantiation, do not have to know which class is to be instantified in advance.
Several forms of factory model:
l Simple factory model. Static factory model is also known.
l Factory method mode. Factory Method, also known as Virtual Constructors.
L abstract factory model. Abstract Factory, also known as the toolbox (Kit or Toolkit) mode.
The gradual date of factory model reflects abstract step by step, solving problems.
Simple Factory -------- Simple Factory
Simple factory model is an instance of what product class that is created by a factory object.
Ordinary class diagram of simple factory model:
Factory
S-Factory
Create (): C-P
P - Interface
P - Concrete
Static creation --- Create
Specific product
Abstract product
Simple factory mode design to the role:
1. Factory Creator Role: This role is the core of factory model, which contains closely related business logic. Factory class creates a product object under the call of the client, which is often another specific Java class implementation.
2. Abstract Product (ABSTRACT Product) Role: It provides a common interface for specific products (types). It is the return type of factory method.
3. Concrete Product Roles: Objects created by factory methods are an instance of it.
Features of simple factory model: Static method returns an object instance.
Abstract product
Specific product
Other class diagrams of simple factory model:
l This is a relatively extreme:! There are also Java class libraries.
l Large example of instance. Degraded simple factory model.
l The second type: factory role and abstract product merger.
The constructor of the single mode and polymorphic mode is private - except for direct instantiated functions, they have their own static factory methods, providing their own instance.
When single mode and polymorphism mode uses a collection to store yourself to create objects to get a memo mode when you get the object created by querying this collection.
MVC mode. The MVC mode is not a strict design pattern, but a higher level of architecture. The MVC mode can be broken down into a combination of several design patterns, including synthesis patterns, policy modes, observer patterns, and may include decorative mode, mediation mode, iterative score, and factory method mode.
Simple factory models say that the created objects often belong to a product level structure. The level structure of this product makes the view (view) in the MVC mode, and the factory role itself enables the controller. AbstractView
View
View
CONTROLLER
Simple factory model uses a static factory method, and the static method cannot be inherited by subclasses, so the plant character cannot form a grade structure given. This disadvantage has been resolved in the factory model.
Simple Factory Mode The Chinese factory role is the key role of the system. It is related to the fate of the system, and it has also concentrated all logic. This makes the system become abnormal when expanding the future.
Simple factory model is not enough to support "open" principles. This mode is fully supported when the product role is expanded. But when there is a new product role to join, you have to modify the factory role to adapt. It supports "opening and closing" principles in limited extent.
Factory method returns an abstract product type is called "for abstract programming." This is the application of relying on the principle of reverse. This is to use the specific product class to hide its real type, which is the extensibility of the system. If there is a new specific product class in the future to join the system, then the factory class can give it to the client object to replace a new sub-class instance, and there is no impact on the client.
Factory method mode
Factory method model is to define an interface of a factory role, postpone the creative work of century to the subclass of factory roles.
Factory classes in the factory method model are no longer responsible for the creation of all products, but to make specific creation work to subclasses. This core class becomes an abstract factory role. Abstract plant characters only give interfaces that must be implemented in specific factories, without contacting which product class should be instantiated.
This further abstract result allows the factory method mode to allow the system to introduce new products without modifying the specific plant role.
Factory method pattern of basic graph:
Abstract Factory
Concrete Factory
P - Interface
P - Concrete
Create
Factory model:
l Abstract Factory (Abstract Factory) Role: This role makes the core of the factory model, it is independent of the application. It can be held by an interface or abstraction.
l Concent Factory Role: The class that serves this role is a specific class that implements the interface of the abstract factory. The specific factory role contains closely related logic related to application, and is called by the application to create objects.
l Abstract product role: Factory method mode says the parent type of the object that is created, that is, the common interface of the specific product.
l Specific product role: This role thinks about the interface declared by the abstract product role. Each object created by the factory method is an example of a specific product role.
Abstract factory model
Abstract factory is the most abstract and universal form of factory model.
The problem facing the factory model is a grade structure of a product, and abstract plants need to face a number of products.
Abstract factory class diagram:
Creator1
Createa: Producta
Createb: ProductB
Creator2
Createa: Producta
Createb: ProductB
Interface Producta
ProductA1
Producta2
Interface Productb, INTERFACUTB
Productb1
Productb2
Interface Creator
Createa: Producta
Createb: ProductB
The abstract factory is the product family in the product level structure:
The specific factory Creator1 inherits the product structure of the product Productut * 1 after inheriting the abstract factory Creata: Producta Createb: ProductB product structure. Landscape with portrait products with portrait products together with abstract factories.
Under what circumstances use abstract factory mode: [GOF] [宏]
l A system should not depend on how the product instance is created, combined, and expressed, which is critical to all forms of factory models.
l This system product has an excess product family, and the system only consumes the products of one of them. This is also the original intention of the abstract factory.
l It is used with products that belong to the same product family, this constraint must be reflected in the design of the system.
l The system provides a library of a product class, all products appear in the same interface, so that the client does not depend on implementation.
Site mode ----- (Sigleton)
Synnitus mode ensures that a class has only one instance, and it is instantiated and supplied to the entire system. This class becomes a single class.
Pixabay points:
l A certain class has only one instance.
l It must create this instance itself.
l It must provide this instance throughout the system.
l Construction functions Private: Not being instantiated by external, nor is it inherited.
Sample mode: Explorer, Recycle Bin, Printer Resource.
Structure of single mode:
0..1
1
Hungry Chinese single class:
EAGER SINGLETION
-m instance: EAGERSINGLETION = New EAGERSINGLETION ()
-Eagersingletion () // constructor is private
getInstance (): EAGERSINGLETION / / Returns this instance, static factory method
Create
Code: Hungry Chinese style
Public class electionletion {
Private static final eagersingletion m_instance = new EAGERSIINGLET ();
Private EagerSingletion () {} // Private constructor to prevent NEW
/ **
* Static factory method
* /
Public static elersionllion getInstance () {
Return M_INSTANCE;
}
}
Lazy Lite:
Lazy singletion
-M instance: lazy rsingletion = NULL;
- lazysingletion () // Constructor privatization
getInstance (): lazysingletion // Returns this instance, static factory, thread security
Create
Code: Lazy Litex
Public class lazysingletion {
Private static final lazySingletion m_instance = NULL;
Private lazySingletion () {} // Private constructor to prevent it from being new
/ **
* Thread safety, static factory method, guarantee return unique example
* /
Public static synchronized EAGERSINGLETION GETISTANCE () {
IF (m_instance = null) {
m_instance = new EAGERSINGLETITY ();
}
Return M_INSTANCE;
}
}
Register-style: Register Lite-state class is GOF designed to overcome the shortcomings of the inheritance of the two. It is only the way it is instantiated by its subclasses can only be lazy - this is unable to change.
Regsingletion
-m registry: Hashmap m_ registry = new hashmap ();
# Regsingletion () // Constructor protection type - can be inherited by subclasses
getInstance (name: string): regsingletion // Returns this instance
About (): String
Create
Code: Register Litex / * Note: P213 Printing error static keyword location is wrong * /
Public class regsingletion {
Private static hashmap m_ registry = new hashmap ();
/ **
* Static block, automatically load before class declaration
* /
STATIC {
Regsingletion x = new regsingletion ();
M_ registry.put (x.getclass (). getname (), x);
}
Protected regsingletion () {} // The constructor protected, can be inherited by subclasses!
/ **
* Thread safety, static factory method, guarantee return unique example
* /
Public static synchronized regsingletion getInstance (String name) {
IF (Name = = null) {
Name = "com.javaptterns.singletion.regsingletion";
}
IF (m_ registry.getname (name) = = null) {
Try {
M_ registry.put (name, class.Forname (). Newinstance ());
} catch (exception e) {
}
}
Return M_INSTANCE;
}
Public string about () {
Return "Work Complete";
}
}
Conditions using single mode:
It should be used when a system requires an instance of the class.
example:
l Can the full variable in the system can put it in a single-state class?
---- No! A design properly should not have a so-called "full variable", which should be placed in a class corresponding to the entity they describe. If these variables are drawn out in an unconnected single-class class, these classes will cause these classes to generate erroneous dependencies and coupling relationships.
l The connection object with the database should not be designed into a single mode. Because a database connection can have several instances.
Litex class in Java language:
Runtime Objects have unique Runtime objects every Java application. Through this object, the application can interact with the running environment. The Runtime class provides a static factory method.
Public static runtime getrntime ();
The use of Runtime objects often includes: executing external commands, returning existing memory, running garbage collectors, loads a dynamic class library.
Process pro = runtime. GetRntime (). EXEC ("notpad.exe");
Java.awt.Toolkit Class: