1. Class adapter mode
When I first came here in the morning and saw my brother drinking water, I suddenly didn't feel very good. I said: the weather is a little gloomy today. It's estimated to rain again. It's wet everywhere. Even the design mode I just learned feels like water injection!
My brother glanced at me and said: what do you say?
I said: do you know the class adapter mode? I think you know that it is an adapter built through the relationship between classes;
It consists of three parts: existing resources, adaptive method interface and target resources. The existing resource takes the target resource as the parent class, and calls the properties or methods of the parent class through the methods that implement the interface. So as to realize the operation of existing resources and target resources. Show you a picture!
Look at the picture and speak: from the perspective of relationship, Dagu inherited diga. Dagu has a transformation device. Dagu can't beat monsters. Diga can beat monsters. When Dagu wants to fight monsters, he needs to open the transfiguration device to call various abilities of diga to fight monsters.
/** * Diga Altman * * @author czy * @date 2021/6/18 */ public class UltramanTiga { public void action(){ System.out.println("Diga uses cosmic rays to hit the cosmic Little King Kong"); System.out.println("Dega deals 100 damage to the monster"); } } /** * Dagu * Dagu needs a shapeshifter to change into diga * * @author czy * @date 2021/6/18 */ public class BigGu extends UltramanTiga implements ShapeShifter{ @Override public void becomeBigPerson() { this.action(); } } /** * Shaper * * @author czy * @date 2021/6/18 */ public interface ShapeShifter { /** * Become a giant */ void becomeBigPerson(); } /** * Come and watch Altman turn into a monster * * @author czy * @date 2021/6/18 */ public class Test { public static void main(String[] args) { //I have a big ancient. I want to fight monsters BigGu bigGu = new BigGu(); //I want a diga bigGu.becomeBigPerson(); } }
Brother: inheritance coupling is a little high! It should not be applicable in actual production.
Me: Yes, inheriting the parent class has high adaptability and coupling
I: Yes, it is right to inherit the parent class and call the parent class method. This mode also needs to make an unnecessary move through the interface to realize the call of the parent class interface through the implementation of the interface method. It is entirely in the water experience, so I say it is watering.
2. Object adaptation mode
Brother: is there a good solution?
I: just change inheritance into combination. Everyone has Dega in his body and needs to inspire him through the light of hope. This is the object adaptation mode. Kaka rabbit!!!
/** * Diga Altman ultimate * * @author czy * @date 2021/6/18 */ public class UltramanTiga { public void action(){ System.out.println("Diga uses cosmic rays to hit the cosmic Little King Kong"); System.out.println("Dega deals 100 damage to the monster"); } } /** * everybody * Everyone's faith has Dega in his heart. He operates Dega to fight monsters through the light of hope * @author czy * @date 2021/6/21 */ public class EveryBody implements Light{ private UltramanTiga ultramanTiga; public EveryBody(UltramanTiga ultramanTiga) { this.ultramanTiga = ultramanTiga; } @Override public void becomeBigPerson() { ultramanTiga.action(); } } /** * Light of hope * * Adapter interface * * @author czy * @date 2021/6/21 */ public interface Light { /** * Become a giant */ void becomeBigPerson(); } /** * Everyone is diga * * @author czy * @date 2021/6/21 */ public class Test { public static void main(String[] args) { EveryBody body = new EveryBody(new UltramanTiga()); body.becomeBigPerson(); } }
You see, multiple interfaces increase the amount of code here! Is it water!
Brother: really water! It can be implemented directly without interface!
I snickered in my heart and looked as steady as an old dog: This is interface oriented programming, Sao Nian! Have you forgotten the seven principles? It depends on the inversion principle. It's interface oriented rather than implementation oriented, so I'm watering!