Policy mode (c + +)

Source: Internet
Author: User

 Strategy mode: Encapsulate a series of algorithms so that they can be replaced with each other. This allows the algorithm to be independent of client-side changes.

If we have a lot of sorting algorithms, but in a non-working environment, we need to use different algorithms, then we can define an abstract class, provide a unified interface, then inherit the abstract class in each sorting algorithm, and implement the sorting algorithm of the subclass, then define a project class, Use different algorithms in different projects by passing constructors to objects of different algorithm classes or instantiation of templates. Ah, a lot of nonsense, now more popular fast food culture ha, then we directly look at UML class diagram bar:

Above is the class diagram, below we look at the code, divided by passing in the object pointer specified and instantiated through the template specified.

Method 1: Specify by passing in the object pointer:

#include <iostream>using namespacestd;classSort//Sorting algorithm Classes{ Public:    Virtual voidUserSort () =0;//using Sort    Virtual~Sort () {};};classBubblesort: PublicSort//Bubble Sort Algorithm class{ Public:    Virtual voidUserSort () {cout<<"Bubblesort::usersort ()"<<Endl; }};classSelectsort: PublicSort//Choosing a Sorting algorithm class{ Public:    Virtual voidUserSort () {cout<<"Selectsort::usersort ()"<<Endl; }};classInsertsort: PublicSort//inserting sorting algorithm classes{ Public:    Virtual voidUserSort () {cout<<"Insertsort::usersort ()"<<Endl; }};classQuickSort: PublicSort//Fast sorting algorithm classes{ Public:    Virtual voidUserSort () {cout<<"Quicksort::usersort ()"<<Endl; }};classProject//project classes to replace different algorithms based on different objects{Private: Sort*M_sort; Public: Project () {} project (Sort*sort): M_sort (sort) {}voidReplacesort () {M_sort-UserSort (); }    ~Project () {if(NULL! =M_sort) {            DeleteM_sort; }    }};intMainintargcChar**argv) {Sort* Sort =NewInsertsort ();//you can also new algorithm classes hereProject Pro (sort);            Pro.replacesort (); return 0;}

The output results are as follows:

Method 2: Through template instantiation implementation

#include <iostream>using namespacestd;classSort//Sorting algorithm Classes{ Public:    Virtual voidUserSort () =0;//using Sort    Virtual~Sort () {};};classBubblesort: PublicSort//Bubble Sort Algorithm class{ Public:    Virtual voidUserSort () {cout<<"Bubblesort::usersort ()"<<Endl; }};classSelectsort: PublicSort//Choosing a Sorting algorithm class{ Public:    Virtual voidUserSort () {cout<<"Selectsort::usersort ()"<<Endl; }};classInsertsort: PublicSort//inserting sorting algorithm classes{ Public:    Virtual voidUserSort () {cout<<"Insertsort::usersort ()"<<Endl; }};classQuickSort: PublicSort//Fast sorting algorithm classes{ Public:    Virtual voidUserSort () {cout<<"Quicksort::usersort ()"<<Endl; }};template<typename t>classProject//project classes to replace different algorithms based on different objects{Private: T m_sort; Public: Project () {}voidReplacesort () {m_sort.usersort (); }};intMainintargcChar**argv) {Project<QuickSort> Pro;//Other algorithms can also be instantiated herePro.replacesort (); return 0;}

The output results are as follows:

In fact, the above code is a bit verbose, through the object pointer and template instantiation implementation only project class and main function there are some differences, it will be the next!!!!!

The above code runs OK on VC6.0.

Policy mode (c + +)

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.