Software Engineering-strategy model of design model learning strategy

Source: Internet
Author: User

Technical Exchange, DH explanation.

Or beforeArticleFrom the previous blog.

When a class such as class A has a method C, but this method C is affected by many situations,
So how can we designCodeEasier to understand?
In fact, the simple factory model we mentioned earlier can also be implemented, but now I want to talk about the policy model.
Let's take a look at a UML diagram: The software designer has such a question in the first half and second half of the examination. Which mode does the UML show you? Haha

We can see that the context and Strategy of the main class are aggregated. Here it can be an attribute or a member variable.
However, strategy has multiple implementation classes.

Use code to speak:

Abstract class phpbase {public abstract function doit ();} class add extends phpbase {public function doit () {echo ('add \ n ');}} class sub extends phpbase {public function doit () {echo ('sub \ n') ;}} class phpstrategy {public function test ($ A) {$ B = NULL; switch ($ A) {Case 0: $ B = new add (); break; Case 1: $ B = new sub (); break ;} $ B-> doit () ;}} is it a bit like the factory model? $ A = new phpstrategy (); $ A-> test (0); $ A-> test (1); what about the result? Add \ nsub \ n has no line breaks... haha.

Next we should use C #. There are so many people using it:

Namespace strategy_designpattern {using system; abstract class Strategy {Abstract Public void doalgorithm ();} class firststrategy: Strategy {override public void doalgorithm () {console. writeline ("in first Strategy");} class secondstrategy: Strategy {override public void doalgorithm () {console. writeline ("in second strategy") ;}} class context {strategy s; public context (Strategy strat) {S = strat;} public void dowork () {// here} public void dostrategywork () {S. doalgorithm () ;}} public class client {public static int main (string [] ARGs) {firststrategy = new firststrategy (); context c = new context (firststrategy); C. dowork (); C. dostrategywork (); Return 0 ;}}}

Finally, it must have been Delphi's debut. I will not write it here. It was created directly using Delphi.

Istrategy = interface; tcontext = Class strict private var // <link> aggregation </link> fstrategy: istrategy; Public constructor create (astrategy: istrategy); Procedure contextrequest; end; istrategy = interface procedure sample; end; tconcretestrategy = Class (tinterfacedobject, istrategy) public procedure sample; end; implementation {$ R *. DFM} constructor tcontext. create (astrategy: istrategy); begin inherited create; fstrategy: = astrategy; end; Procedure tconcretestrategy. sample; begin {put your code for this particle strategy here} end; Procedure tcontext. contextrequest; begin fstrategy. sample; end;

Our realityAlgorithmClass only needs to implement the istrategy interface.

Finally, the application is as follows:

  • Many related classes only have different behaviors. "Policy" provides a method to configure a class with one of multiple actions.
  • Different variants of an algorithm are required. For example, you may define algorithms that reflect different space/time trade-offs.
  • When these variants are implemented as class layers of an algorithm [h o 8 7], the policy mode can be used.
  • Algorithms use data that customers should not know. Policy modes can be used to avoid exposing complex algorithm-related data structures.
  • A class defines multiple behaviors, and these behaviors appear in the form of multiple conditional statements in the operations of this class.
  • Move related condition branches into their respective s t r a t e g y classes to replace these condition statements.

    Today, I am DH.

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