適配器模式

Adapter_Model

期望的接口

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

namespace 適配器模式
{
    //這是客戶所期待的接口。目標可以是具體的或者抽象的類。也可以是接口
    class Target
    {
        public virtual void Request()
        {
            Console.WriteLine("普通請求!!");
        }
    }
}

需要適配的類

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

namespace 適配器模式
{
    //需要適配的類
    class Adaptee
    {
        public void SpecificRequest()
        {
            Console.WriteLine("特殊請求!");
        }
    }
}

適配器

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

namespace 適配器模式
{
    class Adapter:Target
    {
        private Adaptee adaptee = new Adaptee();
        public override void Request()
        {
            adaptee.SpecificRequest();
        }
    }
}

客戶端

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

namespace 適配器模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Target target = new Target();
            Target adapter = new Adapter();
            adapter.Request();
            target.Request();

            Console.ReadKey();

        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章