Purpose: Reduce dependency
Design patterns correspond to different needs, design principles represent the eternal soul, in practice may not always abide by, but always keep in mind.
1. Dependence reversal principle (dependence inversion Principle)
2. Interface isolation principle (Interface segregation Principle)
3. The principle of substitution on the Richter scale (Liskov Substitution Principle)
4. Opening and closing principle (Open Close Principle)
5. Dimitri Law (Demeter Principle)
6. Synthetic multiplexing principles (Composite reuse Principle)
1. Inheritance can implement code reuse, but subclasses should not handle the method overloads of the parent class.
2. Separate the parts that are always changing so that they can be easily changed in the future to extend this part without affecting other parts that do not need to change. Form a new class in a combined fashion.
Implement the Duck class, separating the easy-to-change flight and Tweet methods: Using the Strategy mode + simple Factory
Interface:
namespace designpatterns.strategy{ interface iflybehavior { void Fly (string name);} }
namespace designpatterns.strategy{ interface iquackbehavior { void Toquack (string name);} }
Using System;namespace designpatterns.strategy{abstract class Duck {private Iflybehavior _flybehavior; Private Iquackbehavior _quackbehavior; private string _name; <summary>/////</summary> public string name {get {return this._name; } set {this._name = value; }}///<summary>/////</summary> public Iflybehavior Flybehavior {set {This._flybehavior = value; }}///<summary>/////</summary> public Iquackbehavior Quackbehavi or {set {This._quackbehavior = value; }}///<summary>//Flight///</summary> public void Performfly () { _flybehavior.fly (_name); }//<summary>//Tweet///</summary> public void Performquack () { _quackbehavior.toquack (_name); }///<summary>//Profile///</summary> public abstract void Display (); <summary>///swimming//</summary> public void Swim () {Console.Write ( _name + "Underwater swimming!\n"); } }}
Flight mode
Using System;namespace designpatterns.strategy.flystyle{//<summary>/// </summary > class Flynoway:iflybehavior { private string _name; Public Flynoway (string name) { this._name = name; } public void Fly (string name) { Console.Write (name + "does not fly!\n");} }}
Using System;namespace designpatterns.strategy.flystyle{///<summary>/// </ Summary> class Flywithwings:iflybehavior { private string _name; Public flywithwings (string name) { this._name = name; } public void Fly (string name) { Console.Write (name + "wing flew up!\n");}} }
Namespace designpatterns.strategy.flystyle{ class flyfactory {public enum flystyles { Fly, cannotfly } static public Iflybehavior Getflytype (string name, Flystyles flystyles) { if (flystyles = = flystyles.fly) { //will fly return to new Flywithwings (name); } else { //will not fly return to new Flynoway (name); } }}
Tweet Way
Using System;namespace designpatterns.strategy.quackstyle{//<summary>/// </summary > class Mutequack:iquackbehavior { private string _name; Public Mutequack (string name) { this._name = name; } public void Toquack (string name) { Console.Write (name + "will not be called!\n");} }}
Using System;namespace designpatterns.strategy.quackstyle{//<summary>/// </ Summary> class Squeak:iquackbehavior { private string _name; Public Squeak (string name) { this._name = name; } public void Toquack (string name) { Console.Write (name + "Squeak!\n");} } }
Using System;namespace designpatterns.strategy.quackstyle{//<summary>/// </summary > class Quack:iquackbehavior { private string _name; Public Quack (string name) { this._name = name; } public void Toquack (string name) { Console.Write (name + "Quack!\n"); } }}
Namespace designpatterns.strategy.quackstyle{ class quackfactory {public enum Quackstyles { Mutequack,/ /will not be called Squeak, //Squeak quack //Quack } static public Iquackbehavior Getquacktype (string name, Quackstyles quackstyles) { if (quackstyles = = Quackstyles.mutequack) { return new Mutequack (name); } else if (quackstyles = = Quackstyles.squeak) { return new Squeak (name); } else { return new quack (name); } } }
Duck class
Using system;using designpatterns.strategy.flystyle;using Designpatterns.strategy.quackstyle;namespace designpatterns.strategy.duckmodel{ class Blueduck:duck {public Blueduck (string name) {this . name = name; This. Flybehavior = Flyfactory.getflytype (name, FlyFactory.FlyStyles.CanNotFly); This. Quackbehavior = Quackfactory.getquacktype (name, QuackFactory.QuackStyles.MuteQuack); Display (); } public override void Display () { Console.Write (this. Name + "Blue Feather!\n");}} }
Using system;using designpatterns.strategy.flystyle;using Designpatterns.strategy.quackstyle;namespace designpatterns.strategy.duckmodel{ class Redduck:duck {public Redduck (string name) {this . name = name; This. Flybehavior = Flyfactory.getflytype (name, FlyFactory.FlyStyles.CanNotFly); This. Quackbehavior = Quackfactory.getquacktype (name, QuackFactory.QuackStyles.Squeak); Display (); } public override void Display () { Console.Write (this. Name + "with red Feather!\n");}}}
Using system;using designpatterns.strategy.flystyle;using Designpatterns.strategy.quackstyle;namespace designpatterns.strategy.duckmodel{ class Yellowduck:duck {public Yellowduck (string name) { This. name = name; This. Flybehavior = Flyfactory.getflytype (name, FlyFactory.FlyStyles.Fly); This. Quackbehavior = Quackfactory.getquacktype (name, QuackFactory.QuackStyles.Quack); Display (); } public override void Display () { Console.Write (this. Name + "with yellow feather!\n");}}}
Namespace designpatterns.strategy.duckmodel{ class duckfactory {public enum ducktypes { Red, Yellow, Blue } static public Duck Getoneduck (string name, Ducktypes ducktypes) { if (ducktypes = = ducktypes.red) { return new Redduck (name); } else if (ducktypes = = Ducktypes.yellow) { return new Yellowduck (name); } else { return new Blueduck (name); } } }
Test
Using system;using designpatterns.strategy;using designpatterns.strategy.duckmodel;using Designpatterns.strategy.flystyle;using Designpatterns.strategy.quackstyle;namespace DesignPatterns{class Program { static void Main (string[] args) {Duck red = Duckfactory.getoneduck ("Red-hairy Duck", duckfactory.ducktypes . Red); Red. Performfly (); Red. Performquack (); Red. Swim (); Console.Write ("\ n"); Red. Flybehavior = Flyfactory.getflytype (red. Name, FlyFactory.FlyStyles.Fly); Red. Quackbehavior = Quackfactory.getquacktype (red. Name, QuackFactory.QuackStyles.Quack); Red. Performfly (); Red. Performquack (); Console.Write ("\ n"); Duck yellow = Duckfactory.getoneduck ("Yellow-hairy Duck", DuckFactory.DuckTypes.Yellow); Yellow. Performfly (); Yellow. Performquack (); Yellow. Swim (); Console.Write ("\ n"); Duck blue = Duckfactory.getoneduck ("Blue-Hairy Duck", DUckFactory.DuckTypes.Blue); Blue. Performfly (); Blue. Performquack (); Blue. Swim (); Console.readkey (); } }}
Head First design mode (-) strategy mode (duck)