Abstract Factory mode (abstraction) create pattern C # Simple Example

Source: Internet
Author: User

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

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.