Where to use design patterns

1. Creation mode (Creational):
simple factory mode (simpleFactory) pronunciation: ['simpl] [' f? Kt (?) R?]
Definition: to
provide a function to create an object instance without having to care about its specific implementation. The type of creating an instance can be an interface, an
abstract class, or a concrete class.
1. Abstract Factory (AbstractFactory) Pronunciation: ['? Bstr? Kt]
Definition:
provides an interface to create a series of related or interdependent objects, and There is no need to specify their specific classes.
Use occasions:
1. If you want a system to be independent of its product creation, combination, and presentation, in other words, hope that a system
only knows the interface of the product, and does not care about the implementation time .
2. If, when a system is to be made in a number of product lines to configure other words, is that we can, that we can
time dynamically switch cluster product.
3. If the stressed interfaces series of related products, in order to jointly When using them
2. Builder mode (Builder) Pronunciation: ['b? Ld?]
Definition:
Separate the construction of complex objects from its representation, so that the same construction process can create different representations.
Use occasions:
1. If you create a pair Algorithm should be independent of the time part of the object and their manner of mounting
2. If the same build process with different representation time
3. factory method model (Factory Method)
defined:
Define an interface for creating an object, and let the subclass decide which class to instantiate. The factory method delays the instantiation of a class to
the subclass.
Usage scenarios:
1. The client class does not care which specific class to use, only the interface provides function.
2. create a more complex process, such as the need to initialize other associated resource class, reads the configuration files, etc.
3. when there are a lot of specific interfaces or abstract class has achieved a lot of concrete subclasses,
4. do not want to give clients exposure Too many such internal structures, hiding these details can reduce the degree of coupling.
5. Optimize performance, such as caching large objects or initialization time-consuming objects.
4. Prototype Method (Prototype Method) Pronunciation: ['pr ?? t ? ta? p]
Definition:
Use the prototype instance to specify the type of object to be created, and create a new object by copying this instance.
Applications:
1. If a system wants to be independent of the object it wants to use, you can use the prototype mode , So that the system is only for interface
programming. When the system needs new objects, it can be obtained by cloning the prototype.
2. If the class that needs to be instantiated is dynamically specified at runtime, it can be used Prototype model, by cloning the prototype comes
into desired instance.
5. singleton pattern (the Singleton) pronunciation: [ 's g lt n? ? (?) (?)]
Definition:
to ensure only one instance of a class, and provide a Access its global access point.
Use cases:
When there is only one instance of a class that needs to be controlled, and the client can only access it from a global access point, you can use
With singleton mode, these functions happen to be the problem to be solved by singleton mode.
Two. Structural mode (struct) pronunciation: [str? Kt]
6. Adapter mode (Adapter) pronunciation: [? 'D? Pt?]
Definition :
Convert the interface of a class to another interface that the customer wants. The adapter mode allows
those classes that could not work together because of incompatible interfaces to work together.
Use occasions;
1. If you want to use an existing class first, but its interface does not meet your needs, you can use this case the appropriate
adapter mode, to convert existing implementation to the interface you need.
2. If you want to create a class that can be reused, and some of this class may not work with compatible class, which in the case can be
to use an adapter mode, when what needs to fit anything.
3. If you want to use some of the subclasses have been channeling is not pit for each sub-class adaptation this can in the case of
the adapter mode, a direct adaptation of these subclasses of the parent class on it.
7. bridge mode (bridge) pronunciation: [br d??]
Definition:
the abstraction from its implementation portion, so that they can vary independently.
use field Combination:
1. If you do not want to use a fixed binding relationship between the abstract part and the implementation part, you can use the bridge mode.
2. If there is a situation where both the abstract part and the implementation part can be extended, you can use the bridge mode to make the abstract part and real
now partially varied independently.
3. If you want to achieve the modified portion does not affect the customer, may be employed bridging mode.
4. If the inherited implementation will result in many sub-categories, in this case, could be considered a bridge mode
type.
8. The composition mode (Composite) pronunciation: [ 'k mp z t? ??]
Definition:
The Objects are combined into an attribute structure to represent a "part-whole" hierarchy, and users in the combined mode
have consistent
use of single objects and combined objects. Use scenarios:
1. If you want to represent part-whole hierarchies of objects, You can use ... and the whole part of the operation to unify, so
was the hierarchy to achieve easier to use from the outside, this hierarchy is easy.
2. If you want to agree to use all the objects in the composite structure, you can use ..., it It is the main
function provided by the combination mode .
9. Decorator Method (Decorator Method) Pronunciation: ['dek? Re? T?]
Definition:
Dynamically add some additional responsibilities to an object. In terms of adding functions, the decoration mode subclass generate more flexible.
use:
1. If you need to love the case does not affect other objects, dynamic, transparent way to add responsibilities to objects, you can use the installed
decorative pattern.
2. If you are not suitable to use subclasses Show, they can consider using the decorative pattern.
10. Appearance mode (the Facade) pronunciation: [f 'sɑ:? D ]
Definition:
providing a consistent interface subsystem is a set of interfaces, the Facade pattern defines a top Interface, this interface
port makes the subsystem more with easy to use.
usage scenarios:
1. If you want to provide a simple interface for a complex subsystem, you can consider using the appearance mode. Use
appearance objects to achieve the functions required by most customers, thereby simplifying the use of customers.
2. If you want to make the customer program and The implementation of the abstract class is partially loosely coupled. You can consider using the appearance mode and using the appearance
object to separate this subsystem from his customers, thereby improving the independence and portability of the subsystem.
3. If you build a multi-layer section For the system, you can consider using the appearance mode to use the appearance mode object as the entrance of each layer,
which can simplify the call between the layers, and can also loose the dependencies between the layers.
11. Flyweight mode (Flyweight) pronunciation: ['fla? We? t]
Definition:
Using sharing technology to effectively support a large number of fine-grained objects.
Use cases:
1. If an application uses a large number of fine-grained objects, you can use the flyweight model to reduce the number of objects.
2. If you hesitate Using a large number of objects causes a lot of storage overhead. You can use the flyweight mode to reduce the number of objects and
save memory.
3. If most of the state of the object is Can be transformed into external state, such as obtained through calculation, or passed in from outside, etc.,
can be used to achieve the separation of internal state and external state.
4. If you do not consider the external state of the object, you can use relatively less sharing Objects replace many combined objects, you can use the
Flyweight pattern to share objects. Then combine objects to use these shared objects.
12. Proxy mode (Proxy) pronunciation: ['pr? Ks?]
Definition:
Provide a proxy for other objects to Control access to this object.
Use occasions:
1. When you need to provide a local representative for an object in different address spaces, you can use a remote proxy.
2. When you need to create a very expensive object as needed, you can use a virtual proxy.
3. You need to control the original When accessing the object, you can use the protection proxy.
4. When you need to perform some additional operations when accessing your object, you can use the intelligent guidance proxy.
3. Behavioral mode (behavioral) pronunciation [bi'heivj? R? L]
13 .Chain Of Responsibility pronunciation: [t? E? N] [r?, Sp? Ns? 'B? L? T?]
Definition:
make multiple objects have the opportunity to process the request, so as to avoid the request The coupling relationship between the sender and the receiver.
Connect these objects into a chain and pass the request along the chain until one object processes it.
Use cases:
1. If there are multiple objects that can handle the same The request, but the specific object to process the request is
dynamically determined at runtime .
2. If you want to submit a request to one of multiple objects without specifying the receiver, you can
use the chain of responsibility Mode.
3. If you want To dynamically specify the combination of objects that process a request, you can use the chain of responsibility mode.
14. Command mode (Command) pronunciation: [k? 'Mɑ? Nd]
Definition:
encapsulate a request as an object, so that you can use different requests parameterize the customer, a request queue or
who log requests, and support undoable operations.
15. Interpreter pronunciation: [? N't?? Pr? T?]
Definition:
Given a language, define a representation of its grammar, and define an interpreter, which uses this representation To
explain the sentences in the language.
Use cases:
16. Iterator pattern
definition: It provides a method to sequentially access each element in an aggregate object without exposing the internal
representation of the object .
Use cases:
1. If you want to provide access to the content of an aggregate object, but do not want to expose his internal representation, you can
use the iterator pattern to provide an iterator interface, so that the client only accesses the aggregate object through the iterator interface
without having to Concern about the internal implementation of aggregate objects.
2. If you want to have multiple traversal methods to access aggregate objects, you can use ...
3. If you want to provide a unified interface for traversing different aggregate objects, you can use ...
17. Intermediary mode ( Mediator) pronunciation: [ 'mi: dieit?]
Definition:
the package with a series of object interaction mediation object class of each object such intermediary need to explicitly refer to each other, from
the coupling so Loose, and can independently change the interaction between them.
Use occasions:
1. If the communication between a group of objects is more complicated, resulting in interdependence and chaotic structure, you can use the intermediary model
to manage the interaction between these objects. , and each object need only mediator interaction to the various objects are loosely coupled
together, the structure is more clearly understandable.
2. If an object refers to many objects and directly interacts with these objects, which makes it difficult to reuse the object, you can use the
intermediary model to encapsulate the interaction of this object with other objects into the intermediary object. Intermediary
object interaction is enough.
18. Memo mode (Memento) Pronunciation: [m? 'Ment ??]
Without destroying the encapsulation premise, capture the internal state of an object and save the state outside the object this
kind of application can then restore the object to a previously saved state.
use:
1. If an object has to save all or part of a certain state in time, when needed to facilitate future, can be
restored to the to the object The previous state, you can use the memo mode.
2. If you need to save the internal state of an object, but if you use an interface to let other objects get these
saved states directly , it will expose the implementation of the object Seagate and destroy the encapsulation of the object. it is possible to use the memo.
19. the observer pattern (observer) pronunciation: [b'z v???? ]
definition:
dependencies between the definition of when a subject an-many. When the state of the object changes, all depending on the
object are notified and updated automatically.
Occasions;
1. When an abstract model has two aspects, one aspect of which depends on the operation state change of another aspect, Then
you can use the observer mode to encapsulate the two into the observer and the target object. When the target object changes,
the observer object that depends on it will also change accordingly. This will put the two of the abstract model The separation of aspects allows them to be
changed and reused independently.
2. If you need to change other objects at the same time when changing an object, and you don't know how many
objects should be changed together, in this case, you can use the observer mode. The object that is changed is obviously equivalent to head
-standard objects, but need to modify the joint singing and other objects, watching on a number of objects as a.
3. when an object must notify other objects, but you also want this object and other objects to be notified that it is loose
loose Coupled, that is to say, this object does not actually know the specific notified object. In this case,
the observer mode can be selected, this object is equivalent to the target object, and the object notified by it is the observer object.
20. State mode (State) pronunciation: [ste? T]
Allowing an object to change its internal state is to change its behavior. The object seems to modify its class.
Use occasions:
1. If the behavior of an object depends on its state, and it must change its line at run time according to the state
to be used ... to state and behavior of packet separated, although separated, but the state and behavior is Correspondence relationship, may be
during operation by changing the state, it is possible to call the processing target state corresponding to the state to change up the line object
is.
2. If an operation contained in a large multi-branched statements, and these depend on branches state of the object can be made
using the process ... each branch is dispersed into separate packaging process object classes, so that these objects can be branch corresponds not
depend on other objects independently changed.
21. the strategy pattern (strategy) Pronunciation: ['str? T? D??]
Definition:
Define a series of algorithms, encapsulate them one by one, and make them replaceable with each other. This mode makes the algorithm
can be changed independently of the customers who use it.
Use occasions;
1. There are many related classes, only behaviors In the case of differences, you can use the strategy mode to use one of multiple behaviors
to configure a class method to achieve dynamic switching of the
algorithm. 2. When the same algorithm appears and there are many different implementations, you can use the strategy mode to change these. different implementations "real
is now a class hierarchy algorithm.
3. the algorithm requires packaging, there is the case where data related algorithms may be used to avoid exposure mode strategies with these operators
associated data structure method.
4. When a abstract defines a class of many acts, and is formed by a plurality of if-else statement to select the case of these behaviors
under the condition, these may be replaced conditional statements policy model.
22. a method of pattern template (template method) pronunciation: [ 'temple? t; -pl t]?
definitions:
definition of the algorithm operation in one frame, the number of delayed steps to the sub-class to implement a stencil mode method allows subclasses need.
to change the algorithm Re-configuration algorithm steps define specific
functions:
1. to solve the problem of redundant code
2. Some algorithms step delay to subclasses
3. easily extended
4. Providing parent algorithm framework, the execution flow control algorithm, The subclass cannot change the flow of the algorithm, and the method
call of the subclass is determined by the template method of the
parent class . 5. The parent class can shield those important methods that are not allowed to be changed, and do not allow the subclass to overwrite them.
1. It is necessary to define the algorithm skeleton fixedly, implement the unchanged part of an algorithm, and leave the variable behavior to the subclass to achieve
the situation.
2. Each subclass has public behavior, which should be extracted and concentrated in a public Implement in the class to avoid
duplication of complex code
. 3. The need to control the expansion of the subclass. The template method mode will call the methods of the subclass at specific points, so that only
extensions are allowed at these points.
Knowledge:
callback: means A piece of executable logic reference (or pointer), we pass the reference (or pointer) to another piece of
logic (or method) for this logic to call in time
(site: redhat.iteye.com) 23.Visitor
mode ( Visitor) pronunciation: [??? 'v z t]
definition:
an operation, an object structure of the role each element that makes you can not change the type of each element before.
mention define act on these elements new operation.
use:
1. If an object structure embodiment want to rely on the operation of some specific classes in the object structure, the visitor can use mode
. formula
2. If you want the object to a structure of each of many different elements and Related operations, in order to avoid that
these operations the class becomes cluttered, you can use the visitor pattern.
3. If the object structure rarely change, but often need to object structure elements define a new operation, you can use the visit
Inquirer mode .
3. If the object structure rarely change, but often need to object structure elements define a new operation, you can use the visit
Inquirer mode.

Guess you like

Origin www.cnblogs.com/dfs23/p/12709940.html