What is the adaptation class for java-7.6 adaptation design patterns? Or the adaptation interface?
In this chapter, let's discuss the adaptation issue?
To put it bluntly, you can use any object to call a method, and this object only needs to comply with certain protocols.
The implementation of this protocol has two methods: inheritance and interface, but interfaces are generally used, because their code reusability is better than inheritance, and inheritance itself also contains many implementations of the parent class, cause unnecessary confusion. In addition, we can implement multiple interfaces (similar to multiple inheritance) through interfaces)
The following code compares the two groups:
package com.ray.ch07;class father {private int id = 0;private void sleep() {}public int run(int speed) {return speed;}}public class Test extends father {@Overridepublic int run(int speed) {// TODO Auto-generated method stubreturn super.run(speed);}}
Through inheritance, it is generally like the above Code. Test inherits from father, and the test object can only be transformed to a father class. If multiple inheritance is required, the hierarchy of inheritance may increase suddenly.
Let's take a look at the interface:
package com.ray.ch07;interface Son {void sleep();int run(int speed);}interface Mother {void eat();}public class Test implements Son, Mother {@Overridepublic void eat() {// TODO Auto-generated method stub}@Overridepublic void sleep() {// TODO Auto-generated method stub}@Overridepublic int run(int speed) {// TODO Auto-generated method stubreturn 0;}public static void main(String[] args) {Mother mother = new Test();mother.eat();Son son = new Test();son.sleep();son.run(2);}}
In the above Code, Test can easily be transformed to Son and Mother classes. From the perspective of polymorphism, using interfaces to adapt to code greatly increases reusability and code is more flexible.
Summary: This chapter mainly discusses which one is used in the adaptation implementation. We recommend that you use the interface, because it brings more flexibility and code reusability.