适配器模式

适配器模式

              适配器模式 关键就在在在于一个转换,举例;中国的用电都在220v ,日本,美国都是110v 的,中国的家用电器到国外如何使用呢?国外的电器到中国又如何使用呢,这里就需要一个转换器-也叫适配器,看实例代码吧。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Patten001
{
   public interface ITarget
    {
        void Request();
    }

    public class Target : ITarget
    {
        public void Request()
        {

            Console.WriteLine("客户想要的方法");
        }
    }

    public class Adaptee
    {
        //没有request 这方法;
        public void doOther()
        {
            Console.WriteLine("do...other..");
        }

        public void SpecificRequst()
        {
            Console.WriteLine("called specificRequsted()");
        }
    }

    public class Adapter : Adaptee, ITarget
    {
        public void Request()
        {
            this.SpecificRequst();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ITarget t = new Adapter();
            t.Request();

        }

        public void Info()
        {
            //将一个类的借口转换成客户端希望的另一个借口;
            //模式使得原本由于接口不兼容而不能在一起工作的类可以再一起工作的呀;

            //使用场景;已经存在的接口不能符合我们的需求;
            //创建一个复用的类,使得该类可以与其他不相关类或不可预见的类;
            //即那些接口可能不一定兼容;


        }
    }

   
}

 

posted @ 2015-09-21 14:51  咕-咚  阅读(205)  评论(0)    收藏  举报