經過前幾篇的介紹相信大家對JAVA的設計模式一定有所解了,本篇我們再一起學習一下適配器模式、代理模式和工廠模式。
適配器模式使用的場景非常多,例如現實生活中,我們的筆記本電腦的充電線大部分都是三向插頭,而當我們遇見一個二向插口時,如何給我們的筆記本充電呢?這時我們就需要一個適配器,幫我們把二向插口轉化為三向插口。接下來我們需要討論的適配器模式,就是如同這里的二向轉三向插口,下面我們就以這個現實問題,來用代碼實現一下適配器模式。
1、創建三向電流接口:
/* * 定義一個三相充電器接口 */public interface ThreePathIm { //使用三相電流供電 public void powerWithThree();}2、創建三向電流類:
public class ThreePath implements ThreePathIm { public void powerWithThree() { System.out. 3、創建二向電流類:
/* * 二相電流類 */public class TwoPath { public void prowerWithTwo(){ System.out.println("使用二相電流供電"); } } 4、創建二向接口轉三向接口類(接口適配器):
/* * 電源接口適配器 * 二向接口適配三口接口 */public class TwoPlugAdapt implements ThreePathIm { private TwoPath two ; public TwoPlugAdapt(TwoPath two){ this.two = two; } public void powerWithThree() { System.out.println("通過適配器轉化"); two.prowerWithTwo(); }} 5、創建繼承二向電流類并實現了三向電流接口的類(繼承適配器):
/* * 繼承適配器 */public class extendsAdapt extends TwoPath implements ThreePathIm { public void powerWithThree() { System.out.println("/n使用繼承適配器轉化"); this.prowerWithTwo(); }} 6、創建測試類:
public class noteBook { private ThreePathIm path ; private noteBook(ThreePathIm path){ this.path = path; } private void change(){ path.powerWithThree(); } public static void main(String [] args){ ThreePathIm tpi = new ThreePath(); tpi.powerWithThree(); TwoPath two = new TwoPath();//獲得二相接口對象 ThreePathIm three = new TwoPlugAdapt(two);//把二相電流接口轉為三向 noteBook notebook = new noteBook(three); notebook.change(); three = new extendsAdapt(); notebook = new noteBook(three); notebook.change(); }}
工程模式使用的場景也比較多,比如之前很火的一款名為臉萌的圖片制作軟件,我們可以根據我們的需要來選擇頭發的類型,這是如何實現的呢?下面我們來一起學習一下。 1、創建頭發類型接口:
public interface Hair { public void getHair();//獲得發型方法 } 2、通過該方法實現兩個頭發類型:
a、左偏分:
public class leftHair implements Hair { //左偏分 public void getHair() { System.out.println("我的頭發是左偏分"); }} b、右偏分:
public class rightHair implements Hair { //右偏分 public void getHair() { System.out.println("我的頭發是右偏分"); }} 3、創建頭發工廠:
public class hairFactory { Hair hair; //通過關鍵詞來獲得相應的頭發類型類 public Hair getHairKey(String key){ if("left".equals(key)){ hair = new leftHair(); }else if("right".equals(key)){ hair = new rightHair(); } return hair; } //通過類地址來獲得相應的頭發類型類 public Hair getHairClass(String cls){ try { hair = (Hair)Class.forName(cls).newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalaccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return hair; }}4、創建測試類:
public class hairTest { /** * 測試類 * @param args */ public static void main(String[] args) { Hair hair1 = new leftHair(); Hair hair2 = new rightHair(); hair1.getHair(); hair2.getHair(); //通過工廠對象進行創建類 hairFactory factory = new hairFactory(); Hair hair3 = factory.getHairKey("left"); hair3.getHair(); Hair hair4 = factory.getHairClass("cn.edu.hpu.hair.rightHair"); hair4.getHair(); }}代理模式是對一個對象提供一種代理,用來控制對這個對象的控制。

下面我們通過實現一個汽車行駛時,記錄行車時間和日志的功能,不多說,上代碼:
1、封裝一個汽車行駛的方法:
public interface MoveAble { public void move();}2、創建一個汽車類:
public class Car implements MoveAble { public void move(){ try {// System.out.println("汽車開始行駛");// long start = System.currentTimeMillis(); System.out.println("汽車在行駛中"); Thread.sleep(new Random().nextInt(1000));//模擬汽車行駛 // long end = System.currentTimeMillis();// System.out.println("汽車停止行駛 汽車行駛了:"+(end-start)+"毫秒"); } catch (InterruptedException e) { e.printStackTrace(); } }}3、實現一個汽車子類:
通過繼承方法,創建不同子類來實現行駛時間和日志的記錄。
public class Car2 extends Car { public void move() { System.out.println("汽車開始行駛"); long start = System.currentTimeMillis(); super.move();//執行父類的方法 long end = System.currentTimeMillis(); System.out.println("汽車停止行駛 汽車行駛了:"+(end-start)+"毫秒"); } }4、創建接口代理:
a、時間代理對象:
public class CarTimeProxy implements MoveAble { public CarTimeProxy(MoveAble m){ this.m = m; } public MoveAble m; public void move() { System.out.println("汽車開始行駛"); long start = System.currentTimeMillis(); m.move(); long end = System.currentTimeMillis(); System.out.println("汽車停止行駛 汽車行駛了:"+(end-start)+"毫秒"); }}b、日志代理對象:
public class CarLogProxy implements MoveAble { public CarLogProxy(MoveAble m) { super(); this.m = m; } public MoveAble m; public void move() { System.out.println("日志開始"); m.move(); System.out.println("日志結束"); }}5、測試類:
public class carTest { /** * @param 測試 */ public static void main(String[] args) {// Car car = new Car();// car.move(); //繼承模式的靜態代理// Car car = new Car2();// car.move(); //接口模式的靜態代理,疊加操作 Car car = new Car(); MoveAble m1 = new CarTimeProxy(car); MoveAble m2 = new CarLogProxy(m1); m2.move(); }}6、通過JDK實現代理:
public class TimeHander implements InvocationHandler { public TimeHander(Object object) { super(); this.object = object; } Object object; /* * 參數: * proxy:被代理的對象 * method:被代理對象的方法 * args:方法的參數 */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("汽車開始行駛"); long start = System.currentTimeMillis(); method.invoke(object, null); long end = System.currentTimeMillis(); System.out.println("汽車停止行駛 汽車行駛了:"+(end-start)+"毫秒"); return null; }}7、JDK代理測試:
//jdk動態代理public class Test { public static void main(String [] args){ Car car = new Car(); InvocationHandler hander = new TimeHander(car); Class cls = car.getClass(); MoveAble m = (MoveAble) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), hander); m.move(); }}截止到本篇關于JAVA中的設計模式已經為大家分析完畢,當然這些都是很基本的東西,想真正的在開發中使用,還需要多多練習。如有疑問,可以留言討論。
新聞熱點
疑難解答