24-day design model-strategy model, 24-day Design Model
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka
I. Stragegy Pattern 1. Introduction
The Strategy Mode is also called the rule mode. It encapsulates a series of algorithms and defines an abstract algorithm interface for all algorithms, all algorithms are encapsulated and implemented by inheriting the abstract algorithm interface. The specific algorithm selection is determined by the client (policy ). The Strategy Mode is mainly used to smoothly process algorithm switching.
2. Intention
Define a series of algorithms, encapsulate them one by one, and they can be replaced with each other. This allows algorithms to change independently of customers who use it.
3. Applicability
- If there are many classes in a system, the difference between them is only their behavior, then the policy mode can dynamically allow an object to select a behavior among many behaviors.
- A system must dynamically select one of several algorithms. These algorithms can be encapsulated into specific algorithm classes, which are subclasses of an abstract algorithm class. In other words, these specific algorithm classes have a unified interface. Due to the polymorphism principle, the client can choose to use any specific algorithm class and only hold one data type as the object of the abstract algorithm.
- The data used by a system algorithm cannot be known to the client. Policy mode prevents clients from involving complex algorithm-related data that is not necessary to access.
- If a class defines multiple behaviors, and these behaviors appear in the form of multiple condition statements in the class operations. Move related condition branches to their respective Strategy classes to replace these condition statements.
2. When using examples to save an image, you can dynamically choose the format in which the image is saved. First, the UML diagram:
Then let's look at the Code:
Public abstract class saveImageRule {/* set the format for saving the image */public abstract void saveImage ();}
Then there are two policy implementation classes, indicating the format of the image to be saved as bmp
Public class saveImageToBmp extends saveImageRule {@ Overridepublic void saveImage () {System. out. println ("Save image as BMP ");}}
Save as PNG
Public class saveImageToPng extends saveImageRule {@ Overridepublic void saveImage () {System. out. println ("Save image as PNG ");}}
Then define a class to set the format for saving the image.
Public class Strategy {private saveImageRule m_saImageRule; // Save the image format/* Save the image in one format at the beginning */public Strategy (saveImageRule m_saImageRule) {this. m_saImageRule = m_saImageRule;}/* change the format of the saved image */public void changeImageRule (saveImageRule m_saImageRule) {this. m_saImageRule = m_saImageRule;}/* Get the format of the currently saved image */public void getImageFormat () {m_saImageRule.saveImage ();}}
The following is the usage:
/*** File name: Main. java * Description: Policy mode * created by: Lin bingwen * Date: 2015.1.31 **/package com. modern. strategy; public class Main {public static void main (String [] args) {Strategy m_sStrategy = new Strategy (new saveImageToBmp ()); // At the beginning, it is saved as BMP m_sStrategy.getImageFormat (); m_sStrategy.changeImageRule (new saveImageToPng () by default; // m_sStrategy.getImageFormat ();}}
Result: The image is saved as BMP.
Save the image as PNG
It can dynamically change the algorithm during running !!
Iii. Advantages and Disadvantages
1. Advantages of the Policy mode (1). The policy mode provides methods for managing related algorithm families. The hierarchical structure of a policy class defines an algorithm or behavior family. Proper use of inheritance allows you to move public code to the parent class to avoid repeated code.
(2) The policy mode provides a way to replace the inheritance relationship. Inheritance can process multiple algorithms or actions. If the policy mode is not used, the Environment class that uses algorithms or behaviors may have some subclasses. each subclass provides a different algorithm or behavior. However, in this way, the user of an algorithm or behavior is mixed with the algorithm or behavior itself. The logic that determines which algorithm to use or which behavior to take is mixed with the logic of the algorithm or behavior, so that it is impossible to evolve independently. Inheritance makes it impossible to dynamically change algorithms or behaviors.
3. Use Policy mode to avoid using multiple conditional transfer statements. Multiple transfer statements are not easy to maintain. they mix the logic of which algorithm or behavior is adopted with the logic of the algorithm or behavior and are all listed in a multiple transfer statement, it is more primitive and backward than the inheritance method.
2. disadvantages of policy Mode
(1) The client must know all the policy classes and decide which one to use. This means that the client must understand the differences between these algorithms so that appropriate algorithm classes can be selected in a timely manner. In other words, the policy mode is only applicable when the client knows all the algorithms or actions.
(2) There are many strategies caused by the rule mode. Sometimes you can save the environment-dependent status to the client and design the policy class to be shared so that the policy class instance can be used by different clients. In other words, you can use the metadata mode to reduce the number of objects.
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka