1. Adapter mode
a) Structure:
Class adaptation
Object adaptation
Note: 1) Convert a class interface into another interface that customers want, so that those classes that are not compatible without compatibility can work together.
2)
ADA
The PTER belongs to an intermediate class that is responsible for coordinating the call forwarding between the Client and Target.
3) Since Java only supports single inheritance, the inheritance from Adapter to Target does not appear in Java.
b) Advantages:
i. It is easy to operate the original (Adaptee) to new applications.
c) Use the environment:
i. You want to use an existing class, and her interface does not meet your requirements.
II. You want to create a class that can be multiplexed, which can work with other unrelated classes or unforeseen classes.
Iii. You want to use some already existing subclasses, but it is impossible to make subcatetics for each one to match their interface. The object adapter can adapt her parent interface.
d) Code instance:
i. Class adaptation:
Public class target {
Public void request () {...}
}
Public class
ADA
PTEE {
Public void requestb () {...}
}
Public class
ADA
Pter EXTENDS Adaptee {
Public void request () {
Super.Requestb ();
}
}
Ii. Object adaptation:
Public class target {
Public void request () {...}
}
Public class
ADA
PTEE {
Public void requestb () {...}
}
Public class
ADA
Pter Extends Target {
Private
ADA
Ptee adaptee = new
ADA
PTEE ();
Public void request () {
Adaptee.Requestb ();
}
}
e) Summary:
i. Used to convert an interface to another.
II. There are three class Targets, Adapter, Adaptee in Adapter mode.
ADA
PTER To inherit the Target is a structure considered, thereby use Adapter in the call of Target. Adapter wants Adaptee to function as a function of re-use the functionality of Adaptee.
III. If the AWT application is to use a new class in Swing, you can provide a set of adapters from Swing to AWT.
2. Bridge mode
a) Structure:
Note: 1) Separate the abstraction to his implementation, so that they can change independently.
2) For Client, the implementation details of the Implementor section are hidden.
b) Advantages:
i. Reduce the degree of coupling between the parts and hide the implementation details. It is easy to update each part.
c) Use the environment:
i. You don't want to have a fixed binding relationship between abstract and her implementation. For example, this may be because the implementation portion can be selected or switched at the time of operation. Ii. Abstraction of class and his implementation should be expanded by the method of generating subclasses. At this time, the Bridge mode allows you to combine different abstract interfaces and implementations and expand them separately.
III. Modifications to an abstract implementation should have no impact on customers.
Iv. You want to completely hide the abstract implementation part of the customer.
v. There are many classes that need to be generated, such a class hierarchy indicates that you must decompose an object into two parts.
Vi. You want to share the interaction between multiple objects (you can use the reference count), but at the same time as you don't know this.
d) Code instance:
Public class abstract {
PRIVATE IMPLEMENTOR IMP = New Implementora ();
Public void Operation () {
Imp.operation ();
}
}
Public interface implementor {
Public void Operation ();
}
Public Class Implementora IMPLEments IMPLEMENTOR {
Public void Operation () {...}
}
Public Class Implementorb IMPLEments IMPLEMENTOR {
Public void Operation () {...}
}
e) Summary:
i. It is used to separate the client and implementation classes, making two parts to make separate modifications.
II. Modifications to Abstraction can increase the functionality available to the client.
Iii. Modifications to Implementor can increase the operation available for Abstract.
IV. Modifications and customizations for Abstraction and Implementor are independent and transparent to Client.
v. In Java's database access, the function implementation is divided into two parts, and a part is an interface for the supplier call, and the other is the SPI responsible for the functionality.
3. Composite mode
a) Structure:
Note: 1) Combine the object into a tree structure to indicate the hierarchy of "overall-part".
2) Make the operation of a single object and consistency with the operation of the combined object.
b) Advantages:
i. Consens on the node and the processes of the leaves (Leaf), making the customer's operation simple. (Processable to Leaf and Composite can use the same method)
Ii. You can add new types very simple, but it may also cause the system to be too general, and some specific restrictions cannot be added. (Operation is defined in Component, the subclass can only modify the implementation of the method, but the specific method cannot be added)
c) Use the environment:
i. You want to indicate the "overall-part" hierarchy of the object.
Ii. You want users to ignore the different combination objects to different objects.
d) Code instance:
Public Abstract Class Component {
Public Abstract void Operation (); public void add (component child) {...}
Public void remove (Component Child) {...}
Public Component getChild (int index) {...}
}
Public class leaf extends component {
Public void Operation () {...}
}
Public void composite extends component {
Public void Operation () {...}
}
e) Summary:
i. It is a collection of objects, and every object is either a Composite, or it is a basic class.
II. When implementing the Composite mode, consider the following questions:
1) This mode does not record the information of the parent node in the child node, and can be considered.
2) Sharing components: that is, a component belongs to multiple parent components, and you need to introduce Flyweight mode.
3) Maximize the Component interface: Define the panegroup of a Leaf and Composite feature.
4) Declaring management sub-component operation: Increased transparency in the root declaration of the child, can increase transparency, but will cause security hazards, such as executing nodes in Leaf. Declaring the dosing operation in the child node to ensure the security of the LEAF node, but transparency is lost.
5) Node deletion: For shared sub-nodes, it is necessary to treat it.
III. The Node class used in JTREE in Java, the Node class used in the DOM is the application of the Composite mode.
4. Decorator
a) Structure:
Note: 1) Dynamically add additional responsibilities to an object.
2) Not achieved by inheritance.
3) Only for a certain type of instance instead of adding the entire class.
b) Advantages:
i. Multiple Decorator can be nested.
II. Do not need to be inherited, and add additional functions to class instances. (But there may be a lot of extra small classes)
c) Use the environment:
i. Add responsibilities to a single object in a dynamic, transparent manner without affecting other objects. (Because not through inheritance, etc.)
II. Handling those responsibilities that can be revoked. (Since the responsibility is only dynamically added to the object instance,, when this function is not required, as long as it does not increase the modification)
Iii. When the method of generating subclasses cannot be expanded. One situation is that there may be a large number of independent extensions, to support each combination will result in a large number of subclasses, so that the number of subclasses is explosive (for example, if there are sub-function A, B, and C different combination There are 3 6 6). Another situation may be because class definitions are hidden, or class definitions cannot be used to generate a subclass. (Class is defined as final)
d) Code instance:
Public class component {
Public void Operation () {...}
}
Public class componenta eXtends component {
Public void Operation () {...}}
Public class decorator Extends Component {
PRIVATECIENT COMPONENT
Public void Operation () {
Component.operation ();
}
}
Public class decoratorb extends decorator {
Public void Operation () {
Super.operation ();
AddedOperation ();
}
Private void addedopance () {...}
}
e) Summary:
i. Surrounded by a given class, add additional functions for a given class, and for those who do not need to change, they are still completed by a given class.
Ii. Decorator changes the responsibility of the object, Adapter changes the interface of the object.
Iii. Decorator changes the appearance of the object, and Strategy changes the kernel of the object.
IV. Decorator can be seen as a mixed, only one component's Composite. However, Decorator is to add some additional functions to the object, and Composite is an object aggregation.
v. Java's IO packets and Border packages in Swing are a specific application of Decorator.
5. FACADE mode
a) Structure:
Note: 1) Provide a consistent interface for a set of interfaces in the subsystem. The FACADE mode defines a high-level interface that makes this subsystem easier to use.
b) Advantages:
i. Shielding the complexity of the bottom subsystem, give the user a relatively simple call interface.
II. The user code and the underlying implementation are separated, which reduces coupling.
III. Reduces the number of communication between the client and the subsystem.
c) Use the environment:
i. When a simple interface is provided for a complex subsystem. The FACADE mode provides a simple default view, which is enough for most users, and those who need more customizable users can cross the FACADE layer.
II. There is a lot of dependence between the client program and the abstract class's implementation, by introducing FACADE, the subsystem can be separated from the client, reducing coupling, improving independence and transplantability.
Iii. When you need to build a hierarchy subsystem, use the FACADE mode to define the entry points of each layer in the subsystem. If the subsystem is interdependent, you can make them only communicate with Facade, which simplifies the dependencies between them.
d) Code instance:
Public class facade {
Private subsystema suba = new subsystema ();
Private subsystemb subb = new subsystemb ();
Public void Operation () {
Suba.DOSMETHING ();
Subb.doanotherthing ();
}
}
Public class subsystema {
Public void dosomething () {}
}
Public class subsystemb {
Public void doanotherth "() {}
}
e) Summary:
i. Combine a complex object structure and form a new, simplified interface. Ii. Facade can be implemented with an abstract class, and his specific subclass corresponds to different subsystems, which further reduces coupling between customers and subsystems. (Replacement of the entire subsystem)
III. In J2EE, the FACADE mode is used instead of access to multiple session beans.
6. Flyweight mode
a) Structure:
Note: 1) Use shared technology to support a large number of fine-grained objects.
2) The object being shared may be a sigleton.
3) The shared attribute is saved inside the class, and the individual properties are placed in an array or vector of the class.
b) Advantages:
i. Reduce the required instance.
c) Use the environment:
i. An application uses a lot of objects.
Ii. Due to the use of a large number of objects, it causes great storage overhead. (By sharing instance, reduce storage overhead)
Iii. Most of the status of objects can be variable.
d) Code instance:
Public class flyweightfactory {
Private flyweight [] fly;
Public Flyweight GetFlyWeight (int key) {
IF (fly [key]! = null) {
Return fly [key];
} else {
Fly [key] = new flyweighta ();
Return fly [key];
}
}
}
Public class flyweight {
Public void Operation () {...}
}
Public Class Flyweighta Extends Flyweight {
Public void Operation () {...}
}
e) Summary:
I. Moves some of the data to the class outside and to provide a method to limit these data to the interior of the class to limit the generation of some small, similar clarism. (A bit similar to Prototype mode)
Ii. Flyweight does not make any assumptions he run, similar to the Stateless Session Bean in EJB.
Iii. Flyweight object must be shared, the status she stores must be internal, that is, she must be independent of the scene of the Flyweight object. Therefore, it is generally managed by Flyweight Pool.
IV. You can save storage: use the sharing to reduce the internal state consumption, use the calculation time to exchange the storage of the external state.
v. Sharing also means some form of reference counting and garbage recovery.
7. PROXY mode
a) Structure:
Note: 1) Provide a proxy for other objects to control access to this object.
2) Proxy has an uniform interface with the object, and she intervenes in the request of the agent object, performs some additional operations.
b) Advantages:
i. Shorten the reaction time. (You can start a thread in the proxy, make the method call to return immediately)
Ii. A certain permission management can be performed. (All access to RealSubject, you need to filter first in PROXY) C) Use Environment:
i. Remote Agent: Provides local representatives for an object in different address spaces. (Similar to Stub in RMI)
Ii. virtual proxy: Create a large number of overheads that are overhead as needed. (Delay the initialization moment of the large object, temporarily representing the actual object by the virtual agent)
III. Protection Agent: Controls access to the original object, the protection agent is used for objects that should have different access rights.
Iv. Intelligent Agent: Replains simple pointers, and she performs some additional operations when visiting the object. Her typical use includes counting the reference to the actual object, so that she can automatically release her when the object is not referenced.
v. When you first reference a persistent object, put him into memory. (Delayed persistent object loading time)
Vi. Check that she has locked her before visiting an actual object to make sure that other objects cannot change her. (Since access to RealSubject needs to pass Proxy, you can lock it in Proxy or Synchronized, etc.)
The Java uses this mode when processing the picture. (Virtual agent)
d) Code instance:
Public interface subject {
Public void request ();
}
Public Class RealSubject Implements SUBJECT {
Public void request () {}
}
Public Class Proxy IMPLEments SUBJECT {
PRIVATEVID RealSubject Real
Public void request () {
Real.Request ();
}
}
e) Summary:
i. For a complex, to instantiate very expensive classes to provide a simple placement class.
6. Structural mode
a)
ADA
The PTER is mainly to solve the mismatch between two existing interfaces without considering the implementation and variation of these interfaces.
b) Bridge separates the abstract interface with her implementation part.
i.
ADA
The purpose of the PTER is to avoid code repetition, after the class design is good.
Ii. The purpose of Bridge is to reduce coupling, which occurs before the class design.
c) Decorator aims to add additional duties to the object without subclass.
d) Composite is intended to construct classes, allowing multiple associated objects to be handled in a unified manner, her focus is not being modified, but is represented.
e) Proxy mode cannot be dynamically added or separated, and he is not designed for recursive combination, and its purpose is to provide an alternative to this entity when directly accessing an entity is inconvenient or does not meet the needs.