首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

适配形式Adapter :类的适配器模式

2012-09-14 
适配模式Adapter :类的适配器模式Adapter.javapackage com.test2/** * 适配器 ** 基本适配模式: * 是在一

适配模式Adapter :类的适配器模式

Adapter.java

package com.test2;/** * 适配器 *  * 基本适配模式: * 是在一个实现类(Adaptee)的基础上,扩展出一个新的适配器(Adapter必须是class),这个适配器必须包含这个先前Adaptee功能,也要提供出新接口的功能。 *  * 生活中的例子: * 一个插头(Adaptee)功能已经固定,比如:二相插座,现在我们有个三相插头(Target)需要使用,需要在此基础上扩展一个转换插座(Adapter),但是也能保证先前二相插座能使用。 * @author tianjun tkf36897 * @version 1.0 Dec 27, 2010 */public class Adapter extends Adaptee implements Target {    public void sampleOperation2(){        //具体业务    }        }class test{    public static void main(String[] args)    {        Adapter temp = new Adapter();                //在不改变Adaptee的情况下,2种方法都可以使用        temp.sampleOperation1();        temp.sampleOperation2();    }}

?

Adaptee.java

package com.test2;/** * 已经实现的功能 * * @author tianjun tkf36897 * @version 1.0 Dec 27, 2010 */public class Adaptee{    public void sampleOperation1()    {        //具体业务    }}

?

Target.java

package com.test2;/** * 需要扩展出来的功能 * * @author tianjun tkf36897 * @version 1.0 Dec 27, 2010 */public interface Target {    void sampleOperation1();    void sampleOperation2();}

热点排行