结构型设计模式_适配器模式

1. 适配器模式

1.1.公共部分

1.1.1.适配源

package com.skillstudy.designpattern.action.adapter;

/**
 * Created by kikop on 2018/5/26.
 * 适配源[Adaptee]
 */
public class Adaptee {

    /**
     * 生成发动机
     *

     */
    
public void makeEnginee() {
        System.out.println("[AdapterDesignPattern]AdapterSource-makeEnginee");
    }

    /**
     * 生成轮子
     */
    
public void makeWheel() {
        System.out.println("[AdapterDesignPattern]AdapterSource-makeWheel");
    }
}

1.1.2.目标接口

基于需求分析得到。

package com.skillstudy.designpattern.action.adapter;

/**
 * Created by kikop on 2018/5/26.
 * 目标接口[Target]
 * 这里定义你需要适配的接口
 */
public interface ITargetAdapter {

    public void makeEnginee();
    public void makeWheel();
}

 

1.2.类适配

 结构型设计模式_适配器模式

1.2.1.类适配

package com.skillstudy.designpattern.action.adapter;

/**
 * Created by kikop on 2018/5/26.
 * 类适配器[Adapter]
 */
public class NorthClassAdapter extends Adaptee implements ITargetAdapter {

    @Override
    public void makeEnginee() {
        //用目标方法替换
        
makeBattery();
    }

    @Override
    public void makeWheel() {
        super.makeWheel();
    }



    private void makeBattery() {
        System.out.println("[AdapterDesignPattern]NorthClassAdapter-makeBattery");
    }
}

 

1.2.2.类适配器测试

package com.skillstudy.designpattern.action.adapter;

import org.junit.Test;

/**
 * Created by kikop on 2018/5/26.
 */
public class adapterClassTest {

    @Test
    public void testOrigin() {

        Adaptee adapterSource = new Adaptee();
        adapterSource.makeEnginee();
        adapterSource.makeWheel();
        testAdapter();
    }

    /**
     * 类适配器
     */
    
@Test
    public void testAdapter() {

        Adaptee adapterSource = new NorthClassAdapter();
        adapterSource.makeEnginee();
        adapterSource.makeWheel();

    }
}

 

1.3.对象适配

 

复合设计模式:优先使用组合,易于扩展,灵活性强。

1.3.1.对象适配

结构型设计模式_适配器模式

package com.skillstudy.designpattern.action.adapter;

/**
 * Created by kikop on 2018/5/26.
 * 对象适配器[Adapter]
 */
public class NorthObjectAdapter implements ITargetAdapter {

    /**
     * 适配源
     */
    
protected Adaptee adaptee;


    public NorthObjectAdapter(Adaptee adapterSource) {
        this.adaptee = adapterSource;
    }


    @Override
    public void makeEnginee() {
        makeBattery();
    }

    @Override
    public void makeWheel() {
        adaptee.makeWheel();
    }

    private void makeBattery() {
        System.out.println("[AdapterDesignPattern]NorthClassAdapter-makeBattery");
    }
}

1.3.1.对象适配器测试

package com.skillstudy.designpattern.action.adapter;

import org.junit.Test;

/**
 * Created by kikop on 2018/5/26.
 */
public class adapterObjectTest {

    @Test
    public void testOrigin() {

        Adaptee adapterSource = new Adaptee();
        adapterSource.makeEnginee();
        adapterSource.makeWheel();
        testAdapter();
    }

    /**
     * 类适配器
     */
    
@Test
    public void testAdapter() {

        Adaptee adapterSource = new Adaptee();
        NorthObjectAdapter northObjectAdapter = new NorthObjectAdapter(adapterSource);

        //makeEnginee 内部实现已经改变
        
northObjectAdapter.makeEnginee();
        northObjectAdapter.makeWheel();

    }
}

2. 总结

要使用已有的类,而该类接口与所需的接口并不匹配。

 

 

 

相关文章: