Hybrid mode (Factory method mode + policy mode + facade mode)
Use these three modes to design a simple calculatorProgram: The calculator is used to calculate the value obtained after mathematical calculation between values. It includes the basic "addition and subtraction" function. After analyzing the above requirements, we can conclude that there are two calculation strategies (+ and -).
Computing policy implementation:
/*Abstract Policy*/
Public InterfaceStrategy
{
/*DefinesAlgorithm*/
Public IntCalculate (IntA,IntB );
}
/* Addition policy implementation */
Public Class Addstrategy implements strategy
{
@ Override
Public Int Calculate ( Int A, Int B)
{
Return A + B;
}
}
/* Implementation of subtraction Policy */
Public Class Substrategy implements strategy
{
@ Override
Public Int Calculate ( Int A, Int B)
{
Return A-B;
}
}
In this case, an object of the encapsulation policy is required to make the policy interchangeable:
Public Class Strategycontext
{
/* Encapsulated policy objects */
Private Strategy Strategy = Null ;
Public Strategycontext (strategy Strategy)
{
This . Strategy = strategy;
}
/* Implemented the rule swapping Function */
Public Void Setstrategy (strategy Strategy)
{
This . Strategy = strategy;
}
Public Int Execute ( Int A, Int B)
{
Return This . Strategy. Calculate (A, B );
}
}
To avoid the disadvantage that the policy mode must expose the specific policy to the high-level module, we use the factory to generate the policy. Now, the high-level module only needs one constraint to obtain the required policy.
Implementation of policy generation Factory:
/*Policy generation Abstract Factory*/
Public InterfaceFactory
{
/*Defines an interface for generating policies. You can also use a configuration file to implement constraints for its parameters. enumeration is used here.*/
PublicStrategy createstrategy (strategyenum );
}
Public class strategyfactory implements factory
{< br> @ override
Public strategy createstrategy (strategyenum)
{< br> strategy Strategy = null ;
try
{< br> string strategyname = strategyenum. getstrategyname ();
class
Clazz = Class. forname (strategyname);
strategy = (Strategy) clazz. newinstance ();
}< br> catch (exception E)
{< br> E. printstacktrace ();
}< br> return strategy;
}< BR >}
The calculation steps are as follows: get the factory, obtain the policy, encapsulate the policy, and calculate the result. This is troublesome to write. The high-level module still needs to remember the execution sequence to calculate a result. In this case, we can use the facade mode to avoid the complexity of the subsystem and provide a computing interface for the high-level module.
Implementation of computer facade:
Public Class Facadecontext
{
/* The facade mode does not participate in the subsystem logic, so the subsystem is encapsulated once. */
Private Factory factory = New Strategyfactory ();
Private Strategycontext context = New Strategycontext ( Null );
Public Facadecontext (){}
Public Int Calculate ( Int A, Int B, strategyenum strategy)
{
This . Context. setstrategy ( This . Factory. createstrategy (Strategy ));
Return This .Context.exe cute (A, B );
}
}
/*The front of the calculator, simple delegate class, provides an interface for the High Level to ask the sub-system, so that the High Level module no longer depends on the sub-system*/
Public ClassCalculatorfacade
{
PrivateFacadecontext context =NewFacadecontext ();
PublicCalculatorfacade ()
{}
Public IntCalculate (IntA,IntB, strategyenum strategy)
{
Return This. Context. Calculate (a, B, Strategy );
}
}
Let's take a look at the scenario class:
Public ClassClient
{
Public Static VoidMain (string [] ARGs)
{
/*Obtain the facade and access the subsystem using the interface provided by it*/
Calculatorfacade =NewCalculatorfacade ();
System. Out. println ("2 + 2 =" + calculatorfacade. Calculate (2, 2, strategyenum. Add ));
}
}
Appendix: Strategyenum EnumerationCode:
Public Enum strategyenum
{< br> Add (" com. suxiaolei. calculator. strategy. addstrategy "),
sub (" com. suxiaolei. calculator. strategy. substrategy ");
private string strategyname;
private strategyenum (string strategyname)
{< br> This . strategyname = strategyname;
}< br> Public string getstrategyname ()
{< br> return This . strategyname;
}< BR >}
The above is a simple example of mixed use of three modes. It can be seen that the flexible combination mode can make the system more robust, more flexible, and more scalable. In the preceding example, adding a policy is very easy. You only need to inherit the strategy interface and add the corresponding policy class name to the enumeration. The High-level Code does not need to be changed at all. To use the configuration file, you only needIf you inherit the strategy interface, you do not need to enumerate it. This is more flexible.