适配器代码
/**
* 将两孔插座变成三孔插座的适配器
*/
public class TwoToThreePlugAdapter implements ThreePlugInf //实现三孔插座的接口,这样就可以使该适配器变成三孔插座对象
{
private TwoPlug twoPlug;
/**
* 适配器需要一个两孔插座对象
*/
public TwoToThreePlugAdapter(TwoPlug twoPlug)
{
this.twoPlug=twoPlug;
}
@Override
public void powerThreePlug()
{
twoPlug.powerTwoPlug(); //在三孔插座方法中写入两孔插座的供电方法。
}
}
/**
* 三孔插座
*/
public interface ThreePlugInf
{
public void powerThreePlug();
}
/**
* 两孔插座
*/
public class TwoPlug
{
public void powerTwoPlug()
{
System.out.println("两孔插座供电中.......");
}
}
0赞 · 1采集
蜗牛__
2017-04-13
适配器模式测试代码
/**
* 适配器模式
* 解释:当两个对象彼此不兼容的时候,使用适配器将其兼容
* 示例:笔记本电脑需要一个三孔插座,而现在只有一个两孔插座,所以要出一个适配器类来将三孔插座转化为两孔插座,使其兼容笔记本
*/
public class LaptopTest
{
private ThreePlugInf threePlugInf;//笔记本所需的三孔插座
public LaptopTest(ThreePlugInf threePlugInf)
{
this.threePlugInf=threePlugInf;
}
public ThreePlugInf getThreePlugInf()
{
return threePlugInf;
}
public static void main(String[] args)
{
TwoPlug twoPlug=new TwoPlug(); //现在只有一个两孔插座
TwoToThreePlugAdapter adapter=new TwoToThreePlugAdapter(twoPlug);
LaptopTest laptop=new LaptopTest(adapter); //笔记本需要三孔插座
laptop.getThreePlugInf().powerThreePlug();//调用三孔插座方法测试。
}
}
1赞 · 1采集
滕玉龙
2017-01-30
package com.imooc.pattern.adapter;
/*
* 二相转三相插座的适配器
*/
public class TwoPlugAdapter implements ThreePlugIf {
private GBTwoPlug plug;
public TwoPlugAdapter(GBTwoPlug plug){
this.plug = plug;
}
@Override
public void powerWithThree() {
System.out.println("通过转化");
plug.powerWithTwo();
}
}
截图
0赞 · 0采集
滕玉龙
2017-01-30
package com.imooc.pattern.adapter;
public class NoteBook {
private ThreePlugIf plug;
public NoteBook(ThreePlugIf plug){
this.plug = plug;
}
// 使用插座充电
public void charge(){
plug.powerWithThree();
}
public static void main(String[] args) {
GBTwoPlug two = new GBTwoPlug();
ThreePlugIf three = new TwoPlugAdapter(two);
NoteBook nb = new NoteBook(three);
nb.charge();
}
}
运行结果:
通过转化
使用二相电流供电
截图
0赞 · 0采集
滕玉龙
2017-01-30
package com.imooc.pattern.adapter;
public class GBTwoPlug {
// 使用二相电流供电
public void powerWithTwo(){
System.out.println("使用二相电流供电");
}
}
0赞 · 0采集
滕玉龙
2017-01-30
package com.imooc.pattern.adapter;
/*
* 三相插座接口
*/
public interface ThreePlugIf {
// 使用三相电流供电
public void powerWithThree();
}
0赞 · 0采集
庄学爸
2016-04-28
对象适配器模式
与类的适配器模式一样,对象的适配器模式把被适配的类的API转换成为目标类的API,与类的适配器模式不同的是,对象的适配器模式不是使用继承关系连接到Adaptee类,而是使用委派关系连接到Adaptee类.
Target目标接口中同样只有Request()方法.
Adaptee源接口中也只有SpecificRequest()方法,没有Request方法.而客户端则期待这个方法,为使客户端能够使用Adaptee类,需要提供一个包装(Wrapper)类Adapter.这个包装类包装了一个Adaptee的实例,从而此包装类能够把Adaptee的API与Target类的API衔接起来.
Adapter与Adaptee是委派关系,这决定了适配器模式是对象的.
目标接口:
public interface Target {
//这是源类Adaptee没有的方法
public void Request();
}
源接口类:
public class Adaptee{
public void SpecificRequest()
{
System.out.println("这是原始标准接口!(三相)");
}
}
适配器类:
public class Adapter implements Target{
private Adaptee adaptee;
public Adapter(Adaptee adapt){
this.adaptee=adapt;
}
//实现Request()方法
public void Request()
{
adaptee.SpecificRequest();
}
}
类适配器模式与对象适配器模式区别在于适配器类的行为不同.
类适配器模式的适配器类是继承源接口类,而对象适配器模式的适配器类是以委派的方式与源接口类进行关联.
0赞 · 1采集
庄学爸
2016-04-28
类适配器模式:把被适配的类的API转换成为目标类的API
目标就像二相插座,源就像三相插座,适配器就是一个三相到两相的转换器.
假设Target目标接口中有Request()方法(相当于二相插座).
Adaptee源接口中只有SpecificRquest()方法(相当于三相插座),没有Request()方法(即没有二相插座的插孔),而客户端则期待这个方法,为使客户端能够使用Adaptee类,提供一个中间环节,即类Adapter,把Adaptee的API与Target类的API衔接起来.
Adapter与Adaptee是继承关系,这决定了这个适配器模式是类的.
目标接口:
public interface Target {
//这是源类Adaptee没有的方法
public void Request();
}
上面给出的是目标角色的源代码,这个角色是以一个JAVA接口的形式实现的.可以看出这个接口声明了Request()方法,而源角色Adaptee是一个具体类,它有一个SpecificRequest()方法,但是没有Request()方法.
源接口类:
public class Adaptee{
public void SpecificRequest()
{
System.out.println("这是原始标准接口!(三相)");
}
}
适配器类:
public class Adapter extends Adaptee implements Target {
//实现接口中的Request()方法,方法内部调用继承Adaptee中的SpecificRequest()方法.
public void Request()
{
super.SpecificRequest();
}
}
那么适配器Adapter就把被适配的类的API转换成为目标类的API.
1.ThreePlugIf接口
/*
* 三相插座接口
*/
public interface ThreePlugIf {
//使用三相电流供电
public void powerWithThree();
}
2.GBTwoPlug.java(GB两厢插口)
public class GBTwoPlug {
//使用二相电流供电
public void powerWithTwo(){
System.out.println("使用二相电流供电");
}
}
3.TwoPlugAdapter.java
/*
* 二相转三相的插座适配器
*/
public class TwoPlugAdapter implements ThreePlugIf {
private GBTwoPlug plug;
public TwoPlugAdapter(GBTwoPlug plug){
this.plug = plug;
}
@Override
public void powerWithThree() {
System.out.println("通过转化");
plug.powerWithTwo();
}
}
4.NoteBook.java
public class NoteBook {
private ThreePlugIf plug;
public NoteBook(ThreePlugIf plug){
this.plug = plug;
}
//使用插座充电
public void charge(){
plug.powerWithThree();
}
public static void main(String[] args) {
GBTwoPlug two = new GBTwoPlug();
ThreePlugIf three = new TwoPlugAdapter(two);
NoteBook nb = new NoteBook(three);
nb.charge();
three = new TwoPlugAdapterExtends();
nb = new NoteBook(three);
nb.charge();
}
}
0赞 · 1采集
TonyChiang同學
2014-11-25
1.ThreePlugIf接口
/*
* 三相插座接口
*/
public interface ThreePlugIf {
//使用三相电流供电
public void powerWithThree();
}
2.GBTwoPlug.java(GB两厢插口)
public class GBTwoPlug {
//使用二相电流供电
public void powerWithTwo(){
System.out.println("使用二相电流供电");
}
}
3.TwoPlugAdapter.java
/*
* 二相转三相的插座适配器
*/
public class TwoPlugAdapter implements ThreePlugIf {
private GBTwoPlug plug;
public TwoPlugAdapter(GBTwoPlug plug){
this.plug = plug;
}
@Override
public void powerWithThree() {
System.out.println("通过转化");
plug.powerWithTwo();
}
}
4.NoteBook.java
public class NoteBook {
private ThreePlugIf plug;
public NoteBook(ThreePlugIf plug){
this.plug = plug;
}
//使用插座充电
public void charge(){
plug.powerWithThree();
}
public static void main(String[] args) {
GBTwoPlug two = new GBTwoPlug();
ThreePlugIf three = new TwoPlugAdapter(two);
NoteBook nb = new NoteBook(three);
nb.charge();
three = new TwoPlugAdapterExtends();
nb = new NoteBook(three);
nb.charge();
}
}