24 Design Patterns and 7 principles in Java

Source: Internet
Author: User

First, the creation mode
    1. Abstract Factory Pattern: Provides an interface for creating a family of related or dependent objects without specifying a specific class.
    2. Builder pattern: Uses the builder pattern to encapsulate the construction of a product and allows for construction by step. Separating the construction of a complex object from its representation allows the same build process to create different representations.
    3. Factory mode (factory method pattern): Defines an interface for creating objects, but subclasses decide which class to instantiate. The factory method defers the instantiation of the class to the subclass.
    4. Prototype mode (prototype pattern): Use prototype mode when creating an instance of a given class is expensive or complex.
    5. Singleton mode (Singleton pattern): Ensures that a class has only one instance and provides a global access point.
    6. Multition pattern: Combine two or more patterns in a solution to solve a common or recurring problem.
Second, structural type mode
    1. Adapter mode (Adapter pattern): Transforms the interface of a class into another interface that the customer expects. Adapters allow classes that are incompatible with the original interface to work together. Object adapters use combinations, and class adapters use multiple inheritance.
    2. Bridging mode (bridge pattern): Use bridging mode to change the implementation and abstraction independently by placing them in two different class hierarchies.
    3. Combined mode (composite pattern): Allows you to combine objects into a tree structure to represent a "whole/part" hierarchy. The combination enables the customer to handle individual objects and object combinations in a consistent manner.
    4. Decorator mode (decorator pattern): to dynamically attach responsibility to an object, the adorner provides a more resilient alternative than inheritance to extend functionality.
    5. Appearance mode (facade pattern): Provides a unified interface for accessing a group of interfaces in a subsystem. The appearance defines a high-level interface that makes the subsystem easier to use.
    6. Hengyuan mode (Flyweight pattern): If you want an instance of a class to be used to provide many "virtual instances," Use the fly volume pattern.
    7. Proxy pattern: Provides an alias or placeholder for another object to control access to the object.
Iii. Behavioral Patterns
    1. Responsibility chain mode (Chain of responsibility pattern): You can create an object chain for a request through the chain of responsibility model. Each object sequentially examines the request and processes it, or passes it to the next object in the chain.
    2. Command pattern: The request is closed to objects so that different requests, queues, or logs are used to parameterize other objects. The command mode also supports the revocable operation.
    3. Interpreter mode (interpreter pattern): Creates an interpreter for the language using the interpreter pattern.
    4. Iterator pattern (iterator pattern): Provides a way to sequentially access individual elements in an aggregated object without exposing their internal representations.
    5. Mediator mode (mediator pattern): uses the mediator pattern to centralize the complex communication and control patterns between related objects.
    6. Memo Mode (Memento pattern): You use Memo mode when you need to return an object to its previous state (for example, if your user requests "undo").
    7. Observer pattern (Observer pattern): Defines a one-to-many dependency between objects, so that when an object changes state, objects that depend on it are notified and updated automatically.
    8. State pattern: Allows an object to change its behavior when the internal state changes, and the object looks as if it had changed its class.
    9. Policy mode (strategy pattern): Defines the algorithm family, respectively closed up, so that they can be replaced with each other, this pattern allows the algorithm to change independently of the customers using the algorithm.
    10. Template method Pattern: Defines the skeleton of an algorithm in a method, and delays some steps into subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm.
    11. Visitor Mode (visitor pattern): Use the visitor pattern when you want to add new capabilities to a combination of objects, and the encapsulation doesn't matter.
Seven major design principles
    1. Single RESPONSIBILITY PRINCIPLE: one class is responsible for one responsibility.
    2. Richter replacement principle "LISKOV SUBSTITUTION PRINCIPLE": Inherited and derived rules.
    3. Dependency inversion principle "dependence inversion PRINCIPLE": The high-level module should not rely on the lower layer module, both should rely on its abstraction; abstraction should not depend on detail; detail should depend on abstraction. That is, for interface programming, do not program for implementation.
    4. Interface Isolation principle "INTERFACE segregation PRINCIPLE": Set up a single interface, do not set up a huge bloated interface, as far as possible to refine the interface, the interface of the method as little as possible.
    5. The Dimitri law "low of DEMETER": Cohesion Poly.
    6. Open Close PRINCIPLE: A software entity such as classes, modules, and functions should be open to extensions, closed for modifications, and add functionality to the program by adding the implementation class instead of modifying the original code
    7. Combination/Aggregation multiplexing principle "composition/aggregation reuse Principle (CARP)": Use combinations as much as possible, and use less inherited relationships to achieve the principle of reuse

24 Design Patterns and 7 principles in Java

Related Article

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.