一、概述java
二、面向對象設計原則算法
1)單一職責原則設計模式
2)開閉原則ide
3)里氏替換原則測試
4)依賴倒置原則this
5)接口隔離原則spa
6)迪米特原則設計
7)組合/聚合複用原則代理
三、設計模式code
四、設計模式分類
五、簡單工廠模式
樣例:
產品接口類(Product):
package com.ljb.simplefactory; /** * 產品接口 * @author LJB * @version 2015年4月21日 */ public interface Product { /**產品生產工藝*/ public void craftwork(); /**產品特徵*/ public void type(); }
產品類(鮮奶):
package com.ljb.simplefactory; /** * 鮮奶 * @author LJB * @version 2015年4月21日 */ public class Creamery implements Product { @Override public void craftwork() { System.out.println("牛奶+除菌=鮮奶"); } @Override public void type() { System.out.println("原味濃香"); } }
產品類(酸奶):
package com.ljb.simplefactory; /** * 酸奶 * @author LJB * @version 2015年4月21日 */ public class Yoghourt implements Product { @Override public void craftwork() { System.out.println("牛奶+酵母菌+糖=酸奶"); } @Override public void type() { System.out.println("酸甜可口"); } }
簡單工廠類:
package com.ljb.simplefactory; /** * 靜態工廠 * @author LJB * @version 2015年4月21日 */ public class StaticFactory { public static Product factory (String str) throws BadException { if (str.equalsIgnoreCase("creamery")) { System.out.println("生產鮮奶"); return new Creamery(); } else if (str.equalsIgnoreCase("yoghourt")) { System.out.println("生產酸奶"); return new Yoghourt(); } else { throw new BadException("沒有此產品"); } } }
異常類:
package com.ljb.simplefactory; /** * 自定義異常類 * @author LJB * @version 2015年4月21日 */ public class BadException extends Exception { public BadException (String msg) { super(msg); } }
測試類:
package com.ljb.simplefactory; /** * 測試類 * @author LJB * @version 2015年4月21日 */ public class Test { /** * @param args */ public static void main(String[] args) { try { Product x = StaticFactory.factory("creamery"); x.craftwork(); x.type(); Product y = StaticFactory.factory("yoghourt"); y.craftwork(); y.type(); } catch (BadException e) { e.printStackTrace(); } } }
執行結果:
生產鮮奶
牛奶+除菌=鮮奶
原味濃香
生產酸奶
牛奶+酵母菌+糖=酸奶
酸甜可口
小結:
工廠須要知道多有產品
缺點:對開閉原則支持不夠
六、工廠方法模式
注:引入簡單工廠模式中的產品接口及產品類
增長:
工廠接口(ProductFactory):
package com.ljb.factorymethod; /** * 工廠接口類 * @author LJB * @version 2015年4月21日 */ public interface ProductFactory { public Product factory(); }
工廠實現類(鮮奶工廠):
package com.ljb.factorymethod; /** * 鮮奶工廠 * @author LJB * @version 2015年4月21日 */ public class CreameryFactory implements ProductFactory { @Override public Product factory() { System.out.println("生產鮮奶"); return new Creamery(); } }
工廠實現類(酸奶工廠):
package com.ljb.factorymethod; /** * 酸奶工廠類 * @author LJB * @version 2015年4月21日 */ public class YoghourtFactory implements ProductFactory { @Override public Product factory() { System.out.println("生產酸奶"); return new Yoghourt(); } }
測試類:
package com.ljb.factorymethod; /** * 測試類 * @author LJB * @version 2015年4月21日 */ public class Test { /** * @param args */ public static void main(String[] args) { ProductFactory y = new CreameryFactory(); y.factory().craftwork(); y.factory().type(); ProductFactory x = new YoghourtFactory(); x.factory().craftwork(); x.factory().type(); } }
七、代理模式
樣例:
抽象主題:
package com.ljb.proxy; /** * 抽象主題或者是接口 * @author LJB * @version 2015年4月21日 */ public abstract class Subject { /** * 聲明一個抽象的請求方法 */ abstract public void request(); }
真實主題:
package com.ljb.proxy; /** * 真是主題 * @author LJB * @version 2015年4月21日 */ public class RealSubject extends Subject { @Override public void request() { System.out.println("實現請求"); } }
代理主題:
package com.ljb.proxy; /** * 代理主題(經過代理實現對真實主題的引用,而且能夠加入本身的方法) * @author LJB * @version 2015年4月21日 */ public class ProxySubject extends Subject { private RealSubject realSubject; @Override public void request() { preRequest(); if (realSubject == null) { realSubject = new RealSubject(); } realSubject.request(); aftRequest(); } public void preRequest() { System.out.println("請求前的方法"); } public void aftRequest () { System.out.println("請求後的方法"); } }
測試類:
package com.ljb.proxy; /** * 測試類 * @author LJB * @version 2015年4月21日 */ public class Test { /** * @param args */ public static void main(String[] args) { Subject proxy = new ProxySubject(); proxy.request(); } }
運行結果:
請求前的方法
實現請求
請求後的方法
八、策略(strategy)模式
樣例:
策略抽象類(抽象帳戶):
package com.ljb.strategy; /** * 抽象帳戶 * @author LJB * @version 2015年4月21日 */ public abstract class Account { /** * 計算產品的真實價格 * @param amount * @param price * @return */ public abstract float getFactPrice (int amount , int price); }
策略實現類(普通帳戶):
package com.ljb.strategy; /** * 普通客戶商品價格計算類 * @author LJB * @version 2015年4月21日 */ public class CommonAccount extends Account { @Override public float getFactPrice(int amount, int price) { return amount*price; } }
策略實現類(會員帳戶):
package com.ljb.strategy; /** * 會員商品價格計算類 * @author LJB * @version 2015年4月21日 */ public class InsiderAccount extends Account { @Override public float getFactPrice(int amount, int price) { // TODO Auto-generated method stub return amount*price*9/10; } }
策略實現類(VIP帳戶):
package com.ljb.strategy; /** * vip會員商品價格計算類 * @author LJB * @version 2015年4月21日 */ public class VipAccount extends Account { @Override public float getFactPrice(int amount, int price) { // TODO Auto-generated method stub return amount*price*8/10; } }
策略維護類:
package com.ljb.strategy; /** * 此類用於維護策略類的引用 * 各個算法封裝起來,經過維護類實現對各個算法的引用 * @author LJB * @version 2015年4月21日 */ public class Context { /**持有策略類的引用*/ private Account account; /** * 構造方法,實例化策略類的引用 * 父類做爲方法形參的使用方式 * @param account */ public Context (Account account) { this.account = account; } /** * 調用策略方法 * @param amount * @param price * @return */ public float doAccount (int amount , int price) { return account.getFactPrice(amount, price); } }
測試類:
package com.ljb.strategy; /** * 測試類 * @author LJB * @version 2015年4月21日 */ public class Test { /** * @param args */ public static void main(String[] args) { String name = "CD唱片"; int amount = 2; int price = 50; float sum = 0; // 普通客戶 Context common = new Context(new CommonAccount()); sum = common.doAccount(amount, price); System.out.println("您是普通客戶沒有折扣,商品名稱:" + name + "數量:" + amount + "價格:" + price + "應付金額:" + sum); // 會員客戶 Context insider = new Context(new InsiderAccount()); sum = insider.doAccount(amount, price); System.out.println("您是會員享受9折優惠,商品名稱:" + name + "數量:" + amount + "價格:" + price + "應付金額:" + sum); // VIP客戶 Context vip = new Context(new VipAccount()); sum = vip.doAccount(amount, price); System.out.println("您是VIP會員享受8折優惠,商品名稱:" + name + "數量:" + amount + "價格:" + price + "應付金額:" + sum); } }
運行結果:
您是普通客戶沒有折扣,商品名稱:CD唱片數量:2價格:50應付金額:100.0
您是會員享受9折優惠,商品名稱:CD唱片數量:2價格:50應付金額:90.0
您是VIP會員享受8折優惠,商品名稱:CD唱片數量:2價格:50應付金額:80.0