アダプタモード1
24141 ワード
上のコード:
Personクラス
Personクラスの3つのサブクラス:
IAdaptableインタフェース:
Resultクラス:
次に、アダプタ・モードの書き込みを実現するインタフェースとクラスを示します.
operateResultインタフェース:
MainOperatorクラス:
MainOperatorクラスの2つのサブクラスで、AdultOperatorクラスはWomanとMan:ChildOperatorクラスがChildを処理します.
Mainメソッド呼び出し:
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クラスの3つのサブクラス:
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クラスの2つのサブクラスで、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);
}
}
}
}