Adaptor pattern Principle

Video geek college website at http://www.jikexueyuan.com/course/1335_4.html?ss=1

understand

1. Plugs and sockets

The plugs in each country fit the socket, but the plugs in each country are different, we need to charge when we travel, so we need a converter, one side can plug into the foreign socket, the other side can provide a fork for our plug

2. The cigarette lighter is converted into USB interface for mobile phone charging

The principle of

1. Passing a Turkey off as a duck

To the outside people, it is a duck, inherited from the duck interface, and the external image is the realization of a duck. But is called Turkey method and content, function and action are Turkey.

We wanted a duck to take on the characteristics of a Turkey

External image is a duck, but there are Turkey calls, flying distance

This is the object adapter method

1.Duck.java

  • Define methods

2.GreenDuck.java

  • Implement Dack implementation method

3.Turkey.java

  • Define methods

4.WildTurkey.java

  • Implement Turkey

5.TurkeyAdapter.java

  • External display is the image of the duck, so the interface is realized by the duck
  • External display as Turkey function, so pass Turkey object

6. Maintest.java (Test)

7. Run render

meaning

1. Convert the interface of a class to another interface so that the incompatible classes can be compatible

2. Users can not see the adaptor, can not see the things behind the adapter, decoupling

3. The user calls the target interface method transformed from the adapter (directly insert the GB plug into the adapter for use, without knowing which country’s socket is behind the converter)

4. The adapter invokes related interfaces and methods of the adaptor

Object adapters and class adapters

Multiple inheritance

Inherit the interface part of the target to achieve the purpose of adaptation (to present the image of the duck externally)

The part that inherits the adaptor class implements the interface function by calling the methods in the adaptor class.

The object is an adapter — a composite method

The Turkey example above

Class adapter – Inherited methods

Adaptation is achieved by multiple inheritance of the target interface and the method of the class being adapted

Because the class adapter is both extend and Implement, it is written differently from the object adapter in main

(Only 3 flights)

Adaptation from enumerator to iterator