"Turn" JS design mode begins

Source: Internet
Author: User

(original address: http://blog.chinaunix.net/uid-26672038-id-3904513.html)This article focuses on what design patterns are, and how we learn to design patterns as keystrokes. Is the design pattern really a master key? Each code design pattern almost everyone knows, even if not that also must be in some of the forced Daniel (in part) heard. But few people may know the origin of design patterns:Design Patterns The term is derived from the introduction of Erich Gamma to computer science in the 90 's (hard to imagine how much relevance). It is a solution to some recurring and pervasive problems in software design. So the design pattern is not a piece of code in a language, the design pattern is a kind of idea, the solution that can be adopted to provide the various problems when coding, more inclined to a logical thinking, rather than a universal code block. Design patterns make it easier and easier to reuse some of the more successful designs and architectures, using design patterns to make people understand the whole system's development ideas. four elements of the design pattern pattern name: A number of words to describe the problem, solution, and effect. The pattern name helps us to think, while communicating with other programmers about design ideas. problem (problem): Describes the specifics of using design patterns. Explain the cause and effect of design problems and problems, may describe specific design issues, and may describe classes or object structures that lead to inflexible designs. solution (Solution): Describes the composition of the design, the interrelationships between them and their respective responsibilities and ways of collaborating. The solution does not specifically describe the implementation of a design, but rather provides an abstract description of the design problem and how to solve the problem with some general meaning elements. effect (Consequences): Describes the application of the pattern and the use of patterns need to weigh the problem, although the effect of the pattern is rarely mentioned, but for the evaluation of the pattern in this issue is very important. The pattern effect includes the effect of this pattern on the system's flexibility, extensibility, or portability, which shows that the list of these effects can objectively evaluate the applicability of this pattern to the problem. 23 different design modes Create pattern : Singleton mode, abstract Factory mode, builder mode, Factory mode and prototype mode. Structural mode : Adapter mode, bridge mode, decorator mode, combo mode, appearance mode, enjoy meta-mode and proxy mode. Behavioral Patterns : Template method mode, command mode, iterator mode, observer pattern, Mediator mode, Memo mode, interpreter mode, state mode, strategy mode, responsibility chain mode, and visitor mode. Design Pattern DescriptionAbstract Factory mode (Factory)The customer class is separate from the factory class, making a request to the corresponding factory when the customer needs a product. The disadvantage is that when the product needs to be modified, the factory class also needs to make corresponding changes. Intentions:provides an interface for creating a series of related or interdependent objects without specifying their specific classes. Applicability:a system should be independent of its product creation, when combined. a system needs to be configured by one of several product families. emphasizes the design of a series of related product objects for joint use. for a product library, just want to show the interface instead of the specific implementation. Factory Mode (Factory method)its core factory class is no longer responsible for all the products caught, but the specific work entrusted to the subclass, itself as an abstract factory role, is only responsible for the factory class must implement the interface, and do not need the relationship of the specific product is instantiated details. Intentions:defines a class that is used to create an object interface, allowing subclasses to decide which classes to instantiate and defer instantiation of the class to its subclasses. Applicability:when a class does not know the class of the object it must create. When a class wants to create an object from its subclasses. Builder Mode (creator)The internal representation of an object and the production process of the object are separated, thus making a product object with different internal representations of the construction process. The construction mode allows the internal representation of the product to change independently, and the customer does not need to know the internal details, and can enforce a step-up construction process. Intentions:The construction of a complex object is separated from its performance so that the same form of construction can be represented differently. Applicability:The algorithms for complex objects need to be independent of the parts of the object and the way they are assembled. The construction process allows different object representations to be constructed.     Adapter Mode (Adapter)Changing the interface of a class to the type of interface that the client wants, so that two classes that were previously unable to work because of an interface mismatch could work together. Intentions:modifying an interface to an interface of the desired type, or an interface that is inherently incompatible to be compatible with. Applicability:You want to use a class that already exists, but it doesn't match the interface you need. You want to create a reusable class that works in conjunction with other existing or nonexistent classes. Bridging Mode (bridge)the abstraction and the instance are decoupled so that the two can change independently. That is to reduce the coupling degree. This means that the relationship between abstraction and instantiation uses a combination/aggregation relationship rather than an inheritance relationship, and the two can change independently. Intentions:separate the abstract part from its implementation so that they can change independently. Applicability:you do not want to use a fixed relationship for both the abstract and the implementation parts. both the abstraction of a class and the implementation of a class should all be extensible by generating subclasses. modifications to an abstract implementation section should not have an impact on the customer. you want to share between multiple objects (such as using reference counting), but the customer does not know the point. Responsibility Chain mode (Chain of Responsibility)in the responsibility chain model, many objects are linked by each object's reference to the next. A request is passed on this chain until an object on the chain decides to process the request. For each object on the chain becomes the processor, the processor has two choices: assume the responsibility of handling the request is passed to the other, the request can not be processed by any object. Intentions:enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the requesting sender and the recipient. The objects are linked together and transferred along the chain until an object is processed by them. Applicability:There are objects that can handle a request, specifically which object processing is determined automatically at run time. submits a request to multiple objects in the case of an ambiguous recipient. You can specify that set of objects dynamically.         Command modeThe command pattern encapsulates a request or action into an object. The command mode splits the responsibility for issuing commands and executing commands, delegating to different objects. The command pattern also allows the requester party to be independent of each other, so that the requester does not need to know the receiver's interface. Intentions:encapsulates a request as an object, allowing you to parameterize the customer at different requests. For some queued requests, you can cancel the operation. Applicability:abstract the action to be executed to parameterize an object can use a callback to express this parameterization mechanism. specify, arrange, and execute requests at different times. A command object can have a lifetime that is independent of the initial request. support for Cancel, modify operation. constructs a system with a high-level operation built on the primitive, a transaction encapsulates a set of changes to the data, the command pattern provides a way to model transactions, and a common interface that allows all transactions to be invoked in the same way. It is also easy to add new transactions to extend the system. combination Mode (Composite)Combining patterns between objects is organized into a tree structure that can be used to describe the overall and partial relationships. A combination pattern is a pattern that handles the tree structure of an object. Intentions:combines objects into a tree structure to represent a "partial-whole" hierarchical relationship that makes the user consistent with the use of a single object and a composite object. Applicability:for objects, you want to show a partial-overall hierarchy. you want to use all the objects in a composite structure uniformly. decorator Mode (Decorator)Decorator mode extends object functionality in a transparent manner to the client, an alternative to inheritance that provides more flexibility than inheritance. Dynamically add functionality to objects, which can also be dynamically revoked. Often used for a very large number of functions resulting from the combination of some of the basic functions added. Intentions:dynamically add some additional responsibilities to an object. For added functionality, decorator mode is more flexible than generating subclasses. Applicability:add responsibilities to individual cashing in a dynamic, transparent manner without affecting other objects. When class expansion cannot be done using a similar generation of subclasses. If you have a large number of standalone applications, there will be an order of magnitude explosions if you support various combinations, or the class definition is hidden and cannot be used to generate subclasses.     appearance mode (facade façade mode)the external communication with a subsystem must be done through a unified façade object. Façade mode provides a more advanced interface to make subsystems easier to use. Each subsystem has only one façade mode, and this façade mode has only one instance, that is, singleton mode. There can be a variety of façade classes throughout the system. Intentions:provides a consistent interface for a set of interfaces in a subclass system, and the appearance pattern defines an advanced interface for easier invocation by subclasses. Applicability:when it is necessary to provide a simple interface for a very complex sub-class system. The appearance mode provides a simple default view that is OK for most users, and for advanced users to bypass the façade layer for the required processing.         enjoy meta mode (Flyweight)The enjoy meta mode efficiently supports a large number of fine-grained objects in a shared manner. The key to sharing is to differentiate between intrinsic state and outer state. Intrinsic state storage does not vary with the environment as it is stored within the element. The external state is changed with the change of the environment. The two do not affect each other independently. Distinguish between state and unshared states that can be shared from the regular class. Customers cannot create shared objects directly, they should use a factory object responsible for creating shared objects. The enjoy meta-mode can significantly reduce the number of objects in memory. Intentions:use shared technology to effectively support a large number of fine-grained objects. Applicability:A large number of objects are used in a program. significant storage overhead due to the number of objects. the state of most objects can be changed to an external stateYou can use fewer shared objects for processing. parser Mode (interpreter)given a language, the parser pattern can define the representation of its grammar and provide a parser. The client can use this parser to parse the statements in that language. The parser pattern describes how to parse these statements with a pattern design after having a simple grammar. The language refers to any combination that any parser object can interpret. In parser mode, you need to define a hierarchy of command classes that represent grammars, and each command has a corresponding explanation. Intentions:given a language, a grammar representation is defined and a parser is defined to translate the language. Applicability:the logical processing required to simplify grammar is simple. iterator Mode (Iterator)the iterator pattern can sequentially access elements in a cluster without exposing the internal representation. The aggregation of multiple objects together is called aggregation, and a clustered object is a container containing a set of objects. The iterative pattern encapsulates the iterative logic into a separate object that separates itself from the clustered object itself. Iterative mode simplifies the logical idea of aggregation and separates the behavior independently to adjust the change and replace. So the iterative algorithm can be changed independently of the aggregation object. Intentions:provides a way to sequentially access individual elements in an aggregated object without exposing the object's internal representation. Applicability:Access aggregate objects without the need for internal exposure. support for multiple traversal of aggregation objects. provides a unified interface for traversing various objects.     Broker Mode (mediator)The Mediator pattern wraps a series of objects interacting in such a way that the objects do not have to be clearly related to each other. The coupling becomes loose. When some objects change, it does not affect the role of the other objects, to ensure that the role of each object is independent of each other, this is not between the object and the object, but the interaction between the role and the role of independent Open. The mediator mode transforms many-to-many interactions into one-to-many interactions, abstracts the behavior and collaboration of objects, and separates some of the object's small behaviors from the interaction of other objects. Intentions:The mediator is used to encapsulate the interaction between a series of objects, and does not show the mutual referential relationship between objects, reducing the degree of coupling between them. Applicability:a well-defined set of objects requires a complex way of communicating. resulting in an interdependent relationship but with understanding. an object that communicates directly to many other objects makes it difficult to reuse the object. you need to customize a behavior that is distributed across classes and don't want to generate too many subclasses. Memo Mode (Memento)a Memo object is a snapshot object used to store the internal state of another object, capturing the state of an object without destroying the object's encapsulation, and storing it externally so that it can be viewed at any time so that it can be restored to a certain state in the future. Intentions:captures the internal state of an object without compromising encapsulation and stores the state outside of the object. Applicability:It is important to note that you must maintain a state of the object at some point and not cause encapsulation damage to the object, and do not create redundant exposed interfaces. Observer Mode (Observer)The Observer pattern defines a one-to-many state that allows multiple observers to listen to an object at the same time. When the subject of this object changes, all the observer objects are told, so that they automatically update themselves. Intentions:defines a one-to-many dependency between objects, and when an object changes, all other objects that depend on that object will be notified and automatically updated. Applicability:when an abstract model has two facets, one of which relies on another, encapsulating the two separately in separate objects allows them to change and reuse independently. when an object changes, it needs to change other objects at the same time, and the collection of objects may change. And these objects cannot be assumed to increase the coupling degree. prototype mode (Prototype)by giving a prototype object to indicate the type of object to be created, the prototype object is reused to create more of the same type object. The prototype mode allows for dynamic additions or decreases to the product class, without the need for any hierarchical structure, and the prototype pattern for any structure. Intentions:A prototype instance is used to specify the kind of object creation, and a new object is created by copying the prototypes. Applicability:classes that need to be instantiated are specified at run time. (For the front-end Jser, this is the most familiar)Agent Mode (proxy)The proxy mode provides a proxy object for an object, and the proxy object controls the reference to the object source object. In some cases the customer does not want to live without being able to directly reference a person object, the proxy object in the client and target objects in the role of the mediator, the proxy object can only have a proxy interface, when the proxy object cannot create the object being proxied, the object of the agent must be created and passed on behalf of the other role of the system. Intentions:provides a proxy for other objects to control access or processing of a particular object. Applicability:when you need to use the comparison love universal and complex object pointers instead of simple pointers. Singleton mode (Singleton)The singleton pattern ensures that each class has only one instance, and that it instantiates itself and wants the entire system to provide the interface for that instance. Singleton mode is only used when a single mode is really needed. structure:ensure that a class has only one instance and provides a global access point to access it. Applicability:when a class can have only one instance, the client accesses it through a canonical access point. status Mode (state)state mode allows an object to change behavior when its internal state changes. Makes it look the same as changing the class. State mode wraps the behavior of the object being studied in different objects, each of which belongs to a subclass of an abstract state class. The state mode requires that a subclass of the State class be created for each state that the system may acquire. When the state of the system changes, the system changes the selected subclass. Intentions:allows an object to change behavior when its internal state changes. Applicability:the behavior of an object depends on its state, and the behavior must be changed according to state at run time. policy mode (strategy)The policy pattern is for a set of algorithms that encapsulate each algorithm in a separate class with a common interface so that they can be replaced with each other. The policy pattern allows the algorithm to change without affecting the client. Separate the behavior from the environment. The Environment class is responsible for maintaining and querying the behavior classes, each of which is provided in a specific policy class. Therefore, the increase or decrease of the algorithm will not affect the environment and clients. Intentions:define a series of algorithms for individual encapsulation, and they can be replaced by each other, making the algorithm independent of the customer changes. Applicability:many of the related classes, just the behavioral differences, the policy pattern can be implemented in multiple behaviors to choose one to configure a class. Template Method ModeThe template method pattern prepares an abstract class, implements some of the logic and concrete methods to construct subclasses, and then declares some abstract methods relationship serve the remaining logic of the subclass. Different subclasses can implement these abstract methods in different ways, thus implementing the rest of the logic. First, a top-level logical framework is customized, and the details of the logic are Liu Yan to specific subclasses. Intentions:define an algorithm skeleton and defer steps to subclasses so that subclasses can redefine the algorithm without altering an algorithm structure. Applicability:One -time implementation of the invariant part of the algorithm, the variable part is left in the subclass implementation. A class defines a variety of behaviors, and the actions of those behaviors in this class occur in the form of multiple conditional statements.     Visitor Mode (Visitor)The purpose of the visitor pattern is to encapsulate some operations that are applied to a data structure element. Once the operation needs to be modified, the data structure that accepts the set operation can remain unchanged. It is suitable for the system with relatively uncertain data structure, and freeing the data structure and operation coupling on the structure so that the operation can evolve relatively freely. The visitor pattern makes it easier to add new operations. The visitor pattern concentrates the behavior on a visitor object, rather than being scattered across nodes. When using the visitor pattern, you put as much of the object navigation logic in the visitor class, not in the subclass. Intentions:represents an operation that acts on elements in an object structure, allowing you to define new actions that act on these elements without changing the classes of individual elements. Applicability:An object structure contains multiple class objects, with different interfaces, and you want to implement some operations that depend on a specific class for those objects.         requires a lot of different and unwanted operations on objects in an object structure that need to avoid polluting objects. The visitor pattern can put related operations into a class and be shared by the object structure, so that each sub-object in the object structure can be shared.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.