Home > Web Front-end > JS Tutorial > body text

Summary of the 12 most commonly used design patterns_javascript skills

WBOY
Release: 2016-05-16 18:03:23
Original
1183 people have browsed it

1. Strategy pattern (Strategy): Defines a family of algorithms and encapsulates them separately so that they can be replaced with each other.
For example, Collections.sort(List list, Comparator c); You can implement multiple Comparator interfaces to achieve multiple The purpose of sorting.
2. Decorator: Dynamically add some additional responsibilities to an object.
For example, the java.io package. BufferedInputStream encapsulates FileInputStream, and they all implement the InputStream interface, but The former implements the readLine method.
3. Proxy mode (Proxy): Provides a proxy for other objects to control access to this object.
For example, when a user logs in, both the real login class and the proxy login class The Login interface is implemented. The difference is that the method of the Proxy class adds a judgment of whether the user is legal. Only when the user is legal, the login method of the real login class is called. What the user accesses is actually the login method of the Proxy.
4. Factory Pattern (Factory): Define an interface for creating objects, and let subclasses decide which class to instantiate.
When encountering the need to create different class implementations based on certain preconditions, the factory pattern will be used.
5. Template mode (Template): Define the algorithm skeleton in an operation, and defer some steps to subclasses.
For example, HibernateTemplate, the implementation of the Connection switch has been defined in Template, and the user only needs to add it in the subclass Write different sql according to different businesses.
6. Facade: Provide a consistent interface for a set of interfaces in the subsystem.
I have been using it, such as DBUtil, to encapsulate all database objects , only the interface DBUtil.getDBUtil() is left.
7. Builder pattern (Builder): Separates the construction of a complex object from its representation.
8. Observer pattern (Observer): Defined A one-to-many dependency relationship that allows multiple observer objects to monitor a certain topic object at the same time. When its status changes, all observers will be notified.
For example, ServletContextListener will notify when applcation is started. All implementation classes of this interface.
9. Abstract Factory pattern (Abstract Factory): Provides an interface for creating a series of related or interdependent objects without specifying their specific classes.
10. Adapter pattern (Adapter ): Convert the interface of a class into another interface that the customer wants.
11. Singleton mode (Singleton): Ensure that a class has only one instance and provide a global control point to access it.
For example This mode can be used when loading the configuration file.
12. Command mode (Command): Encapsulate a request into an object so that customers can be parameterized with different requests.
For example, the MVC structure of Struts , is actually a Command mode.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template