首页 > 代码库 > 适配器模式(adapter)c#

适配器模式(adapter)c#

适配器模式(adapter)c#简单例子

结合上回外观模式中的类,从外部引入mydll.dll类库中wemanplay类,其接口有属性stringMove 、 stringJump和行为wemanmove()、wemanjump(),采用适配器变为自己类中通用接口。

在外观模式中增加一个新类适配器

 public class addplay : play//由引类wemanplay类适配为现有play类
    {
        private wemanplay wplay;//引个已编译好的wemanplay类
        public   addplay()
        {
            wplay = new wemanplay();
        }
        public override void move()//将wemanplay类中wemanmove适配为现有类中move
        {
            wplay.wemanmove();
            movestring = wplay.stringMove; //属性stringMove适配为现有类中movestring;

        }
        public override void jump()//将wemanplay类中wemanjump适配为现有类中jump
        {
            wplay.wemanjump();
            jumpstring = wplay.stringJump;//属性stringJump适配为现有类中jumpstring;
        }
所有代码如下:

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;//添加引用wemanplay类

namespace adapterpattern
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            facadeFactory ff = new facadeFactory();//外观模式隐藏了玩家,用户不知道有几个玩家
            ff.move();//实现外观功能1
            this.listBox1.Items.Add(ff.movestring);
            ff.jump();//实现外观功能2
            this.listBox1.Items.Add(ff.jumpstring);
        }

    }


    public abstract class play//对所有玩家进行抽象
    {
        public string movestring { get; set; }
        public string jumpstring { get; set; }
        public abstract void move();
        public abstract void jump();
    }
    public class play1 : play//玩家1
    {
        public override void move()
        {
            movestring = "play1 move";
        }
        public override void jump()
        {
            jumpstring = "play1 jump";
        }
    }
    public class play2 : play//玩家2
    {
        public override void move()
        {
            movestring = "play2 move";
        }
        public override void jump()
        {
            jumpstring = "play2 jump";
        }
    }
    public class addplay : play//由引类wemanplay类适配为现有play类
    {
        private wemanplay wplay;
        public   addplay()
        {
            wplay = new wemanplay();
        }
        public override void move()//将wemanplay类中wemanmove适配为现有类中move
        {
            wplay.wemanmove();
            movestring = wplay.stringMove; //属性stringMove适配为现有类中movestring;

        }
        public override void jump()//将wemanplay类中wemanjump适配为现有类中jump
        {
            wplay.wemanjump();
            jumpstring = wplay.stringJump;//属性stringJump适配为现有类中jumpstring;
        }
    }
    public class facadeFactory//外观模式工厂实现
    {
        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()//外观模式功能一:两个玩家组队一起移动
        {
            foreach (play playt in plays)
            {
                playt.move();
                movestring += playt.movestring;
            }
        }
        public void jump()//外观模式功能二:两个玩家组队一起跳跃
        {
            foreach (play playt in plays)
            {
                playt.jump();
                jumpstring += playt.jumpstring;
            }
        }
    }
}


文中引用的mydll.dll在这里下载http://yunpan.cn/cfwfiu7KzAkiZ  提取码 3d67

 

vs2010中c#中类库dll制作和引用方法


适配器模式(adapter)c#