Adapter mode (adapter) C # Simple Example
Combined with the class in the back appearance mode, the Wemanplay class in the MyDLL.DLL class library is introduced externally, its interface has properties Stringmove, Stringjump and Behavior Wemanmove (), Wemanjump (), and the adapter becomes the universal interface in its own class.
Add a new class adapter in appearance mode
The public class addplay:play//is adapted from the Wemanplay class to the existing play class { private wemanplay wplay;//a compiled Wemanplay class Public Addplay () { wplay = new Wemanplay (); } public override void Move ()//To fit the Wemanmove in the Wemanplay class to the existing class, move { wplay.wemanmove (); movestring = Wplay.stringmove; The attribute Stringmove is adapted to an existing class in movestring; } public override void Jump ()//The Wemanjump in the Wemanplay class is adapted to the existing class jump { wplay.wemanjump (); jumpstring = wplay.stringjump;//Property Stringjump fit into existing class jumpstring; }
All codes are as follows:
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;using mydll;//Add Reference Wemanplay class namespace Adapterpattern {public partial class Form1:form {public Form1 () {InitializeComponent (); private void Btndisplay_click (object sender, EventArgs e) {facadefactory ff = new Facadefactor Y ();//appearance mode hides the player, the user does not know that there are several players ff.move ();//Achieve Appearance function 1 this.listBox1.Items.Add (ff.movestring); Ff.jump ();//Achieve Appearance function 2 this.listBox1.Items.Add (ff.jumpstring); }} public abstract class play//to all players abstract {public string movestring {get; set;} public string jumpstring {get; set;} public abstract void Move (); public abstract void jump (); } public class play1:play//player 1 {public override void Move () {movestring = "play1Move "; } public override void Jump () {jumpstring = ' play1 jump '; }} public class play2:play//player 2 {public override void Move () {movestring = "play2 m Ove "; } public override void Jump () {jumpstring = ' play2 jump '; }} The public class addplay:play//is adapted from the Wemanplay class to the existing play class {private Wemanplay wplay; Public Addplay () {wplay = new wemanplay (); } public override void Move ()//To fit Wemanmove in the Wemanplay class to the existing class move {wplay.wemanmove (); movestring = Wplay.stringmove; The attribute stringmove is suitable for movestring in the existing class; } public override void Jump ()//To fit the Wemanplay class in Wemanjump to the existing class jump {wplay.wemanjump (); jumpstring = wplay.stringjump;//attribute stringjump is suitable for jumpstring in an existing class; }} public class facadefactory//appearance mode factory implementation {public string movestring; public string Jumpstring; List<play> plays = new list<play> (); Public Facadefactory () {plays. ADD (New Play1 ()); Plays. ADD (New Play2 ()); Plays. ADD (New Addplay ()); } public void Move ()//Appearance mode function One: Two players teaming together to move {foreach (play Playt in plays) { Playt.move (); Movestring + = playt.movestring; }} public void jumps ()//Appearance mode Function II: Two players team up together jump {foreach (play Playt in plays) { Playt.jump (); Jumpstring + = playt.jumpstring; } } }}
The mydll.dll referenced in this article is downloaded here Http://yunpan.cn/cfwfiu7KzAkiZ extract Code 3d67
how to make and reference class library DLLs in C # in VS2010
Adapter mode (adapter) C #