Design mode (9)

88. Tell me about the design patterns you are familiar with?

Singleton mode

To put it simply, there is only one instance object of a certain class in an application. You can't go to new because the constructor is decorated by private. Generally, the instance of them is obtained through the getInstance() method.

The return value of getInstance() is a reference to an object, not a new instance, so don’t misunderstand it as multiple objects

Observer mode

There is a one-to-many dependency relationship between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.

Decorator pattern

The existing business logic is further encapsulated to add additional functions. For example, the IO stream in Java uses the decorator mode. When users use it, they can assemble it at will to achieve the effect they want.

Adapter mode

Connecting two completely different things together is like a transformer in real life. Assuming that a mobile phone charger needs a voltage of 20V, but the normal voltage is 220V, a transformer is needed to convert the 220V voltage into a 20V voltage. In this way, the transformer connects the 20V voltage to the mobile phone.

Factory mode

Simple factory pattern: an abstract interface, implementation classes of multiple abstract interfaces, and a factory class to instantiate abstract interfaces

Proxy mode (proxy)

There are two types, static proxy and dynamic proxy.

89. What is the difference between a simple factory and an abstract factory?

Simple factory, this model itself is very simple and used in the case of relatively simple business. It is generally used for small projects or when specific products are rarely expanded (so that the factory class does not need to be changed frequently).

The purpose of the abstract factory pattern is to provide an interface for the client to create product objects in multiple product families.

Guess you like

Origin blog.csdn.net/xghchina/article/details/114902829