Design Pattern 22-behavior pattern policy pattern

Source: Internet
Author: User

Definition:Strategy Pattern defines a series of algorithms, encapsulates them one by one, and enables them to replace each other. This mode allows algorithms to change independently of customers who use it.

Type: object behavior mode.

Overview:

Policy mode is a typical object behavior mode. It separates a series of different algorithms for processing objects and encapsulates them into classes. The emergence of policies mainly aims to solve the logic judgment when different algorithms are replaced and move the logic judgment to the Client. Rule modes are common but relatively simple.

A series of algorithms, as a programmer, can easily come up with sorting algorithms. The sorting algorithm is used as an example. If the text content Context is recorded as unordered data, it is best to select Quick Sort. If multiple parts are sorted, only some data is inserted at will, select Sort can be used ).

Class diagram:


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ss7T69Xfo7o8L3A + CjxvbCB0eXBlPQ = "1">

  • Context maintains a pointer to SortAlgo to access the corresponding sorting algorithm.
  • SortAlgo is an algorithm policy interface class that only provides interfaces.
  • QuickSort and SelectSort implement specific algorithms according to interfaces.

    Sample Code:

    Using System; using System. collections. generic; using System. text; namespace Pattern22 {// abstract algorithm class abstract class SortAlgo {// algorithm method public abstract void Sort () ;}// quick sorting algorithm class QuickSort: sortAlgo {// specific fast Sorting Algorithm public override void Sort () {Console. writeLine ("quick Sorting Algorithm Implementation") ;}// select the sorting algorithm class SelectSort: SortAlgo {// specific implementation select the Sorting Algorithm public override void Sort () {Console. writeLine ("select Sorting Algorithm Implementation") ;}// Context class Context {SortAlgo algo; public Context (SortAlgo _ algo) {this. algo = _ algo;} // context interface public void SortData () {algo. sort () ;}} class Program {static void Main (string [] args) {Context context; // when random data is input, select the Quick Sort Algorithm context = new Context (new QuickSort (); context. sortData (); // when inputting some ordered data, use the selection Sorting Algorithm context = new Context (new SelectSort (); context. sortData (); Console. read ();}}}

    Applicability:

    1. Multiple classes only differ in performance. You can use the Strategy Mode to dynamically select the behavior to be executed during running.
    2. Add other algorithms to implement the current operation.
    3. Hide the implementation details of specific algorithms on the client.

      Advantages and disadvantages:

      1. Advantage: Move the judgment logic out of the class to complete the new algorithm by adding a new class.
      2. Disadvantages: if there are too many algorithm classes, it will cause trouble to judge how to call different algorithms on the Client side.

        References:

        1. Design Patterns-reusable Object-Oriented Software basics
        2. Big talk Design Model

  • 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.