Abstract Factory mode (abstraction) create pattern C # Simple Example
For player user number play1, play2 .... Changes, while the behavior moves, Jum without change
Player Playone Behavior: Move to the left and jump up
Player Playtwo Behavior: Move to the right and jump down
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;namespace adapterpattern{public partial class abstract Form:form {public abstractform () {InitializeComponent (); } private void Btndisplay_click (object sender, EventArgs e) {playfactory pf1 = new Playone ();//Hidden Hide behavior move and jump. Playworld p1 = new Playworld (PF1);//behavior move left and jump up This.listBox1.Items.Add (P1.createplay ()); Playworld P2 = new Playworld (new Playtwo ());//behavior moves right and jumps down This.listBox1.Items.Add (P2.createplay ()); }} The handover of the public class playworld//entity factory and the abstract factory {imove move; Ijump Jump; Public Playworld (Playfactory pp) {this.move = Pp.move (); This.jump = Pp.jump (); } public string Createplay () {move.move (); Jump.jump (); return move.movestring + jump.jumpstring; }} public abstract class playfactory//abstract factory {public abstract imove move (); Public abstract ijump jump (); } public class playone:playfactory//abstract Factory instance 1 {public override Imove Move () {return new L Eftmove (); } public override Ijump jump () {return new topjump (); }} public class playtwo:playfactory//abstract Factory instance 2 {public override Imove Move () {return New Rightmove (); } public override Ijump jump () {return new downjump (); }} public abstract class imove//abstract behavior imove {public string movestring; public abstract void Move (); } public abstract class ijump//abstract behavior Jump {public string jumpstring; public abstract void jump (); } public class Leftmove:imove//move behavior Instance 1 {public override void Move () {movestring = "move Left"; }} public class Rightmove:imove//move behavior Instance 2 {public override void Move () {movestring = "Move Right"; }} public class Topjump:ijump//jump behavior Instance 1 {public override void Jump () {jumpstring = "Jump Up"; }} public class Downjump:ijump//jump behavior Instance 2 {public override void Jump () {jumpstring = "Jump Down"; } }}
When you need to add a new player, just add another class that inherits Playfacotry, and nothing else needs to be changed.
New player behavior: Move left and jump down
public class newplay:playfactory//new player: Move left and jump down {public override Imove Move () { return new Leftmove (); } public override Ijump Jump () { return new downjump (); } }
Finally add a sentence on the client can be, it is convenient.
Playworld NEWP = new Playworld (new Newplay ());
This.listbox1. Items. Add (NEWP. Createplay ());
Abstract Factory mode (abstraction) create pattern C # Simple Example