Adapter Mode c #
Adapter Mode c # simple example
In combination with the class in the last appearance mode, introduce mydll from the outside. the wemanplay class in the dll class library. Its interfaces include stringMove, stringJump, wemanmove (), and wemanjump (). You can use an adapter to change it to a common interface in your class.
Add a new class adapter in appearance Mode
Public class addplay: play // adapted from the cited wemanplay class to the existing play class {private wemanplay wplay; // cited the compiled wemanplay class public addplay () {wplay = new wemanplay ();} public override void move () // adapt wemanmove in the wemanplay class to move {wplay in the existing class. wemanmove (); movestring = wplay. stringMove; // The stringMove attribute is adapted to movestring in the existing class;} public override void jump () // adapt wemanplay to jump {wplay in the existing class. wemanjump (); jumpstring = wplay. stringJump; // The stringJump attribute is adapted to jumpstring in the existing class ;}
All code is 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 facadeFactory (); // The appearance mode hides players. The user does not know how many players ff. move (); // implement the appearance function 1 this. listBox1.Items. add (ff. movestring); ff. jump (); // implement the appearance function 2 this. listBox1.Items. add (ff. jumpstring) ;}} public abstract class play // abstracts all players {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 = "play1 move ";} public override void jump () {jumpstring = "play1 jump" ;}} public class play2: play // PLAYER 2 {public override void move () {movestring = "play2 move";} public override void jump () {jumpstring = "play2 jump" ;}} public class addplay: play // adapted from the introduced wemanplay class to the existing play class {private wemanplay wplay; public addplay () {wplay = new wemanplay ();} public override void move () // adapt wemanmove in wemanplay to move {wplay in existing classes. wemanmove (); movestring = wplay. stringMove; // The stringMove attribute is adapted to movestring in the existing class;} public override void jump () // adapt wemanplay to jump {wplay in the existing class. wemanjump (); jumpstring = wplay. stringJump; // The stringJump attribute is adapted to jumpstring in the existing class;} public class facadeFactory // The factory implementation of the appearance mode {public string movestring; public string jumpstring; List
Plays = new List
(); Public facadeFactory () {plays. add (new play1 (); plays. add (new play2 (); plays. add (new addplay ();} public void move () // appearance mode function 1: move two players together {foreach (play playt in plays) {playt. move (); movestring + = playt. movestring ;}} public void jump () // appearance mode function 2: two players team up and jump together {foreach (play playt in plays) {playt. jump (); jumpstring + = playt. jumpstring ;}}}}
The referenced mydll. dll here downloads the http://yunpan.cn/cfwfiu7KzAkiZ extraction code 3d67
How to create and reference a class library dll in c # In vs2010