`

适配器模式1

 
阅读更多

上代码:

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);
        }
    }
}
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics