更新時間:2021-10-18 13:40:56 來源:動力節(jié)點(diǎn) 瀏覽1356次
抽象工廠模式圍繞創(chuàng)建其他工廠的超級工廠工作。這個工廠也被稱為工廠的工廠。這種類型的設(shè)計模式屬于創(chuàng)建模式,因為這種模式提供了創(chuàng)建對象的最佳方式之一。
在抽象工廠模式中,接口負(fù)責(zé)創(chuàng)建相關(guān)對象的工廠,而無需明確指定它們的類。每個生成的工廠都可以按照工廠模式提供對象。
我們將創(chuàng)建一個 Shape 接口和一個實(shí)現(xiàn)它的具體類。我們創(chuàng)建一個抽象工廠類 AbstractFactory 作為下一步。定義了工廠類 ShapeFactory,它擴(kuò)展了 AbstractFactory。創(chuàng)建了一個工廠創(chuàng)建者/生成器類 FactoryProducer。
AbstractFactoryPatternDemo,我們的演示類使用 FactoryProducer 來獲取 AbstractFactory 對象。它將信息(圓形/矩形/方形)傳遞給 AbstractFactory 以獲取它需要的對象類型。
第1步
為 Shapes 創(chuàng)建一個界面。
形狀.java
public interface Shape {
void draw();
}
第2步
創(chuàng)建實(shí)現(xiàn)相同接口的具體類。
圓角矩形.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步
創(chuàng)建一個抽象類來獲取普通和圓角對象的工廠。
抽象工廠.java
public abstract class AbstractFactory {
abstract Shape getShape(String shapeType) ;
}
第4步
創(chuàng)建擴(kuò)展 AbstractFactory 的工廠類以根據(jù)給定的信息生成具體類的對象。
形狀工廠
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步
創(chuàng)建一個Factory生成器/生產(chǎn)者類,通過傳遞Shape等信息來獲取工廠
工廠生產(chǎn)者.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.
以上就是抽象工廠設(shè)計模式詳解,如果您想了解更多關(guān)于Java的知識,不妨來關(guān)注一下動力節(jié)點(diǎn)的Java在線學(xué)習(xí),里面有更多的教程在等著大家,希望對大家的學(xué)習(xí)能夠有所幫助。
初級 202925
初級 203221
初級 202629
初級 203743