Adapter mode
★★★★ ★★★★ ★★
Definition: The adapter pattern (sometimes called wrapper style or wrapper) ADAPTS the interface of a class to what the user expects. An adaptation allows classes that would normally not work together because of interface incompatibilities to work together by wrapping the class’s own interface in an existing class.
(1) Adaptation of classes (methods of expanding and modifying target classes)
Through inheritance and interface implementation: Implement new methods by inheriting the methods and interfaces of the original class.
(2) Object adaptation (expand and modify the use method of the target object)
Object references and interface implementations hold the original class: methods of the original object are implemented by invoking the reference to the object, and the interface implements the new method.
(3) Interface adaptation (solve the problem that there are too many interface definition methods and some interface methods do not need to be implemented)
Define an abstract class that implements the methods of the target interface (empty methods), inherit the abstract class, and override whichever method you want to implement.
Three adaptation scenarios:
Adapter pattern for class:
When you want to convert a class to a class that satisfies another new interface, you can use the adapter pattern of the class to create a new class that inherits the original class and implements the new interface.
Adaptor pattern for object:
When you want to convert an object to an object that satisfies another new interface, you create a Wrapper class that holds an instance of the original class. Within the Wrapper class’s methods, you call the instance’s methods.
Adapter mode for the interface:
When we don’t want to implement all of the methods in an interface, we can create an abstract class Wrapper to implement all of the methods. When we write other classes, we can inherit the abstract class.
Note: Adapter mode is not suitable for use during the detailed design phase. It is a compensation mode, intended for use during late system extension and modification.
Decorator 【Decorator】
★★★ ★★★ ★★★ ★★
Definition: Decorator mode refers to the dynamic extension of an object’s functionality without changing the original class file or using inheritance. It wraps the real object by creating a wrapper object, a decoration.
The ITarget class is the decorated interface, and the Decorator class is a Decorator class that dynamically adds functionality to the Target class.
As you can see from the above, decoration mode is similar to object adaptation mode in that it is a way to extend objects. But there are significant differences:
(1) Different objects are held:
Decorator mode, the decorator holds the interface of the decorator type.
Object adaptation mode, the adapter holds the specific object to be adapted.
(2) Different ways of expansion:
Decoration mode does not increase the number of methods of the decorated object, but enhances the methods or functions of the decorated object.
Object adaptation mode increases the number of methods used to adapt objects.
(3) Different purposes:
Decorator mode is to enhance functionality by adding new responsibilities or functions without changing the interface.
The adaptation mode of an object is to add an interface to match the original method.
Proxy mode [Proxy]
★★★★ ★★★
Definition: Provides a proxy for other objects to control access to that object. In some cases, an object is inappropriate or cannot directly reference another object, and a proxy object can act as an intermediary between the client and the target object.
(1) Static proxy
In the figure, IProxy is an abstract role, Proxy is a Proxy role, and NJProxy and BJProxy are real roles.
Static proxies are similar to decorators in that you can modify the methods of the objects that you delegate/decorator to. But there are significant differences:
(1) Decoration mode, the decorator needs to pass in the interface to be decorated when constructing.
In proxy mode, the agent does not need to pass in the proxy interface when constructing.
(2) Decoration mode, the main purpose is to enhance the method or function of the decorated.
Proxy mode, the main purpose is to control the proxy access object, the proxy object methods and functions are not enhanced.
Understanding the control of proxy access objects
For example, If Mr Ma is good at selling goods, he is fit to run Alibaba. Ma Huateng is good at social networking, so he is suitable for acting Tencent’s operation.
If it is the use of decoration mode, then Ma Yun can run Alibaba, also can run Tencent, so chaos…
Use case: Network request broker
(2) Dynamic proxy
Unlike static proxies, dynamic proxies do not implement a specific proxy interface. Instead, they implement the InvocationHandler interface, which calls the proxy’s interface to implement the dynamic proxy.
Dynamic proxies can be used when the number of interfaces we need to delegate is too large and methods do not need to be handled in detail.
Use cases: Web request broker, Retrofit, etc
Facade 【Facade】
★★★★★ ★★★★
Definition: Provides a consistent interface for a set of interfaces in a subsystem. Defines a high-level interface that makes the subsystem easier to use.
(Resolve dependencies between related classes and put their dependencies into a facade class to reduce system complexity)
A graphical appearance mode:
In this case, the Computer acts as the facade, its components act as subsystems, and the User acts as the client.
The Facade pattern has the following advantages:
1) Shielding subsystem components from customers reduces the number of objects handled by customers and makes the subsystem easier to use. By introducing the facade pattern, the client code becomes simple and has few objects associated with it.
2) The loose coupling between the subsystem and the customer is realized, which makes the component change of the subsystem does not affect the customer class that calls it, and only needs to adjust the appearance class.
3) Reduce compile dependency in large software systems and simplify the migration process between different platforms, because compiling one subsystem generally does not require compiling all other subsystems. Changes in one subsystem have no effect on the other subsystems, and changes within the subsystem do not affect appearance objects.
4) Only provides a unified access to the subsystem, does not affect the user directly using the subsystem class.
Disadvantages of the Facade pattern:
1) Do not restrict customer access to subsystem classes well, and restricting customer access to subsystem classes too much reduces variability and flexibility.
2) Adding new subsystems without introducing abstract facade classes may require modifying the facade class or client source code, violating the “open closed principle”. The extensibility is poor
Because the facade pattern provides a unified entry point to subsystems, common and singleton patterns are used in combination.
The facade pattern is used to divide a very complex method into private submethods and provide it externally with a unified public method.
Bridge mode
★★★ ★★★ ★★★ ★★
Definition: To separate the abstract part from its implementation part so that both can vary independently. It is an object structure pattern, also known as Handle and Body pattern or Interface pattern.
Bridging is about separating things from their implementations so that they can change independently.
The essence of the bridging pattern is to separate abstraction from implementation. Only when the two are separated can they change independently, and only when the two can change relatively independently can the system have better scalability and maintainability. The bridging pattern follows the open close principle well and favors Composition Over Inheritance well.
【Composite】
★★★★ ★★★★ ★★
Definition: Grouping objects into a tree structure to represent a partial-whole hierarchy. The composition pattern makes the use of individual objects and composite objects consistent. The key to mastering the composite pattern is to understand the meaning of “part/whole” as well as “single object” and “composite object”.
A Leaf is a Leaf object (a single object) and a Composite is a container object (a Composite object).
The combined mode can be divided into transparent mode and security mode:
Transparent: Declare all methods to manage child objects in Component, including Add, Remove, and so on. All subclasses that implement the Component interface thus have Add and Remove methods. The advantage of this is that leaves and branches are indistinguishable from the outside world, and they have exactly the same interface.
Safe: Instead of declaring Add and Remove methods in Component, the subclass’s Leaf does not need to implement them. Instead, it declares all methods used to manage subclass objects in the Composit.
Applicable scenario
(1) When you want to express the partial-whole hierarchy of an object
(2) when the user is expected to ignore the difference between a composite object and a single object, and the user will use all objects in the composite structure uniformly.
For example:
Component is just like a well-known beauty and hair salon, which has Leaf stores and Composite stores. However, for customers, if they have membership cards of this store, according to the truth, there should be no difference in their use whether they are in the franchise store, the main store or branch store. This is where composite patterns are needed.
Similarly, a large multinational company can use the portfolio model to demonstrate its corporate structure. Its structure is similar to binary tree structure.
Use case: Android View and ViewGroup
【Flyweight】
★★★★ ★★★★ ★
Definition: The main purpose of the share pattern is to achieve the sharing of objects, that is, the shared pool, when there are many objects in the system to reduce the memory overhead, usually used in conjunction with the factory pattern.
Usage Scenarios:
1. When we find that there are a large number of instances of objects of a certain type and they have similar functions.
2. We found that the performance of the system could be improved without adding more complexity by using the share mode.
Use cases: database connection pool, thread pool, RxBus
Program source code
Github project source address :github.com/xuexiangjys… , remember to give a STAR if you feel helpful! ~ ~
Wechat official account
More information, welcome to wechat search public number: [My Android open source journey]