java设计模式之 -----适配器模式

Adapter Pattern(适配器模式),适配器模式分为三种:类的适配器模式、对象的适配器模式、

接口的适配器模式。简单来说,适配器模式多用于在已知的类或者方法基础上进行扩展和延伸。
 
  
package com.cupid.Pattern.Adapter;

/**
 * @Author: cupid
 * Created by Cupid520 on 2018/2/22.
 * @Description: 基础方法接口
 */
public interface BaseInterface {
    public void srcMethod();
    public void srcMethod_2();
}
package com.cupid.Pattern.Adapter;

/**
 * @Author: cupid
 * Created by Cupid520 on 2018/2/22.
 * @Description: 源类,即模拟要被扩展的类和方法
 */
public class SrcClass {
    public void srcMethod(){
        System.out.println("this is srcMethod from SrcClass");
    }
}
 
  
*************************************************
package com.cupid.Pattern.Adapter;

/**
 * @Author: cupid
 * Created by Cupid520 on 2018/2/22.
 * @Description: 类的适配器模式
 */
public class ClassAdapter extends SrcClass implements BaseInterface{
    @Override
    public void srcMethod_2() {
        System.out.println("类的适配器模式 : srcMethod_2");
    }
}
package com.cupid.Pattern.Adapter;
 
  
*************************************************
/**
 * @Author: cupid
 * Created by Cupid520 on 2018/2/22.
 * @Description: 对象的适配器
 */
public class ObjectAdapter implements BaseInterface{
    private SrcClass srcClass;

    public ObjectAdapter(SrcClass srcClass){
        this.srcClass = srcClass;
    }

    public ObjectAdapter(){
        this.srcClass = new SrcClass();
    }

    @Override
    public void srcMethod() {
        if(srcClass != null){
            srcClass.srcMethod();
        }else{
            throw new RuntimeException("the field srcClass is null");
        }
    }

    @Override
    public void srcMethod_2() {
        System.out.println("对象的适配器模式 : srcMethod_2");
    }
}
    *************************************************
 
  
package com.cupid.Pattern.Adapter;

/**
 * @Author: cupid
 * Created by Cupid520 on 2018/2/22.
 * @Description: 接口的适配器模式,基础接口的抽象类
 */
public abstract class BaseAbstractInterface implements BaseInterface{
    @Override
    public void srcMethod() {
    }

    @Override
    public void srcMethod_2() {
    }
}
package com.cupid.Pattern.Adapter;

/**
 * @Author: cupid
 * Created by Cupid520 on 2018/2/22.
 * @Description: 接口的适配器,此处为测试则仅重写方法二
 */
public class InterfaceAdapter extends BaseAbstractInterface{
    @Override
    public void srcMethod_2() {
        System.out.println("接口的适配器 : srcMethod_2");
    }
}
*******************************************************************

测试主类

package com.cupid.Pattern.Adapter;

/**
 * @Author: cupid
 * Created by Cupid520 on 2018/2/22.
 * @Description: 测试主类
 */
public class MainTest {
    public static void main(String[] args) {
        /*
        类的适配器模式
        ClassAdapter classAdapter = new ClassAdapter();
        classAdapter.srcMethod_2();
        classAdapter.srcMethod();
        */
        /*
        对象的适配器模式
        SrcClass srcClass = new SrcClass();
        ObjectAdapter objectAdapter = new ObjectAdapter(srcClass);
        objectAdapter.srcMethod();
        objectAdapter.srcMethod_2();
        */
        /*
        接口的适配器模式
        InterfaceAdapter interfaceAdapter = new InterfaceAdapter();
        interfaceAdapter.srcMethod_2();
        */
    }
}

你可能感兴趣的:(Java,设计模式)