Java 设计模式-适配器模式(Adapter)

       适配器模式,从名字就可以很好的理解该模式。 我们平时使用的电子产品的适配器,就是将我们的生活电压220v 转换为电子产品使用的5V电压。 原本220V电压的生活用电是没有办法作用在电子产品上的,可是我们增加了一个适配器后就可以完美的解决了这个问题。

   适配器模式(Adapter Pattern )就是把一个类的接口变换成用户期待的另一种接口,从而使原本因接口不匹配无法在一起工作的两个类可以在一起工作。

    适配器模式根据适配的类型可以分为接口适配和类与对象适配。

    接口适配: Target 是一个接口,Adaptee 是以及已经存在的类 Adapter 则需要实现Target接口同时需要继续已经存在的类。

    类与对象适配:通过继承Target类,利用现有的类满足所需要的方法。

/**
 * Apatee ,需要被适配的接口
 *  生活电压接口
 * @author zhangwei
 * @version $Id: LivingElectric.java, v 0.1 2014年9月23日 下午10:23:55 zhangwei Exp $
 */
public interface LivingElectric {

    /**
     * 返回生活用电的电压
     *
     * @return
     */
    public int getLivingElectric();

}
/**
 * Target: 这个是笔记本期待的电压接口
 * 笔记本的电压接口
 * @author zhangwei
 * @version $Id: NoteElectric.java, v 0.1 2014年9月23日 下午10:26:44 zhangwei Exp $
 */
public interface NoteElectric {

    /**
     * 获取笔记本的电压
     *
     * @return
     */
    public int getNoteElectric();

}
/**
 *Adapter 将生活电压转换为笔记本需要的电压
 * @author Lenovo
 * @version $Id: ElectricAdapter.java, v 0.1 2014年9月23日 下午10:29:16 Lenovo Exp $
 */
public class ElectricAdapter implements NoteElectric {

    private LivingElectric livingElectric;

    /**
     * @see com.cathy.demo.pattern.adapter.NoteElectric#getNoteElectric()
     */
    public int getNoteElectric() {

        return livingElectric.getLivingElectric() / 40;

    }

    public ElectricAdapter(LivingElectric livingElectric) {
        super();
        this.livingElectric = livingElectric;
    }

}
/**
 * 笔记本类
 * @author Lenovo
 * @version $Id: Note.java, v 0.1 2014年9月23日 下午10:29:34 Lenovo Exp $
 */
public class Note {
    NoteElectric power;

    public void setPower(NoteElectric noteElectric) {
        power = noteElectric;
    }

    public void use() {
        if (power.getNoteElectric() > 7 || power.getNoteElectric() <= 0) {
            throw new IllegalArgumentException("电压不匹配,无法工作");
        }
        System.out.println(MessageFormat.format("当前电压{0}V-符合工作条件开始工作", power.getNoteElectric()));
    }
}

 

/**
 *
 * @author zhangwei
 * @version $Id: Client.java, v 0.1 2014年9月23日 下午10:33:13 Lenovo Exp $
 */
public class Client {
    public static void main(String[] args) {
        Note note = new Note();
        // 连接电源
        note.setPower(new ElectricAdapter(new LivingElectric() {

            /**
             * 返回生活电压220V
             * @see com.cathy.demo.pattern.adapter.LivingElectric#getLivingElectric()
             */
            public int getLivingElectric() {
                return 220;
            }
        }));
        // 使用笔记本
        note.use();
    }
}

 

      运行的结果是:当前电压5V-符合工作条件开始工作

 

    

/**
 *
 * @author zhangwei
 * @version $Id: ChinaLivingElectirc.java, v 0.1 2014年9月24日 上午9:03:03 Lenovo Exp $
 */
public class ChinaLivingElectirc implements LivingElectric {

    /**
     * @see com.cathy.demo.pattern.adapter.LivingElectric#getLivingElectric()
     */
    public int getLivingElectric() {
        return 220;
    }

}
/**
 *
 * @author Lenovo
 * @version $Id: Adapter.java, v 0.1 2014年9月24日 上午9:02:10 Lenovo Exp $
 */
public class Adapter extends ChinaLivingElectirc implements NoteElectric {

    /**
     * @see com.cathy.demo.pattern.adapter.NoteElectric#getNoteElectric()
     */
    public int getNoteElectric() {
        return getLivingElectric() / 40;
    }

}
/**
 *
 * @author zhangwei
 * @version $Id: Client.java, v 0.1 2014年9月23日 下午10:33:13 Lenovo Exp $
 */
public class Client {
    public static void main(String[] args) {

        //
        Note note = new Note();
        // 连接适配器
        note.setPower(new Adapter());
        // 开始工作
        note.use();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值