Simple understanding of design patterns

There is a lot of analysis about design patterns on the Internet, which is not very well understood, so here is a list of my simple understandings, I hope you can correct me!  

  1. Factory mode: Refers to not providing a specific method of creating objects to the outside world, as long as a name parameter is passed in, the object can be obtained, such as Spring's Bean factory, User user = beanFactory.getBean("user"):

  2. Abstract factory pattern: refers to the factory that creates the factory. The bean factory mentioned above is equivalent to its sub-factory. There is also a larger factory that creates these factories. When you get them, you only need to pass in a name.

  Name a factory creator FactoryProducer here, BeanFactory beanFactory = FactoryProducer.getFactory("beanFactory");

  3. Singleton mode: Create a class, this class can only have one instance, and must create its own unique instance (constructor private, internal new), must provide this instance to all other objects (public static xxx getInstance(){}).

  4. Builder pattern: Use multiple simple objects to build a complex object step by step.

  5. Strategy mode: choose the mode of transportation, choose to ride a bicycle or take a car, each mode of travel is a strategy, and these strategies are regarded as objects, the behavior of travel is also an object, and the travel objects can freely choose different travel method object.

  6. Observer pattern: Define a one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated, reducing coupling.

  This is equivalent to the publish-subscribe mode, allowing multiple observer objects to monitor or subscribe to the same topic object at the same time. When the state changes, the topic object will notify all observer objects in the form of broadcast, so that they can automatically update themselves .

  7. Proxy mode: Provide a proxy for other objects to control access to this object. Sometimes when a complex object is directly accessed, a lot of performance is consumed when it is created. At this time, through the proxy object, some control is performed when accessing this object to reduce the complex process.

  8. Decorative Mode: Allows adding new functionality to an existing object without changing its structure. For example, in a rough house, the walls, floor tiles, and furniture are decorated, but the structure of the house is not changed, and the functions of kitchen, bathroom, and bedroom are added.

  This pattern creates a decorated class that wraps the original class and provides additional functionality while maintaining the integrity of the class's method signatures.

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324966167&siteId=291194637