總結設計模式——(大話設計模式讀後感上篇)
1、簡單工廠模式、工廠模式與抽象工廠模式
public interface Human { public void say(); } class Man implements Human { public void say() { System.out.println("男人"); } } class Woman implements Human { public void say() { System.out.println("女人"); } }
簡單工廠模式:簡單來說 ,在工廠類中新增判斷方法返回對應的類;
缺點 違背開放封閉原則,需要經常修改工廠類
class SampleFactory { public static Human makeHuman(String type){ if(type.equals("man")){ return new Man(); }else if(type.equals("womman")){ return new Woman(); } } }
工廠模式: 將邏輯判斷拿到客戶端執行
interface Factory { public Human crateMan(); } class ManFactory implements Factory{ public Human crateMan() { return new Man(); } } class WomanFactory implements Factory{ public Human crateMan() { return new Woman(); } } class NormalFatory{ public static void main(String[] args) { Factory factory=new ManFactory(); Humanman2=factory.crateMan(); man2.say(); } }
抽象工廠模式:因為實際業務中,通過客戶端新增邏輯程式碼非常長,而且不容易修改。現在可以用子類抽象工廠來組裝分離不同的業務。
interface Car { void gotowork(); } class Bus implements Car { public void gotowork() { System.out.println("坐公交車去上班!"); } } class Train implements Car { public void gotowork() { System.out.println("坐火車去上班!"); } } interface AbstractFactory { Human getHuman(); Car getCar(); } class ManFactory implements IAbstractFactory { public Car getCar() { return new Bus(); } public Human getHuman() { return new Man(); } } class WomanFactory implements IAbstractFactory { public Car getCar() { return new Train(); } public Human getHuman() { return new Woman(); } } class AbstractFactoryTest{ public static void main(String[] args) { IAbstractFactory factory = new ManFactory(); Car car = factory.getCar(); Human human = factory.getHuman(); human.say(); car.gotowork(); IAbstractFactory factory2 = new ManFactory(); car = factory2.getCar(); human = factory.getHuman(); human.say(); car.gotowork(); } }
2 、策略模式: 上面 1所述的模式我們發現 工廠中主要巢狀方法,如果巢狀的是屬性,就是策略模式。
class Strategy{ Human human; Strategy(Human human){ this.human = human; } publicvoid doExec1(){ human.say(); /*一般這裡都是和工廠模式一樣新增邏輯判斷*/ } }
3、代理模式 :我的理解代理就是物件的引用訪問控制;相當於是策略模式的延伸。通過Proxy訪問物件
class Proxy{ Woman woman; Proxy(Woman woman){ this.woman = woman; } publicvoid doExec1(){ woman.say(); } }
4、原型模式 :這裡涉及一個深拷貝和淺拷貝的概念。
⑴淺克隆 淺複製僅僅複製所考慮的物件,而不復制它所引用的物件。 Object類提供的方法clone只是拷貝本物件,其物件內部的陣列、引用物件等都不拷貝,還是指向原生物件的內部元素地址
⑵深克隆 深複製把要複製的物件所引用的物件都複製了一遍,在jvm中新建個物件。
淺克隆,部分物件未實現Cloneable介面
深克隆,所有物件都實現Cloneable介面重寫clone()或者通過流序列化如下所示
在所有涉及的類都實現Serializable介面 Object deepClone() throws IOException, ClassNotFoundException{ //將物件寫到流裡 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); //從流裡讀回來 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return ois.readObject(); }
簡單來說,Man man =new Man() 就是建立一個原型, Man man1 = man.clone();這裡就是淺客隆
5、模版模式 定義一套模版,子類在模版的結構中新增不同的邏輯。 我們對工廠模式的案例重寫下
abstract class Human{ public void say(); public void action(); template(){ this.say(); this.action(); } } class Man implements Human { public void say() { System.out.println("男人"); } public void action(){ System.out.println("玩遊戲"); } } class Woman implements Human { public void say() { System.out.println("女人"); } public void action(){ System.out.println("貼面膜"); } }
客戶端
public static void main(String[] args) { Human human; human = new Man(); human.template(); human = new Woman(); human.template(); }
6、外觀模式 我的理解就是對 類方法的不同組合
class Facade{ Man man; Woman woman; // A組合 public methodA(){ man.say(); woman.action(); } //B組合 public methodB(){ man.say(); woman.say(); } }
7、建造者模式 這個模式 與外觀模式好像,這不過外觀模式 多用於不同物件的方法組合。建造者模式是一種 類由多個方法組合
class Director{ //這個方法內部是固定的,就像做好菜的祕方一樣 public void group(Human human){ human.say(); huamn.action(); ...//say方法建造過程中可以放前,也可以放action後。 } }
時間有限,篇幅有點長了,下篇再寫。以上只是一個識別設計模式的簡單案例,實際程式碼中可能會有三四種模式的複合或者更多複合。 可能因為個人的理解和角度不同,如果覺得有問題,請留言。