Take a chestnut
Problem Description
People from different countries play in the NBA, but they communicate in English.
Simple implementation
Player
/** * Player * Created by callmeDevil on 2019/8/4. */ public abstract class Player { protected String name; public Player(String name){ this.name = name; } // Initiate an attack public abstract void attack(); // Turn back to defense public abstract void defense(); }
Forwards
/** * Forward * Created by callmeDevil on 2019/8/4. */ public class Forwards extends Player { public Forwards(String name){ super(name); } @Override public void attack() { System.out.println(String.format("Forward %s attack", name)); } @Override public void defense() { System.out.println(String.format("Forward %s Defense", name)); } }
Center
/** * Center-forward * Created by callmeDevil on 2019/8/4. */ public class Center extends Player { // The code is similar to that of the vanguard }
Guards
/** * guard * Created by callmeDevil on 2019/8/4. */ public class Guards extends Player { // The code is similar to that of the vanguard }
test
public class Test { public static void main(String[] args) { Player b = new Forwards("shane battier"); b.attack(); Player m = new Guards("tracy mcgrady"); m.attack(); Player ym = new Center("Yao Ming"); // Yao asked: What do attack and defense mean? ym.attack(); ym.defense(); } }
test result
Forward Battier Attack Defender McGrady Attacks Center Yao Ming Attack Center Yao Ming Defense
Existing problems
Yao Ming may not be good at English when he first arrived in the NBA, that is to say, he could not understand the tactical arrangements of the coach, attach and defense did not know what they meant, so there would be problems in this way, and a Chinese-English translation would be needed.
Adapter mode
Definition
Convert the interface of a class into another interface that the customer wants. The adapter pattern enables classes that could not work together due to incompatible interfaces to work together.
classification
It is mainly divided into class adapter mode and object adapter mode. Because the class adapter pattern matches one interface with another through multiple inheritance, and Java and other languages do not support multiple inheritance, that is, a class has only one parent class, so here we mainly talk about object adapter.
UML diagrams
code implementation
ForeignCenter
/** * Foreign Center * Created by callmeDevil on 2019/8/4. */ public class ForeignCenter { // The names of foreign center players are deliberately distinguished from those of the three players by attributes rather than constructive methods. private String name; // It shows that foreign center only knows Chinese "attack" (note: for example, in fact, do not program in this way). public void attack(){ System.out.println(String.format("Foreign Center %s attack", name)); } // It shows that foreign center only knows Chinese "attack" (note: for example, in fact, do not program in this way). public void Defense(){ System.out.println(String.format("Foreign Center %s Defense", name)); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Translator
/** * Translator * Created by callmeDevil on 2019/8/4. */ public class Translator extends Player{ // Declare and instantiate an internal "foreign center" object, indicating that the translator is associated with foreign players. private ForeignCenter wjzf = new ForeignCenter(); public Translator(String name){ super(name); wjzf.setName(name); } @Override public void attack() { // The translator translated "attack" into "attack" and told the foreign center. wjzf.attack(); } @Override public void defense() { // The translator translated "defense" into "defense" and told the foreign Center wjzf.Defense(); } }
test
public class Test { public static void main(String[] args) { Player b = new Forwards("shane battier"); b.attack(); Player m = new Guards("tracy mcgrady"); m.attack(); Player ym = new Translator("Yao Ming"); // The translator told Yao Ming that the coach asked you to "attack" and "defend". ym.attack(); ym.defense(); } }
test result
Forward Battier Attack Defender McGrady Attacks Foreign center Yao Ming attacked Foreign Center Yao Ming Defense
summary
- The data and behavior of the system are correct, but when the interface is inconsistent, we should consider using adapters to match an original object outside the scope of control with an interface.
- The adapter pattern is mainly applied to situations where you want to reuse some existing classes, but the interface is inconsistent with the reuse environment requirements.
- Use existing classes, but if its interfaces, that is, its methods, and your requirements do not coincide, you should consider using the adapter pattern.
- The two classes do the same or similar things, but use it when they have different interfaces.
- Customer code can call the same interface in a unified way, which can be simpler, more direct and more compact.
- Use adapter mode adaptation when both sides are not easy to modify.