Design Pattern series-strategy

Source: Internet
Author: User

I. Class Diagram

 

Ii. Intention

Define a seriesAlgorithm,Encapsulate them one by one,And make them replaceable. This mode allows algorithms to change independently of customers who use it.

Iii. Applicability

A) Many related classes only have different behaviors."Policy"Provides a method to configure a class with one of multiple actions.

B) use different variants of an algorithm. For example, you may define different spaces/Time-weighed algorithm. When these variants are implemented as class layers of an algorithm[H o 8 7],You can use the policy mode.

C) algorithms use data that customers should not know. Policy modes can be used to avoid exposing complex algorithm-related data structures.

D) A class defines multiple behaviors.,These actions appear in the form of multiple condition statements in the operations of this class. Move related condition branches to their respectiveS t r a t e g yClass to replace these conditional statements.

Iv. Instances

Zhuge Liang's tips:

Article 1: Let Zhao Yun and his entourage visit Qiao Guoluo first. Qiao Guo is always the ghost of Zhou Yu and Sun ce. He has a certain prestige in Dongwu and has a good relationship with Wu Guotai. When I visited Qiao guoyi, I could tell Wu Guotai about Sun Liu's marriage (Wu Guotai did not know before), and Sun Quan zhou Yu was passive. Secondly, let him speak out in front of Wu Guotai to increase the possibility of Sun Liu's marriage.

Article 2: swindling Cao to report the revenge of Chibi, raising troops to the south and sending Liu Bei back to Jingzhou. Liu Bei spent a whole day in Dongwu, so he was happy. This is the design of Zhou Yu to make Liu Bei have fun.

Article 3: Use Mrs sun to block the pursuit of troops. Sun shangxiang is also the owner of Dongwu County. In her words, Dongwu's generals still have to pay attention to the volume of traffic.

 Package Explore. Strategy;

Public Interface Strategy {
Public Void Perform ();
}

Package Explore. Strategy;

Public Class Context {
Private Strategy strategy;

Public Context (strategy Strategy ){
This . Strategy = strategy;
}

Public Void Setstrategy (Strategy s ){
This . Strategy = s;
}

Public Void Perform (){
This . Strategy. Perform ();
}
}

Package Explore. Strategy;

Public Class Thefirststrategy Implements Strategy {

@ Override
Public Void Perform (){
System. Out. println ("Let Zhao Yun and his entourage visit Qiao Guoluo first. ");
}

}

Package Explore. Strategy;

Public Class Thesecondstrategy Implements Strategy {
@ Override
Public Void Perform (){
System. Out. println. ");
}

}

Package Explore. Strategy;

Public Class ThethirdstrategyImplements Strategy {
@ Override
Public Void Perform (){
System. Out. println ("use Mrs sun to guard the army. ");
}

}

Package Explore. Strategy;

Public Class Zhaoyun {
Public Static Void Main (string [] ARGs ){
// Article 1: Let Zhao Yun and his entourage visit Qiao Guoluo first.
Strategy first = New Thefirststrategy ();
// Article 2: swindling Cao to report the revenge of Chibi, raising troops to the south and sending Liu Bei back to Jingzhou.
Strategy Second = New Thesecondstrategy ();
// Article 3: Use Mrs sun to block the pursuit of troops.
Strategy third = New Thethirdstrategy ();

Context c = New Context (first );
C. Perform ();
C. setstrategy (second );
C. Perform ();
C. setstrategy (third );
C. Perform ();
}
}

5. StrategyMode in the comparator (Comparator).

 

 //  Comparator  
Public Interface Comparator <t> {
Int Compare (T O1, t O2 );
......

// Custom comparison algorithm
Public Interface Comparable <t> {
Public Int Compareto (t o );

// Integer-defined comparison algorithm
Public Final Class Integer Extends Number Implements Comparable <integer> {
Public Int Compareto (integer anotherinteger ){
Int Thisval = This . Value;
Int Anotherval = anotherinteger. value;
Return (Thisval <anotherval? -1: (thisval = anotherval? 0: 1 ));
}
......

// Comparison algorithms defined in the string class
Public Int Compareto (string anotherstring ){
Int Len1 = count;
Int Len2 = anotherstring. count;
Int N = math. Min (len1, len2 );
Char V1 [] = value;
Char V2 [] = anotherstring. value;
Int I = offset;
Int J = anotherstring. offset;

If (I = J ){
Int K = I;
Int Lim = N + I;
While (K <Lim ){
Char C1 = V1 [k];
Char C2 = v2 [k];
If (C1! = C2 ){
Return C1-C2;
}
K ++;
}
} Else {
While (N --! = 0 ){
Char C1 = V1 [I ++];
Char C2 = v2 [J ++];
If (C1! = C2 ){
Return C1-C2;
}
}
}
Return Len1-len2;
}

 

When we compare two objects, we only need to call one compare ()Method:

 
IntCMP = comparator. Compare (obj1, obj2 );

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.