java设计模式(六)适配器模式

定义

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。简而言之,将一个类的接口转换成客户希望的另外一个接口。

分类

适配器模式有分三类:
1、类适配器模式(class adapter pattern)
2、对象适配器模式(object adapter pattern)
3、缺省适配器模式(default adapter pattern),也叫默认适配器模式、接口适配器模式

角色职责

(1)目标抽象类:Target,该角色把其他类转换为我们期望的接口,可以是一个抽象类或接口,也可以是具体类。
(2)被适配者: Adaptee ,原有的接口,也是希望被适配的接口。
(3)适配器: Adapter, 将被适配者和目标抽象类组合到一起的类。

代码实现

类适配器

在这里插入图片描述
目标抽象类:

public interface Target {
    public void request();
}

被适配者:

public class Adaptee {
    public void specificRequest() {
        System.out.println("适配者中的业务代码被调用");
    }
}

适配器:

public class ClassAdapter extends Adaptee implements Target {
    @Override
    public void request() {
        super.specificRequest();
    }
}

测试结果:

public class ClassAdapterTest {
    public static void main(String[] args) {
        System.out.println("类适配器模拟测试:");
        Target target = new ClassAdapter();
        target.request();
    }
}

控制台输出结果:

类适配器模拟测试:
适配者中的业务代码被调用

对象适配器

在这里插入图片描述
适配器:

public class ObjectAdapter implements Target {
    private Adaptee adaptee;

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

    @Override
    public void request() {
        //业务处理前加强
        adaptee.specificRequest();
        //业务处理后加强
    }
}

测试结果:

public class ObjectAdapterTest {
    public static void main(String[] args) {
        System.out.println("对象适配器模拟测试:");
        Adaptee adaptee = new Adaptee();
        Target target = new ObjectAdapter(adaptee);
        target.request();
    }
}

控制台输出结果:

对象适配器模拟测试:
适配者中的业务代码被调用

缺省适配器模式

在这里插入图片描述
目标角色:

public interface SampleOperation {
	public abstract void operation1();
	public abstract void operation2();
	public abstract void operation3();
	public abstract void operation4();
	public abstract void operation5();
}

适配器角色:

public abstract class DefaultAdapter implements SampleOperation{

	@Override
	public void operation1() {
	}

	@Override
	public void operation2() {
	}

	@Override
	public void operation3() {
	}

	@Override
	public void operation4() {
	}

	@Override
	public void operation5() {
	}
}

测试缺省需要用的类:

public class Operator {
	private SampleOperation sampleOperation;
	
	public void addOperation(SampleOperation sampleOperation) {
		this.sampleOperation = sampleOperation;
	}

	public void operation1() {
		sampleOperation.operation1();
	}

	public void operation2() {
		sampleOperation.operation2();
	}

	public void operation3() {
		sampleOperation.operation3();
	}

	public void operation4() {
		sampleOperation.operation4();
	}

	public void operation5() {
		sampleOperation.operation5();
	}
}

测试结果:

public class DefaultAdapterTest {
	public static void main(String[] args) {
		// 1、使用缺省适配器只需要实现需要用到的接口方法
		Operator operator = new Operator();
		operator.addOperation(new DefaultAdapter() {
			@Override
			public void operation2() {
				System.out.println("操作2");
			}
		});
		operator.operation2();
	}
}

适用场景

客户端需要一个target(目标)接口,但是不能直接重用已经存在的adaptee(适配者)类,因为它的接口和target接口不一致,所以需要adapter(适配器)将adaptee转换为target接口。

优缺点

优点:
1、复用性:系统需要使用已经存在的类,功能符合系统要求,但这个类的接口不符合系统的需求,通过适配器模式解决不兼容的问题,使这些功能类得到复用。
2、扩展性:适配器使得系统多了一个方式扩展系统的功能
3、耦合性:一定程度上的解耦
缺点:
过多地使用适配器,增加系统理解难度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java适配器设计模式是一种结构型设计模式,用于将一个类的接口转换成客户端所期望的另一种接口,从而使原本接口不匹配的两个类能够协同工作。适配器模式有两种实现方式:类适配器和对象适配器。 类适配器使用继承的方式实现适配器。它包括三个主要角色:目标接口、适配者类和适配器类。目标接口定义了客户端所期望的接口,适配者类是被访问和适配的现存组件库中的组件接口,适配器类继承了适配者类并实现了目标接口,通过继承和重写方法的方式将适配者接口转换成目标接口。 对象适配器使用对象组合的方式实现适配器。它也包括三个主要角色:目标接口、适配者类和适配器类。目标接口和适配者类的定义与类适配器相同,唯一的区别是适配器类不再继承适配者类,而是将适配者对象作为构造参数传入适配器类中,在适配器类的方法中调用适配者对象的方法来实现适配。 适配器设计模式在以下场景中适用: 1. 已经存在的类的方法与需求不匹配,需要进行接口转换。 2. 不同产品或不同厂家的类具有相似的功能但接口不相同,需要对它们进行适配。 总结来说,适配器设计模式能够提高类的透明性和复用性,解耦目标类和适配类,提高程序的扩展性。但在编写适配器时需要全面考虑,可能会增加系统的复杂性,并且过度使用适配器可能会导致代码的混乱。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Java设计模式适配器模式](https://blog.csdn.net/qq_37922483/article/details/124568177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [java设计模式-适配器模式](https://blog.csdn.net/weixin_62862983/article/details/122536241)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

走进IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值