标签:情况 pattern interface 相同 分享 存在 .com str 类型
1.定义:将一个类的接口转换成客户期望的另一个接口;使原本接口不兼容的类可以一起工作。
2.类型:结构型
3.适用场景:已经存在的类,它的方法和需求不匹配时(方法结果相同或相似);
不是软件设计阶段考虑的设计模式,是随着软件维护,由于不同产品、
不同厂家造成功能类似而接口不相同情况下的解决方案。
4.优点:能提高类的透明性和复用,现有的类复用但不需要改变;
目标类和适配器类解耦,提高程序扩展性;符合开闭原则。
5.缺点:适配器编写过程需要全面考虑,可能会增加系统的复用性;增加系统代码可读的难度。
6.扩展:对象适配器、类适配器。
7.相关设计模式:外观模式
8.实例目录package
9.实例UML类图
10.代码
1 package com.geely.design.pattern.structural.adapter; 2 3 public class AC220 { 4 public int outputACC220V(){ 5 int output = 220; 6 System.out.println("输出交流电" + output + "V"); 7 return output; 8 } 9 }
1 package com.geely.design.pattern.structural.adapter; 2 3 public interface DC5 { 4 int outputDC5V(); 5 }
1 package com.geely.design.pattern.structural.adapter; 2 3 public class PowerAdapter implements DC5{ 4 private AC220 ac220 = new AC220(); 5 @Override 6 public int outputDC5V() { 7 int adapterInput = ac220.outputACC220V(); 8 int adapterOutput = adapterInput / 44; 9 System.out.println("使用PowerAdapter输入AC: " + adapterInput + "V" + "输出DC: " + adapterOutput + "V"); 10 return adapterOutput; 11 } 12 }
1 package com.geely.design.pattern.structural.adapter; 2 3 public class Test { 4 public static void main(String[] args) { 5 DC5 dc5 = new PowerAdapter(); 6 dc5.outputDC5V(); 7 } 8 }
类适配器
1 package com.geely.design.pattern.structural.adapter.classadapter; 2 3 public class Adaptee { 4 public void adapterRequest(){ 5 System.out.println("被适配者的方法"); 6 } 7 }
1 package com.geely.design.pattern.structural.adapter.classadapter; 2 3 public interface Target { 4 void request(); 5 }
package com.geely.design.pattern.structural.adapter.classadapter; public class Adapter extends Adaptee implements Target { @Override public void request() { super.adapterRequest(); } }
1 package com.geely.design.pattern.structural.adapter.classadapter; 2 3 public class ConcreteTarget implements Target { 4 @Override 5 public void request() { 6 System.out.println("concreateTarget目标方法"); 7 } 8 }
1 package com.geely.design.pattern.structural.adapter.classadapter; 2 3 public class Test { 4 public static void main(String[] args) { 5 Target target = new ConcreteTarget(); 6 target.request(); 7 8 Target adapterTarget = new Adapter(); 9 adapterTarget.request(); 10 } 11 }
对象适配器
package com.geely.design.pattern.structural.adapter.objectadapter; public class Adaptee { public void adapterRequest(){ System.out.println("被适配者的方法"); } }
1 package com.geely.design.pattern.structural.adapter.objectadapter; 2 3 public interface Target { 4 void request(); 5 }
1 package com.geely.design.pattern.structural.adapter.objectadapter; 2 3 public class Adapter implements Target { 4 private Adaptee adaptee = new Adaptee(); 5 @Override 6 public void request() { 7 adaptee.adapterRequest(); 8 } 9 }
1 package com.geely.design.pattern.structural.adapter.objectadapter; 2 3 public class ConcreteTarget implements Target { 4 @Override 5 public void request() { 6 System.out.println("concreateTarget目标方法"); 7 } 8 }
1 package com.geely.design.pattern.structural.adapter.objectadapter; 2 3 public class Test { 4 public static void main(String[] args) { 5 Target target = new ConcreteTarget(); 6 target.request(); 7 8 Target adapterTarget = new Adapter(); 9 adapterTarget.request(); 10 } 11 }
标签:情况 pattern interface 相同 分享 存在 .com str 类型
原文地址:https://www.cnblogs.com/linbq1911/p/10229153.html