设计模式_1_工厂模式与抽象工厂
生活随笔
收集整理的這篇文章主要介紹了
设计模式_1_工厂模式与抽象工厂
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
工廠模式
工廠模式(創建型模式):創建對象接口,讓其子類自己決定實例化哪一個工廠類,工廠模式使其創建延伸到子類進行
主要解決接口選擇問題,明確計劃不同條件下執行創建不同實例
通過子類實現工廠實例,創建過程在其子類執行
優點:提高擴展性,屏蔽產品具體實現,調用者只關心產品接口;
缺點:增加一個產品,會導致系統中類個數增加,造成系統復雜度
使用場景:
簡單工廠:一個工廠類,一個產品抽象類
工廠方法:多個工廠類,一個產品抽象類
抽象工廠:多個工廠類,多個產品抽象類
個人思考:
采用松耦合,多態的方式,使得我想要哪一個功能,就生成相應對象 問題的考慮方向只需放在要實例化哪個類,要增加哪一項功能也可以使用枚舉的方式 實現工廠模式: 將每個工廠實例作為枚舉對象,通過枚舉工廠進行調用抽象工廠
抽象工廠模式(創建型模式,其他工廠的工廠):
接口只負責創建一個相關的對象的工廠,不需要顯式指定類,工廠模式就能按照工廠模式提供對象(多態)
用于解決接口選擇問題
優點:當產品族中,多個對象被設計成一起工作,能保證客戶端只使用一個產品族中對象
缺點:產品族中擴展產品,需要在抽象類中添加代碼, 還需要在具體實現里面添加代碼
個人思考:
下面給出例子說明抽象工廠與工廠模式:
package com.AbstractFactory; /*** 懂得一點,使用設計模式, 先搞定UML* 建立多個工廠, 然后建立一個總的工廠,通過總的工廠調用下屬,* 主要還是多態的性質運用, 程序中還有很多瑕疵* 主要還是接口的選擇, 以及狀態的變化* @author regotto**/ public class AbstractFactoryDemo1 {public static void main(String[] args) {// 獲得圖像工廠AbstractFactoryShapeColor shapefactory=FactoryProducer1.getFactory("Shape");// 根據目標圖像繪制對應的圖案shapefactory.getShape("Rectangle").draw();// 獲得顏色工廠AbstractFactoryShapeColor colorfactory=FactoryProducer1.getFactory("Color");//根據需要的顏色給對應的圖像做顏色填充colorfactory.getColor("Red").fill();;}}//使用工廠 class FactoryProducer1{public static AbstractFactoryShapeColor getFactory(String FactoryType) {if(FactoryType==null) return null;else if(FactoryType.equals("Shape"))return new ShapeFactory();else if(FactoryType.equals("Color"))return new ColorFactory();else return null;} }//抽象工廠 abstract class AbstractFactoryShapeColor{public abstract Shape getShape(String shapeType);public abstract Color getColor(String colorType); }//Shape工廠 class ShapeFactory extends AbstractFactoryShapeColor{public Shape getShape(String shapeType) {if(shapeType==null)return null;if(shapeType.equals("Rectangle"))return new Rectangle();else if(shapeType.equals("Square"))return new Square();else if(shapeType.equals("Circle"))return new Circle();else return null;}@Overridepublic Color getColor(String colorType) {// TODO Auto-generated method stubreturn null;} }//Color工廠 class ColorFactory extends AbstractFactoryShapeColor{public Shape getShape(String shapeType) {if(shapeType==null)return null;if(shapeType.equals("Rectangle"))return new Rectangle();else if(shapeType.equals("Square"))return new Square();else if(shapeType.equals("Circle"))return new Circle();else return null;}@Overridepublic Color getColor(String colorType) {if(colorType==null)return null;if(colorType.equals("Red"))return new Red();else if(colorType.equals("Bule"))return new Blue();else if(colorType.equals("Green"))return new Green();else return null;} }//Shape屬性 interface Shape{void draw(); } class Rectangle implements Shape{public void draw() {System.out.println("Rectangle"); } } class Square implements Shape{public void draw() {System.out.println("Square"); } } class Circle implements Shape{public void draw() {System.out.println("Circle"); } }//Color屬性 interface Color{void fill(); } class Red implements Color{public void fill() {System.out.println("Red");} } class Blue implements Color{public void fill() {System.out.println("Blue");} } class Green implements Color{public void fill() {System.out.println("Green");} }代碼中分別建立了兩個工廠—color以及shape, 實現頂級抽象父類AbstractFactoryShapeColor, 最后新建一個實體工廠分別獲得color以及shape兩個工廠, 通過AbstractFactoryShapeColor的引用分別調用draw和fill方法對圖像的繪制和顏色的填充
體現了多個工廠, 多個抽象產品, 獲得抽象產品的方式延伸到子類中進行
總結
以上是生活随笔為你收集整理的设计模式_1_工厂模式与抽象工厂的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java ioc和aop的含义_Spri
- 下一篇: bootstrap3 表单构建器_Fas