《设计模式》7.适配器模式(结构型)

作用

将已知类的接口通过适配器转换成用户所期待的接口,让那些接口不兼容的类可以一起工作。

角色

  • 目标接口(Target):用户所期待的接口
  • 适配者(Adaptee):已知类,包含需要通过适配器转换的接口
  • 适配器(Adapter):用于将已知类接口转换为用户期待接口的

类适配器

通过类继承实现,将目标类(DuridDataSource)接口(#executeQuery、#executeSubmit)通过适配器类(DataSourceAdapter)封装到(#get、#set)中供用户调用。

public interface Adapter {
    Object get(String sql);
    Object set(String sql);
}
public class DuridDataSource {
    public Object executeQuery(String sql) {
        return new String("Query: " + sql);
    }

    public Object executeSubmit(String sql) {
        return new String("Submit: " + sql);
    }
}
public class DataSourceAdapter extends DuridDataSource implements Adapter {
    @Override
    public Object get(String sql) {
        return executeQuery(sql);
    }

    @Override
    public Object set(String sql) {
        return executeSubmit(sql);
    }
}
public class DataSourceAdapterTester {
    public static void main(String args[]) {
        DataSourceAdapter adapter = new DataSourceAdapter();
        Object queryResult = adapter.get("select * from table");
        System.out.println(queryResult);
        Object updateResult = adapter.set("update table set column1=value1 where id=1");
        System.out.println(updateResult);
    }
}
Query: select * from table
Submit: update table set column1=value1 where id=1

对象适配器

通过持有目标类实例对象,用户调用适配器类(DataSourceAdapter)适配接口(#get、#set)调用目标类(DuridDataSource)实例接口(#executeQuery、#executeSubmit)。

public interface Adapter {
    Object get(String sql);
    Object set(String sql);
}
public class DataSourceAdapter implements Adapter {
    private DuridDataSource dataSource;
    public DataSourceAdapter(DuridDataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public Object get(String sql) {
        return dataSource.executeQuery(sql);
    }

    @Override
    public Object set(String sql) {
        return dataSource.executeSubmit(sql);
    }
}
public class DuridDataSource {
    public Object executeQuery(String sql) {
        return new String("Query: " + sql);
    }

    public Object executeSubmit(String sql) {
        return new String("Submit: " + sql);
    }
}
public class DataSourceAdapterTester {
    public static void main(String args[]) {
        DataSourceAdapter adapter = new DataSourceAdapter(new DuridDataSource());
        Object queryResult = adapter.get("select * from table");
        System.out.println(queryResult);
        Object updateResult = adapter.set("update table set column1=value1 where id=1");
        System.out.println(updateResult);
    }
}
Query: select * from table
Submit: update table set column1=value1 where id=1

接口适配器

当接口(Adapter)定义了多个方法,仅希望提供用户部分接口时,通过抽象类(Animal)实现所有接口,并且不做任何操作,在具体实现类(Bird、Dog、Fish)中通过继承实现抽象类(Animal)需要提供给用户的接口。

public interface Adapter {
    void fly();
    void swim();
    void run();
}
public abstract class Animal implements Adapter {
    @Override
    public void fly() {}

    @Override
    public void swim() {}

    @Override
    public void run() {}
}
public class Bird extends Animal {
    @Override
    public void fly() {
        System.out.println("Bird can fly");
    }
}
public class Dog extends Animal {
    @Override
    public void run() {
        System.out.println("Dog can run");
    }
}
public class Fish extends Animal {
    @Override
    public void swim() {
        System.out.println("fish can swim");
    }
}
public class AnimalAdapterTester {
    public static void main(String args[]) {
        Adapter bird = new Bird();
        bird.fly();
        bird.swim();
        bird.run();

        Adapter fish = new Fish();
        fish.swim();
        fish.fly();
        fish.run();

        Adapter dog = new Dog();
        dog.run();
        dog.fly();
        dog.swim();
    }
}
Bird can fly
fish can swim
Dog can run
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值