Java设计模式之适配器模式

本文详细介绍了适配器模式在Java编程中的应用,包括类适配器和对象适配器的具体实现,以及如何在客户端代码中使用适配器来解决不同组件之间的兼容性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


类适配器:

package com.hycz.design.pattern.Adapter.ClassAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class Adaptee {

    public void specificRequest() {
        System.out.println("被适配类具有 特殊功能...");
    }
}

package com.hycz.design.pattern.Adapter.ClassAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:51 PM
 * To change this template use File | Settings | File Templates.
 */
public class Adapter extends Adaptee implements Target {
    @Override
    public void request() {
        //To change body of implemented methods use File | Settings | File Templates.
        this.specificRequest();
    }
}

package com.hycz.design.pattern.Adapter.ClassAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:49 PM
 * To change this template use File | Settings | File Templates.
 */
public interface Target {
    // 目标接口,或称为标准接口

    public void request();
}

package com.hycz.design.pattern.Adapter.ClassAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class ConcreteTarget implements Target {
    @Override
    public void request() {
        //To change body of implemented methods use File | Settings | File Templates.
        System.out.println("普通类 具有 普通功能...");
    }
}

package com.hycz.design.pattern.Adapter.ClassAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:52 PM
 * To change this template use File | Settings | File Templates.
 */
public class Client {

    public static void main(String[] args) {
        // 使用普通功能类
        Target concreteTarget = new ConcreteTarget();
        concreteTarget.request();

        // 使用特殊功能类,即适配类
        Target adapter = new Adapter();
        adapter.request();
    }
}

对象适配器:

package com.hycz.design.pattern.Adapter.ObjectAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:49 PM
 * To change this template use File | Settings | File Templates.
 */
public interface Target {
    // 目标接口,或称为标准接口

    public void request();
}

package com.hycz.design.pattern.Adapter.ObjectAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class ConcreteTarget implements Target {
    @Override
    public void request() {
        //To change body of implemented methods use File | Settings | File Templates.
        System.out.println("普通类 具有 普通功能...");
    }
}

package com.hycz.design.pattern.Adapter.ObjectAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class Adaptee {

    public void specificRequest() {
        System.out.println("被适配类具有 特殊功能...");
    }
}

package com.hycz.design.pattern.Adapter.ObjectAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:51 PM
 * To change this template use File | Settings | File Templates.
 */
public class Adapter implements Target {

    Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void request() {
        //To change body of implemented methods use File | Settings | File Templates.
        adaptee.specificRequest();
    }
}

package com.hycz.design.pattern.Adapter.ObjectAdapter;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/14/13
 * Time: 5:52 PM
 * To change this template use File | Settings | File Templates.
 */
public class Client {

    public static void main(String[] args) {
        // 使用普通功能类
        Target concreteTarget = new ConcreteTarget();
        concreteTarget.request();

        // 使用特殊功能类,即适配类
        Target adapter = new Adapter(new Adaptee());
        adapter.request();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值