设计模式——适配器模式(7)

//SD卡的接口 public interface SDCard { //读取SD卡方法 String readSD(); //写入SD卡功能 void writeSD(String msg); } //SD卡实现类 public class SDCardImpl implements SDCard { public String readSD() { String msg = "sd card read a msg :hello word SD"; return msg; } public void writeSD(String msg) { System.out.println("sd card write msg : " + msg); } } public class TFCardImpl implements TFCard { public String readTF() { String msg ="tf card read msg : hello word tf card"; return msg; } public void writeTF(String msg) { System.out.println("tf card write a msg : " + msg); } } public class SDAdapterTF implements SDCard { private TFCard tfCard; public SDAdapterTF(TFCard tfCard) { this.tfCard = tfCard; } public String readSD() { System.out.println("adapter read tf card "); return tfCard.readTF(); } public void writeSD(String msg) { System.out.println("adapter write tf card"); tfCard.writeTF(msg); } } public class Client { public static void main(String[] args) { Computer computer = new Computer(); SDCard sdCard = new SDCardImpl(); System.out.println(computer.readSD(sdCard)); System.out.println("------------"); TFCard tfCard = new TFCardImpl(); SDAdapterTF adapter = new SDAdapterTF(tfCard); System.out.println(computer.readSD(adapter)); } }
上一篇:群晖前面加了雷池WAF,安装失败,然后无法识别出用户真实访问IP


下一篇:SQL自学:存储过程的理解、应用与语法