The strategy pattern described here is a design pattern that is often used in program design with multiple branching scenarios. For example, we remove the fruit skin, generally for different fruit, there will be different ways to peel. This is indicated in the programming language:
if(type == apple){ //deal with apple } else if (type == banana){ //deal with banana} else if (type == ......){ //......}
As the above code says, we use conditional blocks to judge different types of fruit and then take different skinning methods. But this way in the programming field will lead to the original code is modified, that is, every time we want to add a new kind of fruit peeling, we have to modify the original code, which will lead to the original code is not stable. So we take a better way to achieve the peeling function of different fruit, that is the strategy mode.
public interface PeelOff { void peelOff();}public class ApplePeelOff implement PeelOff{ void peelOff(){ //deal with apple }}public class BananaPeelOff implement PeelOff{ void peelOff(){ //deal with banan }}public class PeelOffFactory{ private Map<String, PeelOff> map = new HashMap(); private init(){ //init all the Class that implements PeelOff interface }}public static void main(){ String type = "apple"; PeelOff peelOff = PeelOffFactory.getPeelOff(type); //get ApplePeelOff Class Instance. peelOff.pealOff();}
For this method, the next time we want to add a new kind of fruit skinning, we just need to create a new fruit peeling class, let it implement the Peefoff interface just fine. The business logic implemented using the policy pattern is more flexible and is often used as a substitute for conditional statement blocks in programming.
And the SPI mechanism is in fact similar to the policy model, in fact, Java used in a technical implementation, the full name is the service Provider Interface, that is, services provide interface, generally used in open source framework research and development field. For example, the implementation of the JDBC connection in Java uses the SPI mechanism. In a JDBC connection, because there are a variety of databases, each database is implemented differently, it is unrealistic to expect the JDK to implement all the databases again. So, the JDK provides an interface, you just follow my specifications to achieve, then I can ensure that users can connect to your database, this set of specifications is the SPI mechanism. About the SPI mechanism, you can read another article I wrote, perhaps you can better understand: "I am SPI, I make the framework more elegant!" 》
So what's the difference between a strategy model and an SPI mechanism?
If you look at the level of code access, the policy pattern modifies the code in the original project, except that it does not modify the code in the original class, but instead creates a new class. The SPI mechanism does not modify the code in the original project, it creates a new project, and eventually the way code is introduced in the Jar package.
From this point of view, regardless of the policy mode or SPI mechanism, they are isolating the changes from the original code, thus avoiding the effect of the new code on the original code. However, the policy pattern is isolation at the class level, while the SPI mechanism is isolation at the project framework level.
From the application domain, the policy pattern is more applied in the business domain, namely the business code writing and the business code refactoring. The SPI mechanism is more used in the design of the framework, the flexibility provided by the SPI mechanism, so that the framework has good plug-in characteristics, easy to expand.
To summarize, there are several similarities and differences between the strategy model and the SPI mechanism:
- from the perspective of design ideas. the idea of the strategy model and the SPI mechanism is similar, which is to isolate the changing parts through a certain design, thus making the original part more stable.
- from the isolation level. the isolation of policy patterns is class-level isolation, and the SPI mechanism is project-level isolation.
- from the field of application. The policy pattern is more used in business code writing, and the SPI mechanism is more used for framework design.
About the strategy model and SPI mechanism here, if there is anything you want to know, please tell me the message.