Policy mode: For the same command or behavior, different policies do different actions.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacestrategydesign{classProgram {Static voidMain (string[] args) {Strategycontext Context=NewStrategycontext (); //set the random policyContext. Setstrategy (Newrandstrategy ()); Context. Setup (); //set "send directly"Context. Setstrategy (Newstraightstrategy ()); Context. Setup (); } } Public Abstract classAbstractstrategy { Public Abstract voidSetup (); } Public classRandstrategy:abstractstrategy { Public Override voidSetup () {Console.WriteLine ("e-mail delivery in thousand-face mode"); } } Public classStraightstrategy:abstractstrategy { Public Override voidSetup () {Console.WriteLine ("Mail sent by ordinary merchant"); } } Public classStrategycontext {abstractstrategy Strategy=NULL; Public voidSetstrategy (Abstractstrategy strategy) { This. Strategy =strategy; } Public voidSetup () { This. Strategy. Setup (); } }}
C # design Pattern: Strategist Mode (stragety pattern)