一、抽象工廠模式
概述
抽象工廠模式(Abstract Factory Pattern)是圍繞一個(gè)超級(jí)工廠創(chuàng)建其他工廠。該超級(jí)工廠又稱(chēng)為其他工廠的工廠。這種類(lèi)型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對(duì)象的最佳方式
在抽象工廠模式中,接口是負(fù)責(zé)創(chuàng)建一個(gè)相關(guān)對(duì)象的工廠,不需要顯式指定它們的類(lèi)。每個(gè)生成的工廠都能按照工廠模式提供對(duì)象
抽象工廠模式提供了一種創(chuàng)建一系列相關(guān)或相互依賴(lài)對(duì)象的接口,而無(wú)需指定具體實(shí)現(xiàn)類(lèi)。通過(guò)使用抽象工廠模式,可以將客戶端與具體產(chǎn)品的創(chuàng)建過(guò)程解耦,使得客戶端可以通過(guò)工廠接口來(lái)創(chuàng)建一族產(chǎn)品
主要解決:主要解決接口選擇的問(wèn)題
何時(shí)使用:我們明確地計(jì)劃不同條件下創(chuàng)建不同實(shí)例時(shí)
優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
當(dāng)一個(gè)產(chǎn)品族中的多個(gè)對(duì)象被設(shè)計(jì)成一起工作時(shí),它能保證客戶端始終只使用同一個(gè)產(chǎn)品族中的對(duì)象
缺點(diǎn):
產(chǎn)品族擴(kuò)展非常困難,要增加一個(gè)系列的某一產(chǎn)品,既要在抽象的 Creator 里加代碼,又要在具體的里面加代碼
1. 各個(gè)角色介紹
1.1 抽象工廠(Abstract Factory)
聲明了一組用于創(chuàng)建產(chǎn)品對(duì)象的方法,每個(gè)方法對(duì)應(yīng)一種產(chǎn)品類(lèi)型。抽象工廠可以是接口或抽象類(lèi)
1.2 具體工廠(Concrete Factory)
實(shí)現(xiàn)了抽象工廠接口,負(fù)責(zé)創(chuàng)建具體產(chǎn)品對(duì)象的實(shí)例
1.3 抽象產(chǎn)品(Abstract Product)
定義了一組產(chǎn)品對(duì)象的共同接口或抽象類(lèi),描述了產(chǎn)品對(duì)象的公共方法
1.4 具體產(chǎn)品(Concrete Product)
實(shí)現(xiàn)了抽象產(chǎn)品接口,定義了具體產(chǎn)品的特定行為和屬性
2. UML圖
將創(chuàng)建 Shape 和 Color 接口和實(shí)現(xiàn)這些接口的實(shí)體類(lèi)。下一步是創(chuàng)建抽象工廠類(lèi) AbstractFactory。接著定義工廠類(lèi) ShapeFactory 和 ColorFactory,這兩個(gè)工廠類(lèi)都是擴(kuò)展了 AbstractFactory。然后創(chuàng)建一個(gè)工廠創(chuàng)造器/生成器類(lèi) FactoryProducer
AbstractFactoryPatternDemo 類(lèi)使用 FactoryProducer 來(lái)獲取 AbstractFactory 對(duì)象。它將向 AbstractFactory 傳遞形狀信息 Shape(CIRCLE / RECTANGLE / SQUARE),以便獲取它所需對(duì)象的類(lèi)型。同時(shí)它還向 AbstractFactory 傳遞顏色信息 Color(RED / GREEN / BLUE),以便獲取它所需對(duì)象的類(lèi)型
3. 具體例子和代碼
角色分配
-
Shape:形狀接口
- Circle:圓形(實(shí)現(xiàn)形狀接口)
- Rectangle:三角形(實(shí)現(xiàn)形狀接口)
- Square:正方形(實(shí)現(xiàn)形狀接口)
-
Color:形狀接口
- Red:圓形(實(shí)現(xiàn)形狀接口)
- Green:三角形(實(shí)現(xiàn)形狀接口)
- Blue:正方形(實(shí)現(xiàn)形狀接口)
-
AbstractFactory:抽象工廠
- ColorFactory:顏色工廠
- ShapeFactory:形狀工廠
-
FactoryProducer:工廠生產(chǎn)者
3.1 形狀接口以及實(shí)現(xiàn)類(lèi)
- Shape
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 形狀接口
*/
public interface Shape {
/**
* 繪圖
*/
void draw();
}
- Circle
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 實(shí)現(xiàn)形狀接口-圓形
*/
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
- Rectangle
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 實(shí)現(xiàn)形狀接口-長(zhǎng)方形
*/
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
- Square
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 實(shí)現(xiàn)形狀接口-正方形
*/
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
3.2 顏色接口以及實(shí)現(xiàn)類(lèi)
- Color
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 顏色接口
*/
public interface Color {
/**
* 顏色填充
*/
void fill();
}
- Red
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 繼承顏色接口-紅色
*/
public class Red implements Color {
@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
- Green
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 繼承顏色接口-綠色
*/
public class Green implements Color {
@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}
- Blue
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 繼承顏色接口-藍(lán)色
*/
public class Blue implements Color {
@Override
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}
3.3 抽象工廠類(lèi)以及實(shí)現(xiàn)類(lèi)
- AbstractFactory
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 抽象工廠
*/
public abstract class AbstractFactory {
/**
* 構(gòu)造顏色實(shí)體
*
* @param color 顏色名稱(chēng)
* @return 顏色實(shí)體
*/
public abstract Color getColor(String color);
/**
* 構(gòu)造形狀實(shí)體
*
* @param shape 形狀名稱(chēng)
* @return 形狀實(shí)體
*/
public abstract Shape getShape(String shape);
}
- ColorFactory
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 顏色工廠
*/
public class ColorFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType) {
return null;
}
@Override
public Color getColor(String color) {
if (color == null) {
return null;
}
if (color.equalsIgnoreCase("RED")) {
return new Red();
} else if (color.equalsIgnoreCase("GREEN")) {
return new Green();
} else if (color.equalsIgnoreCase("BLUE")) {
return new Blue();
}
return null;
}
}
- ShapeFacotry
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 形狀工廠
*/
public class ShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
// (優(yōu)化:這里可以通過(guò)反射來(lái)獲取)
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
} else if (shapeType.equalsIgnoreCase("SQUARE")) {
return new Square();
}
return null;
}
@Override
public Color getColor(String color) {
return null;
}
}
3.4 工廠生產(chǎn)者
- FactoryProducer
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
* @description 工廠生產(chǎn)者
*/
public class FactoryProducer {
/**
* 根據(jù)選擇獲取對(duì)應(yīng)的生產(chǎn)工廠
*
* @param choice 選擇類(lèi)型
* @return 具體的工廠
*/
public static AbstractFactory getFactory(String choice) {
if (choice.equalsIgnoreCase("SHAPE")) {
return new ShapeFactory();
} else if (choice.equalsIgnoreCase("COLOR")) {
return new ColorFactory();
}
return null;
}
}
3.5 測(cè)試主函數(shù)
package com.vinjcent.prototype.abstractFactory;
/**
* @author vinjcent
*/
public class Main {
public static void main(String[] args) {
// 1.獲取形狀工廠
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
// 2.獲取形狀為 Circle 的對(duì)象
Shape circle = shapeFactory.getShape("CIRCLE");
// 2.1 調(diào)用 Circle 的 draw 方法
circle.draw();
// 3.獲取形狀為 Rectangle 的對(duì)象
Shape rectangle = shapeFactory.getShape("RECTANGLE");
// 3.1 調(diào)用 Rectangle 的 draw 方法
rectangle.draw();
// 4.獲取形狀為 Square 的對(duì)象
Shape square = shapeFactory.getShape("SQUARE");
// 4.1 調(diào)用 Square 的 draw 方法
square.draw();
// 5.獲取顏色工廠
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
// 6.獲取顏色為 Red 的對(duì)象
Color red = colorFactory.getColor("RED");
// 6.1 調(diào)用 Red 的 fill 方法
red.fill();
// 7.獲取顏色為 Green 的對(duì)象
Color green = colorFactory.getColor("GREEN");
// 7.1調(diào)用 Green 的 fill 方法
green.fill();
// 8.獲取顏色為 Blue 的對(duì)象
Color blue = colorFactory.getColor("BLUE");
// 8.1調(diào)用 Blue 的 fill 方法
blue.fill();
}
}
- 測(cè)試結(jié)果
4. 使用場(chǎng)景
- QQ 換皮膚,一整套一起換
- 生成不同操作系統(tǒng)的程序
注意事項(xiàng):
產(chǎn)品族難擴(kuò)展,產(chǎn)品等級(jí)易擴(kuò)展文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-693782.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-693782.html
到了這里,關(guān)于Java特性之設(shè)計(jì)模式【抽象工廠模式】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!