Java Design Pattern(2)

A, Factory's design pattern: the most common pattern, create a new object, eg A a = new A (); factory model benefits: the factory model can be done separately created objects proposed, plays the role of decoupling, i.e. : If you want to modify the logic to create the object without modifications in the project's everywhere, and only need to modify one inside the factory on it, greatly reducing the workload changes.

This pattern creates design pattern belongs, it is only an object interface definition created, and by its specific subclasses responsible for creating objects by instantiating different subclasses of objects. First, FIG Factory class diagram (Class Diagram) Method mode structure, wherein:

  1. Product defines the objects created by the factory method unified interface.
  2. ConcreteProduct specific class, to achieve Product Interface.
  3. Creator is generally an abstract class, declare a number of factory method (method), it was created by an object of type Product. Because it can "produce" objects, so called the factory method. Creator may also have a default method to create a specific object.
  4. ConcreteCreator overloaded factory method to create a specific example of a ConcreteProduct.

That Creator dependent type ConcreteProduct create Product objects in ConcreteCreator. Factory method application code just processed Product interfaces, regardless of the particular class (ConcreteProduct), code reusability is enhanced, since it is independent of the specific user-defined class.

Scope Summary of the factory pattern 
can not foresee what kind of example you need to create • When the coding. 
• A class uses its subclasses to create objects. 
• developers do not want to create an instance of the class to which the information and how to create instances of exposure to external programs.

Two, Singleton Singleton pattern: the let One class only has One instance, something like the Parameter static, the Save Memory CAN,
           Beneficial to Garbage Collection Singleton is a creation model, which is used to ensure that only produce one instance, and it provides access to a global access point. for some classes, the guarantee only one instance is important, such as sometimes, the database connection or Socket connection is subject to certain restrictions, must be maintained at the same time there is only one connection. As another example, the collection set can not contain duplicate elements, added to the set where the object must be unique, if duplicate values are added to the set, it only takes one instance .JDK the official use of the Singleton pattern to achieve the set of this feature, you can see the internal static class java.util.Collections SingletonSet in the original code. in fact, Singleton is one of the simplest but also the most widely used model in the JDK can be seen everywhere.

In order to achieve Singleton pattern, we need is a static variable, can whether the memory has produced a instance without creating the object of a static variable or static method can be called directly, without producing concrete examples, such variable or method will not instantiate the class may change. structure can be seen in Figure 1, uniqueInstance is this independent static variable, it can remember whether the object has been instantiated in the static method instance of this variable to judge, if not instantiated through a new object is generated, if you have instantiated object is no longer generate new, still return to previously created instance.

 

Three, Prototype Prototype mode: the same as clone ()

1. Definition: a prototype model is used to indicate the type of object to be created by a prototype object, and then use this method to copy an object to create an object of type more pain.

2, the principle: there are two parts, concrete and abstract prototype prototype.

3, use of time: The system needs to be created to eat objects are dynamically loaded, and when the product has a certain level, you can consider using the prototype model.

> When the class to be instantiated at run time are specified, e.g., by dynamic loading;

> Factory or in order to avoid creating a parallel class hierarchy and class hierarchy of products;

> Instance of a class or when there is only one of several different states combinations.

> Establishing a corresponding number of prototypes and clone them may be more convenient than the state of each hand with a suitable class is instantiated.

4, the effect of:

> Can run again time to add and delete products.

> You can be specified by changing the value of the product.

> New objects can be specified by changing the structure.

> Reduced class constructor

> May be dynamically configured based applications.

5, to achieve:

> Using a prototype manager

> Achieve cloning operations (shallow vs. deep copy)

> Initialization clone.

6, prototype models intended: Specifies the type of object prototype created instance, and create new objects by copying the prototype.

7, to solve the problem:

For example, there is an object, the object at some point already contains some effective value at this time and may need a new object of the object is exactly the same, and any subsequent changes to the new object will not affect the original target values, that is to say the new object and original object are two separate objects, but the initial value of the new object is determined from the original target.

Clone:

Assignment to create an object:

> Java in the assignment to create objects that can achieve the object reuse, but the new object and original object are the same quote; If you change the value of one of the object, another object may also change.

> Clone using the method returns a copy of the object, so that, if the value of a modified object, another of the objects do not change.

Four, Proxy proxy mode: function on behalf of a class of another class. This type of design pattern belongs structural model.

In proxy mode, we create an object with an existing object to provide a functional interface to the outside world.

Role proxy mode involves: 
1: Abstract theme theme and role agency declares the real theme of the public interface, to make any real need to place the theme of the theme can be replaced with the agency. 

2: Acting topic contains references to the role of the real theme, which can be operated at any time real theme, theme agents and real merits and demerits provide the same interface themes, it can always replace the real theme. theme by reference in the proxy holds true subject, not only can control the creation of a real theme or delete, you can intercept before the real theme is called, or to perform certain operations after the call. 

3: real proxy object defines the specific target agents represented by the role.  

Interception mechanism is one of the important ways to use the agency model, 
in addition to blocking, proxy mode is also commonly used in the resource loading, when we want to load a lot of resources, we can make real theme of the role of load resources in the background, so that the agent responsible for thematic roles the reception is waiting message. 

There is a licensing mechanism, through the ability to intercept the real theme of the proxy to control access to the real topic. 

Link: https: //www.cnblogs.com/YLY9595TYJ/p/10681288.html

Guess you like

Origin www.cnblogs.com/BleachCurtain/p/11029698.html