Design Pattern Twelve: Adapter Pattern

      Adapter Pattern (Adapter Pattern) is also known as Transformer Pattern or Packaging Pattern:

      Definition: Convert the interface of a class into another interface clints expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. (Programming the interface of one class to another interface that clients expect allows two classes to work together that would otherwise not work together due to interface mismatches.)


      General class diagram:

    

     Target target role: This role defines other classes and converts them into the classes or interfaces we expect

      Adaptee source role: Who do you want to turn into a target role, this "who" is the source role

      Adapter role: the core role of the adapter, the other two roles are existing roles, and the adapter role needs to be newly created, its responsibility is very simple, that is, to convert the source role into the target role through inheritance or class association

      

      advantage

  1. Through the adapter, the client can call the same interface, so it is transparent to the client. It's simpler, more straightforward, and more compact to do so.

  2. The existing classes are reused, which solves the problem of inconsistency between the existing classes and the requirements of the reuse environment.

  3. Decouple the target class and the adapter class, and reuse the existing adapter class by introducing an adapter class without modifying the original code.

  4. An object adapter can adapt multiple different adapter classes to the same target, that is to say, the same adapter can adapt both the adapter class and its subclasses to the target interface.

  shortcoming

  For object adapters, the implementation process of replacing the adapter is more complicated.

    

 //机器人类
    abstract public class Robot
    {
        abstract public void RobotCry();
        abstract public void RobotMove();
        abstract public void SetAdaptee(object Value); //这么申明带有一个参数的抽象方法。
    }
    //抽象的Adaptee
    public class AnimalAdaptee
    {
        public virtual void Cry() { }
        public virtual void Move() { }    
    }

    //不同种类的动物
    //狗
    public class DogAdaptee : AnimalAdaptee
    {
        public override void Cry()
        {
            //base.Cry();
            Console.WriteLine("这是狗叫:汪汪汪!");
        }
        public override void Move()
        {
            //base.Move();
            Console.WriteLine("这是狗跳:跳跳跳!");
        }
    }
    //鸟
    public class BirdAdaptee : AnimalAdaptee
    {
        public override void Cry()
        {
            //base.Cry();
            Console.WriteLine("这是鸟叫:叽叽叽!");
        }
        public override void Move()
        {
            //base.Move();
            Console.WriteLine("这是鸟飞:快快飞!");
        }
    }

    //适配器类Adaptor
    public class RobotAdapptor : Robot
    {
        private AnimalAdaptee _myAdaptee;
        public AnimalAdaptee MyAdaptee
        {
            get { return _myAdaptee; }
            set { _myAdaptee = value; }
        }
        public override void SetAdaptee(Object Value)
        {
            _myAdaptee = (AnimalAdaptee)Value;
        }
        public override void RobotCry()
        {
            //throw new NotImplementedException();
            if (_myAdaptee == null) _myAdaptee = new AnimalAdaptee();
            _myAdaptee.Cry();
        }
        public override void RobotMove()
        {
            //throw new NotImplementedException();
            if (_myAdaptee == null) _myAdaptee = new AnimalAdaptee();
            _myAdaptee.Move();
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Robot MyRobot = new RobotAdapptor();
            AnimalAdaptee dogAdaptee = new DogAdaptee();

            MyRobot.SetAdaptee(dogAdaptee);
            MyRobot.RobotCry();
            MyRobot.RobotMove();

            //申明为第二种鸟类
            AnimalAdaptee birdAdaptee=new BirdAdaptee();
            MyRobot.SetAdaptee(birdAdaptee);
            MyRobot.RobotCry();
            MyRobot.RobotMove();

            Console.ReadKey();
        }
    }
Well, that's all for this chapter. Welcome everyone to join the QQ group: 280993838. Or pay attention to my official account:

Guess you like

Origin blog.csdn.net/zjw1349547081/article/details/54943799