Home  >  Article  >  Java  >  24 design patterns and 7 principles in Java

24 design patterns and 7 principles in Java

高洛峰
高洛峰Original
2016-12-12 13:33:551419browse

1. Creation pattern

1. Abstract factory pattern: Provides an interface for creating a family of related or dependent objects without specifying a specific class.
2. Builder pattern (Builder pattern) : Use the generator pattern to encapsulate the construction process of a product and allow step-by-step construction. Separate the construction of a complex object from its representation so that the same construction process can create different representations.
3. Factory method pattern ): Defines an interface for creating objects, but it is up to the subclass to decide which class to instantiate. The factory method allows the class to defer instantiation to the subclass.
4. Prototype pattern: When creating a given When the instance process of a class is expensive or complex, use the prototype pattern.
5. Singleton pattern: Ensure that a class has only one instance and provide a global access point.
6. Multiple instance pattern (Multition pattern) ): Combine two or more patterns in one solution to solve general or recurring problems.

2. Structural pattern

1. Adapter pattern (Adapter pattern): Convert the interface of a class into Another interface that customers expect. Adapters allow classes with incompatible interfaces to work together seamlessly. Object adapters use composition, and class adapters use multiple inheritance.
2. Bridge pattern (Bridge pattern): Use the bridge pattern by placing implementation and abstraction in Two different class hierarchies so that they can be changed independently.
3. Composite pattern: Allows you to combine objects into a tree structure to represent the "whole/part" hierarchy. Combination allows customers to create a consistent Ways to handle individual objects and combinations of objects.
4. Decorator pattern: Dynamically attach responsibilities to objects. To extend functionality, decorators provide a more flexible alternative than inheritance.
5. Appearance Pattern (facade pattern): Provides a unified interface for accessing a group of interfaces in the subsystem. The facade defines a high-level interface to make the subsystem easier to use.
6. Flyweight Pattern: If you want To allow one instance of a class to provide many "virtual instances", use the fly volume pattern.
7. Proxy pattern (Proxy pattern): Provide a substitute or placeholder for another object to control the access to this object. Visit.

3. Behavioral pattern

1. Chain of responsibility pattern: Through the chain of responsibility pattern, you can create an object chain for a request. Each object checks the request in sequence and responds to it Process it or pass it to the next object in the chain.
2. Command pattern: Enclose "request" into an object to parameterize other objects with different requests, queues or logs. Command pattern also Supports reversible operations.
3. Interpreter pattern (Interpreter pattern): Use the interpreter pattern to create an interpreter for the language.
4. Iterator pattern (iterator pattern): Provides a method to sequentially access each item in an aggregate object element without exposing its internal representation.
5. Mediator pattern: Use the Mediator pattern to centralize complex communication and control methods between related objects.
6. Memento pattern: When When you need to return an object to its previous state (for example, your user requests "undo"), you use the memo pattern.
7. Observer pattern (observer pattern): Define one-to-many dependencies between objects, so that Come, when an object changes state, objects that depend on it will be notified and automatically updated.
8. State pattern (State pattern): allows the object to change its behavior when the internal state changes, and the object will appear to have changed. Its class.
9. Strategy pattern: defines a family of algorithms and seals them separately so that they can be replaced with each other. This pattern makes the changes of the algorithm independent of the customers who use the algorithm.
10. Template method pattern (Template pattern): Define the skeleton of an algorithm in a method, and defer some steps to subclasses. The template method allows subclasses to redefine certain steps in the algorithm without changing the algorithm structure.
11. Visitor pattern: When you want to add new capabilities to a combination of objects, and encapsulation is not important, use the visitor pattern.


---------- -------------------------------------------------- ----------------------------------------

Seven design principles:

1. Single responsibility principle [SINGLE RESPONSIBILITY PRINCIPLE]: One class is responsible for one responsibility.
2. LISKOV SUBSTITUTION PRINCIPLE: The rules of inheritance and derivation.
3. Dependency inversion principle [DEPENDENCE INVERSION PRINCIPLE]: High-level modules should not depend on low-level modules, both should depend on their abstractions; abstractions should not depend on details; details should depend on abstractions. That is, program for the interface, not for the implementation.
4. Interface segregation principle [INTERFACE SEGREGATION PRINCIPLE]: Establish a single interface, do not build a huge and bloated interface, try to refine the interface, and have as few methods as possible in the interface.
5. Dimi Special rule [LOW OF DEMETER]: low coupling, high cohesion.
6. OPEN CLOSE PRINCIPLE: A software entity such as a class, module, and function should be open for extension and closed for modification.
7. Combination/ Aggregation reuse principle [Composition/Aggregation Reuse Principle (CARP)]: Try to use combination and aggregation and use less inheritance relationships to achieve the principle of reuse.


Statement:
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