上代码:
Person类
package com.wjy.adaptable.entity; import com.wjy.adaptable.adaptabler.MainOperator; public class Person implements IAdaptable{ public String name; public int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public Object getAdapter(Class adapter) { // TODO Auto-generated method stub if(adapter==MainOperator.class) return new MainOperator(this); return null; } }
person类的三个子类:
package com.wjy.adaptable.entity; import com.wjy.adaptable.adaptabler.AdultOperator; public class Man extends Person { public double height; public double weight; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public Object getAdapter(Class adapter) { // TODO Auto-generated method stub if(adapter==AdultOperator.class) return new AdultOperator(this); return null; } } package com.wjy.adaptable.entity; import com.wjy.adaptable.adaptabler.AdultOperator; public class Woman extends Person { public double height; public double weight; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public Object getAdapter(Class adapter) { // TODO Auto-generated method stub if(adapter==AdultOperator.class) return new AdultOperator(this); return null; } } package com.wjy.adaptable.entity; import com.wjy.adaptable.adaptabler.ChildOperator; import com.wjy.adaptable.adaptabler.MainOperator; public class Child extends Person { public double height; public double weight; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public Object getAdapter(Class adapter) { // TODO Auto-generated method stub if(adapter==ChildOperator.class) return new ChildOperator(this); return null; } }
IAdaptable接口:
package com.wjy.adaptable.entity; public interface IAdaptable { public abstract Object getAdapter(Class adapter); }
Result类:
package com.wjy.adaptable.entity; public class Result { public int age; public String name; public double height; public double weight; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } }
以下为为了实现适配器模式写的接口和类:
operateResult接口:
package com.wjy.adaptable.adaptabler; import com.wjy.adaptable.entity.Result; public interface operateResult { public abstract Result getPerson(); }
MainOperator类:
package com.wjy.adaptable.adaptabler; import com.wjy.adaptable.entity.Person; import com.wjy.adaptable.entity.Result; public class MainOperator implements operateResult{ Person person; public MainOperator(Person person) { this.person=person; } @Override public Result getPerson() { // TODO Auto-generated method stub return null; } }
MainOperator类的两个子类,其中AdultOperator类处理Woman和Man:
ChildOperator类处理Child:
package com.wjy.adaptable.adaptabler; import com.wjy.adaptable.entity.Man; import com.wjy.adaptable.entity.Person; import com.wjy.adaptable.entity.Result; import com.wjy.adaptable.entity.Woman; public class AdultOperator extends MainOperator{ public AdultOperator(Person person) { super(person); } @Override public Result getPerson() { Result result = new Result(); if(person instanceof Man) { result.name=person.name; result.age=person.age; result.height=((Man) person).height; result.weight=((Man) person).weight; } else if(person instanceof Woman) { result.name=person.name; result.age=person.age; result.height=((Woman) person).height; result.weight=((Woman) person).weight; } return result; } } package com.wjy.adaptable.adaptabler; import com.wjy.adaptable.entity.Child; import com.wjy.adaptable.entity.Person; import com.wjy.adaptable.entity.Result; import com.wjy.adaptable.entity.Woman; public class ChildOperator extends MainOperator { Result result=new Result(); public ChildOperator(Person person) { super(person); // TODO Auto-generated constructor stub } @Override public Result getPerson() { Child child=(Child)person; result.name=child.name; result.age=child.age; result.weight=child.weight; result.height=child.height; return result; } }
Main方法调用:
package com.wjy.adaptable.operator; import java.util.ArrayList; import java.util.List; import com.wjy.adaptable.adaptabler.AdultOperator; import com.wjy.adaptable.adaptabler.ChildOperator; import com.wjy.adaptable.entity.Child; import com.wjy.adaptable.entity.Man; import com.wjy.adaptable.entity.Person; import com.wjy.adaptable.entity.Woman; public class humanManager { public static void main(String[] args) { List<Person> persons=new ArrayList<>(); Woman woman=new Woman(); woman.age=21; woman.name="yhw"; woman.height=167.00; woman.weight=50.00; persons.add(woman); Man man=new Man(); man.age=21; man.name="wjy"; man.height=177.00; man.weight=74.00; persons.add(man); Child child=new Child(); child.name="xiaobu"; child.age=2; child.height=55.00; child.weight=88; persons.add(child); for (Person person : persons) { if(person instanceof Man || person instanceof Woman) { System.out.println((((AdultOperator)(person.getAdapter(AdultOperator.class))).getPerson()).name); } else if(person instanceof Child) { System.out.println((((ChildOperator)(person.getAdapter(ChildOperator.class))).getPerson()).name); } } } }
相关推荐
13适配器模式1
8. 适配器模式1
适配器(Adaper)角色public class Adapter extends Adaptee implements Target { /** * 由于源类
设计模式之适配器模式Java版 设计模式之适配器模式Java版 设计模式之适配器模式Java版 设计模式之适配器模式Java版
第3章 适配器模式提出问题问题描述:访问数据库,抽取并处理数据后以特定格式返回给用户。假设现有系统已有基础数据工具类 DataTool ,提供 processD
MyBatis适配器模式的体现在代码世界中适配器模式也经常使用,我们就拿MyBatis日志模块来说,MyBatis并没有提供日志的实现类,需要第三方日志组件,但
C#设计模式之适配器模式应用实例
java设计模式之适配器模式,希望对大家有所帮助。
适配器模式的Java实现版本,包含类适配器模式和对象适配器模式的实现,同时也包含两者的UML类图
动态代理模式 适配器模式 实现,都可以运行,有一部分注解
适配器模式代码demo 适配器模式代码demo 适配器模式代码demo
适配器模式模式说明当前有一工具(一个已存在的类),能实现若干功能(方法),需要在某平台(目标)上使用,但与平台的接口不同(已存在的类的方法与接口方法名不同),需
1. 要求设计抽象类Plotter,至少包含抽象方法plot(data, *args, **kwargs)方法,以期通过不同子类 2. 实现类PointPlot
1.当你希望使用某些现有类,但其接口与您的其他代码不兼容时,请使 2.当你希望重用几个现有的子类,这些子类缺少一些不能添加到超类中 1.符合单一职责原则 2.符
java设计模式【之】适配器模式【源码】【场景:水和容器】 * 适配器模式,类似(亡羊补牢) * 已经存在的类(功能已经开发完成了),与新需求的功能相似,但是略有差异 * 当已经存在的方法无法满足现有需求,需要...
适配器模式的介绍: 适配器模式(Adapter Pattern) 是作为两个不兼容接口之间的桥梁, 这种类型的设计模式属于结构型模式。 一些书籍也称适配器模式为缺省适配器模式(Default Adapter Pattern) 。 适配器模式主要...