Strategy mode (strategy) behavioral pattern C # Simple Example

Source: Internet
Author: User

Strategy mode (strategy) behavioral pattern C # Simple Example

The main example is the use of strategy mode to decompose several mobile algorithms.

namespace adapterpattern{public partial class Strategyfrom:form {public Strategyfrom () {        InitializeComponent (); } private void Btndisplay_click (object sender, EventArgs e) {Context context1 = new Context (new            Leftmove ());            Context1.movemethod1 ();            LISTBOX1.ITEMS.ADD (movedata.movestring);            CONTEXT1.MOVEMETHOD2 ();            ListBox1.Items.Add (movedata. movestring);            Context context2= New Context (new RightMove ());//algorithm changes after context2.movemethod1 ();            LISTBOX1.ITEMS.ADD (movedata.movestring);            CONTEXT2.MOVEMETHOD2 ();                   LISTBOX1.ITEMS.ADD (movedata.movestring);    }} public interface iproessmove//expression algorithm abstraction (strategy) {void Process (); } public class Context {Iproessmove proessmove;//object combination public Context (iproessmove proessmove)//connection to policy mode        dot {this.proessmove = Proessmove;     }   public void MoveMethod1 () {proessmove.process ();        Movedata.movestring + = "Method 1;";            } public void MoveMethod2 () {proessmove.process ();        Movedata.movestring + = "Method 2;";    }} The public class movedata//move algorithm uses the data {public static string movestring {set; get;} } public class leftmove:iproessmove//move algorithm 1 {public void Process () {movedata. movestring        = "Left move algorithm"; }} public class rightmove:iproessmove//move algorithm 2 {public void Process () {movedata.movest        Ring = "Right motion algorithm"; }} public class topmove:iproessmove//move algorithm 3 {public void Process () {Movedata.movestrin        g = "Move Up algorithm"; }    } }


Strategy mode (strategy) behavioral pattern C # Simple Example

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.