适配模式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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值