天天看點

設計模式-23種設計模式-結構型-擴充卡模式

一、擴充卡模式介紹

設計模式-23種設計模式-結構型-擴充卡模式
二、擴充卡模式引入

需求:

設計模式-23種設計模式-結構型-擴充卡模式
設計模式-23種設計模式-結構型-擴充卡模式

 1.類擴充卡模式

  介紹:

設計模式-23種設計模式-結構型-擴充卡模式

   代碼實作(Java):

設計模式-23種設計模式-結構型-擴充卡模式
//被适配的類
public class Voltage220V {
    //輸出220V的電壓
    public int output220V() {
        int src = 220;
        System.out.println("電壓=" + src + "伏");
        return src;
    }
}      
//适配接口
public interface IVoltage5V {
    public int output5V();
}      
//擴充卡類
public class VoltageAdapter extends Voltage220V implements IVoltage5V {

    @Override
    public int output5V() {
        //擷取到220V電壓
        int srcV = output220V();
        int dstV = srcV / 44 ; //轉成 5v
        return dstV;
    }

}      
public class Phone {

    //充電
    public void charging(IVoltage5V iVoltage5V) {
        if(iVoltage5V.output5V() == 5) {
            System.out.println("電壓為5V, 可以充電~~");
        } else if (iVoltage5V.output5V() > 5) {
            System.out.println("電壓大于5V, 不能充電~~");
        }
    }
}      
public class Client {

    public static void main(String[] args) {
        System.out.println(" === 類擴充卡模式 ====");
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter());
    }

}      

 2.對象擴充卡模式

   介紹:

設計模式-23種設計模式-結構型-擴充卡模式

設計模式-23種設計模式-結構型-擴充卡模式
//被适配的類
public class Voltage220V {
    //輸出220V的電壓,不變
    public int output220V() {
        int src = 220;
        System.out.println("電壓=" + src + "伏");
        return src;
    }
}      
//适配接口
public interface IVoltage5V {
    public int output5V();
}      
//擴充卡類
public class VoltageAdapter  implements IVoltage5V {

    private Voltage220V voltage220V; // 關聯關系-聚合


    //通過構造器,傳入一個 Voltage220V 執行個體
    public VoltageAdapter(Voltage220V voltage220v) {

        this.voltage220V = voltage220v;
    }



    @Override
    public int output5V() {

        int dst = 0;
        if(null != voltage220V) {
            int src = voltage220V.output220V();//擷取220V 電壓
            System.out.println("使用對象擴充卡,進行适配~~");
            dst = src / 44;
            System.out.println("适配完成,輸出的電壓為=" + dst);
        }

        return dst;

    }

}      
public class Phone {

    //充電
    public void charging(IVoltage5V iVoltage5V) {
        if(iVoltage5V.output5V() == 5) {
            System.out.println("電壓為5V, 可以充電~~");
        } else if (iVoltage5V.output5V() > 5) {
            System.out.println("電壓大于5V, 不能充電~~");
        }
    }
}      
public class Client {

    public static void main(String[] args) {
        System.out.println(" === 對象擴充卡模式 ====");
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter(new Voltage220V()));
    }

}      

3.接口擴充卡模式

  介紹

設計模式-23種設計模式-結構型-擴充卡模式

   代碼實作(Java)

設計模式-23種設計模式-結構型-擴充卡模式
public interface Interface4 {
    public void m1();
    public void m2();
    public void m3();
    public void m4();
}      
//在AbsAdapter 我們将 Interface4 的方法進行預設實作
public abstract class AbsAdapter implements Interface4 {

    //預設實作
    public void m1() {

    }

    public void m2() {

    }

    public void m3() {

    }

    public void m4() {

    }
}      
public class Client {
    public static void main(String[] args) {

        AbsAdapter absAdapter = new AbsAdapter() {
            //隻需要去覆寫我們 需要使用 接口方法
            @Override
            public void m1() {
                System.out.println("使用了m1的方法");
            }
        };

        absAdapter.m1();
    }
}      

三、擴充卡模式在SpringMVC架構應用的源碼分析 

設計模式-23種設計模式-結構型-擴充卡模式
設計模式-23種設計模式-結構型-擴充卡模式
設計模式-23種設計模式-結構型-擴充卡模式

 代碼實作(Java):

設計模式-23種設計模式-結構型-擴充卡模式
//多種Controller實作  
public interface Controller {

}

class HttpController implements Controller {
    public void doHttpHandler() {
        System.out.println("http...");
    }
}

class SimpleController implements Controller {
    public void doSimplerHandler() {
        System.out.println("simple...");
    }
}

class AnnotationController implements Controller {
    public void doAnnotationHandler() {
        System.out.println("annotation...");
    }
}      
///定義一個Adapter接口 
public interface HandlerAdapter {
    public boolean supports(Object handler);

    public void handle(Object handler);
}

// 多種擴充卡類

class SimpleHandlerAdapter implements HandlerAdapter {

    public void handle(Object handler) {
        ((SimpleController) handler).doSimplerHandler();
    }

    public boolean supports(Object handler) {
        return (handler instanceof SimpleController);
    }

}

class HttpHandlerAdapter implements HandlerAdapter {

    public void handle(Object handler) {
        ((HttpController) handler).doHttpHandler();
    }

    public boolean supports(Object handler) {
        return (handler instanceof HttpController);
    }

}

class AnnotationHandlerAdapter implements HandlerAdapter {

    public void handle(Object handler) {
        ((AnnotationController) handler).doAnnotationHandler();
    }

    public boolean supports(Object handler) {

        return (handler instanceof AnnotationController);
    }

}      
import java.util.ArrayList;
import java.util.List;

public class DispatchServlet {

    public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();

    public DispatchServlet() {
        handlerAdapters.add(new AnnotationHandlerAdapter());
        handlerAdapters.add(new HttpHandlerAdapter());
        handlerAdapters.add(new SimpleHandlerAdapter());
    }

    public void doDispatch() {

        // 此處模拟SpringMVC從request取handler的對象,
        // 擴充卡可以擷取到希望的Controller
         HttpController controller = new HttpController();
        // AnnotationController controller = new AnnotationController();
        //SimpleController controller = new SimpleController();
        // 得到對應擴充卡
        HandlerAdapter adapter = getHandler(controller);
        // 通過擴充卡執行對應的controller對應方法
        adapter.handle(controller);

    }

    public HandlerAdapter getHandler(Controller controller) {
        //周遊:根據得到的controller(handler), 傳回對應擴充卡
        for (HandlerAdapter adapter : this.handlerAdapters) {
            if (adapter.supports(controller)) {
                return adapter;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        new DispatchServlet().doDispatch(); // http...
    }

}      

四、擴充卡模式注意事項和細節