适配器模式

适配器模式(包装模式)
特点:把一个类的接口变成客户端所期待的另一种接口,从而使无法一起工作的类可以一起工作
      强调代码的组织而不是实现,体现了优先使用组合而不是继承

示例代码:
namespace Adapter
{
    /// <summary>
    ///
    /// </summary>
    public class bird
    {
        public bird()
        {
        }

        public void Fly()
        {
            Console.WriteLine("I can fly !");
        }
    }
}

namespace Adapter
{
    interface ITigger
    {
        void Eat();
    }
}

namespace Adapter
{
    class Tiger
    {
        public string Name { set; get; }

        public Tiger() { }
        public Tiger(string name)
        {
            this.Name = name;
        }

        public void Eat()
        {
            Console.WriteLine("I can eat !");
        }
    }
}


namespace Adapter
{
    class Program
    {
        static void Main(string[] args)
        {
            ITigger ft = new FlyTigger();
            ft.Eat();
        }
    }
}

posted @ 2010-11-07 15:45  星空有我  阅读(262)  评论(2编辑  收藏  举报