Policy patterns define a series of algorithms, encapsulate each algorithm, and make them replaceable. The rule mode allows algorithms to change independently of customers who use it. (Original article: The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it .)
Context ):
1. The ConcreteStrategy algorithm is required.
2. Maintain a Strategy instance internally.
3. dynamically sets the specific implementation algorithm of Strategy during runtime.
4. Interaction and data transmission with Strategy.
Strategy ):
1. A public interface is defined. Different algorithms implement this interface in different ways. The Context uses this interface to call different algorithms, which are generally implemented using interfaces or abstract classes.
ConcreteStrategy (specific policy class ):
2. Implemented the interface defined by Strategy and provided specific algorithm implementation.
Now I want to create a Strategy mode using the example of computer installation when we buy a computer:
First look at a use case diagram:
Create a new Computer. cs:
View sourceprint? Public abstract class Computer
{
Public abstract string MainBoard ();
Public abstract string Cpu ();
Public abstract string PhenoType ();
Public abstract string Memory ();
Public abstract string HardDisk ();
Public abstract string Display ();
}
Then create lenovo. cs:
View sourceprint? Public class lenovo: Computer
{
Public override string MainBoard ()
{
Return "Asus 880G series ";
}
Public override string Cpu ()
{
Return "Samsung dual-core 180 (2.4 GHz )";
}
Public override string PhenoType ()
{
Return "integrated high-performance video card ";
}
Public override string Memory ()
{
Return "1g ddriii ";
}
Public override string HardDisk ()
{
Return "500G ";
}
Public override string Display ()
{
Return "19-inch wide screen LCD ";
}
}
Then create HP. cs:
View sourceprint? Public class HP: Computer
{
Public override string MainBoard ()
{
Return "ATI RS482 ";
}
Public override string Cpu ()
{
Return "X-Dragon 64-bit x2 dual-core 5000 + ";
}
Public override string PhenoType ()
{
Return "NV G310 512 M ";
}
Public override string Memory ()
{
Return "2G DDR2 667 ";
}
Public override string HardDisk ()
{
Return "3200G ";
}
Public override string Display ()
{
Return "19-inch wide screen LCD ";
}
}
Then create a computer-like BuyComputer. cs:
View sourceprint? Public class BuyComputer
{
Private Computer _ computer;
Public BuyComputer (Computer computer)
{
_ Computer = computer;
}
Public string ShowComputerConfigure ()
{
StringBuilder strCom = new StringBuilder ();
StrCom. AppendLine ("your computer configuration is as follows :");
StrCom. AppendLine ("the main board is:" + _ computer. MainBoard ());
StrCom. AppendLine ("processor:" + _ computer. Cpu ());
StrCom. AppendLine ("video card is:" + _ computer. PhenoType ());
StrCom. AppendLine ("Memory is:" + _ computer. Memory ());
StrCom. AppendLine ("hard disk is:" + _ computer. HardDisk ());
StrCom. AppendLine ("Display is:" + _ computer. Display ());
StrCom. AppendLine ("assembled ");
Return strCom. ToString ();
}
}
Then call it:
<