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