更新時間:2021-10-18 13:40:56 來源:動力節點 瀏覽1221次
抽象工廠模式圍繞創建其他工廠的超級工廠工作。這個工廠也被稱為工廠的工廠。這種類型的設計模式屬于創建模式,因為這種模式提供了創建對象的最佳方式之一。
在抽象工廠模式中,接口負責創建相關對象的工廠,而無需明確指定它們的類。每個生成的工廠都可以按照工廠模式提供對象。
我們將創建一個 Shape 接口和一個實現它的具體類。我們創建一個抽象工廠類 AbstractFactory 作為下一步。定義了工廠類 ShapeFactory,它擴展了 AbstractFactory。創建了一個工廠創建者/生成器類 FactoryProducer。
AbstractFactoryPatternDemo,我們的演示類使用 FactoryProducer 來獲取 AbstractFactory 對象。它將信息(圓形/矩形/方形)傳遞給 AbstractFactory 以獲取它需要的對象類型。
第1步
為 Shapes 創建一個界面。
形狀.java
public interface Shape {
void draw();
}
第2步
創建實現相同接口的具體類。
圓角矩形.java
public class RoundedRectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside RoundedRectangle::draw() method.");
}
}
public class RoundedSquare implements Shape {
@Override
public void draw() {
System.out.println("Inside RoundedSquare::draw() method.");
}
}
矩形.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
第3步
創建一個抽象類來獲取普通和圓角對象的工廠。
抽象工廠.java
public abstract class AbstractFactory {
abstract Shape getShape(String shapeType) ;
}
第4步
創建擴展 AbstractFactory 的工廠類以根據給定的信息生成具體類的對象。
形狀工廠
public class ShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
}
public class RoundedShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new RoundedRectangle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new RoundedSquare();
}
return null;
}
}
第5步
創建一個Factory生成器/生產者類,通過傳遞Shape等信息來獲取工廠
工廠生產者.java
public class FactoryProducer {
public static AbstractFactory getFactory(boolean rounded){
if(rounded){
return new RoundedShapeFactory();
}else{
return new ShapeFactory();
}
}
}
第6步
使用 FactoryProducer 獲取 AbstractFactory 以通過傳遞類型等信息來獲取具體類的工廠。
AbstractFactoryPatternDemo.java
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
//get shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory(false);
//get an object of Shape Rectangle
Shape shape1 = shapeFactory.getShape("RECTANGLE");
//call draw method of Shape Rectangle
shape1.draw();
//get an object of Shape Square
Shape shape2 = shapeFactory.getShape("SQUARE");
//call draw method of Shape Square
shape2.draw();
//get shape factory
AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true);
//get an object of Shape Rectangle
Shape shape3 = shapeFactory1.getShape("RECTANGLE");
//call draw method of Shape Rectangle
shape3.draw();
//get an object of Shape Square
Shape shape4 = shapeFactory1.getShape("SQUARE");
//call draw method of Shape Square
shape4.draw();
}
}
第7步
驗證輸出。
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside RoundedRectangle::draw() method.
Inside RoundedSquare::draw() method.
以上就是抽象工廠設計模式詳解,如果您想了解更多關于Java的知識,不妨來關注一下動力節點的Java在線學習,里面有更多的教程在等著大家,希望對大家的學習能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習