适配器模式1
来源:互联网 发布:php后台密码破解工具 编辑:程序博客网 时间:2024/04/29 22:18
上代码:
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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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); } }}}
0 0
- 适配器模式1
- java适配器模式1
- 适配器模式1
- 设计模式-适配器模式(1)
- 设计模式之适配器(1)
- 适配器及适配器模式
- 适配器及适配器模式
- 适配器模式(类适配器)
- 适配器模式(默认适配器)
- 适配器模式(对象适配器)
- 适配器模式-类适配器
- 适配器模式-对象适配器
- 设计模式1-适配器模式(Adapter)
- 结构型模式(1)---适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- java类的一些特殊效果
- 2013/05/08 学习笔记
- java for语句的一个用法
- 利用二级指针删除单向链表
- 单例模式 Singleton
- 适配器模式1
- jcl错误集锦
- 组合模式 Composite
- js控制div弹出层
- JCL实用程序
- 接口的理解
- java中Class.forName的含义
- 用适配器模式解决接口的不足
- JDesktopPane加背景图片