Step by step. NET Design Patterns learning notes 3. Strategy)

Source: Internet
Author: User

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:

<

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.