Design pattern based on Android — detailed explanation of the strategy pattern of SDK source code

In fact, the strategy model is very simple (do you feel relaxed when you hear this sentence?). For example, sorting, the official told you that I have a sorting interface here, the sort () method of iSORT, and then people do their best to implement this sorting method: bubbling, fast, heap, etc. These methods are "different strategies". Then, a sorting method is required for a module, but the specific sort method cannot be specified for the time being (for expansion reasons), so the iSORT interface needs to be used. Finally, in what specific scenario, what specific sort method is passed in to achieve flexible sorting. This is the strategy mode! Next, we analyze how animation in Android uses policy mode.

1. Intend to define a series of algorithms, encapsulate them one by one, and make them replaceable. The policy pattern allows the algorithm to vary independently of the customers using it.

2. The implementation of different animations of structure diagram and code animation mainly depends on the different implementation of interpolator.

3. Effect (1). Behavioral mode (2). Some conditional statements of if... Else... Are eliminated (3). The customer can choose the implementation, but the customer must understand the implementation of this different strategy (this sentence seems to be nonsense. In a word, the customer needs to learn the cost) (4). The default implementation is mentioned in the code comment, which can make the customer not understand the strategy, but also realize the default strategy (5) There are many ways to inject: constructor, set method, annotation. Configuration resolution, etc

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>