最近在学设计模式,感觉有点意思 刚好今天老师让我们用适配器模式 实现一下 赵高的指鹿为马。
下面是java代码
package com.adapter;
public interface Horse {
public void quackYI();
public void run();
}
package com.adapter;
public class WhiteHorse implements Horse {
public void quackYI() {
System.out.println("WhiteHorse......在叫.....");
}
public void run() {
System.out.println("WhiteHorse......奔跑中.....");
}
}
package com.adapter;
public interface Miller {
public void quackHHHI();
public void run();
}
package com.adapter;
public class WhiteMiller implements Miller {
public void quackHHHI() {
System.out.println("WhiteMiller......在嘶吼.....");
}
public void run() {
System.out.println("WhiteMiler......慢跑中.....");
}
}
package com.adapter;
public class ZhaoGaoMillerAdapter implements Horse {
Miller miller;
ZhaoGaoMillerAdapter (Miller miller){
this.miller=miller;
}
public void quackYI() {
miller.quackHHHI();
}
public void run() {
for(int i=0;i<3;i++){
miller.run();
}
}
}
package com.adapter;
public class AdapterTest {
public static void main(String[] args) {
Horse horse= new WhiteHorse();
Miller miller = new WhiteMiller();
Horse adapter = new ZhaoGaoMillerAdapter(miller);
System.out.println("Horse...says:");
horse.quackYI();
horse.run();
System.out.println("\n Miller...says:");
miller.quackHHHI();
miller.run();
System.out.println("\n MilerAdapter...says:");
adapter.quackYI();
adapter.run();
}
}