操作
我们将创建一个Shape接口和一个实现它的具体类。下一步,我们创建一个抽象工厂类AbstractFactory。定义了工厂类ShapeFactory,它继承了AbstractFactory。创建工厂创建者/生成器类FactoryProducer。
AbstractFactoryPatternDemo,我们的演示类使用FactoryProducer来获取AbstractFactory对象。它将信息(CIRCLE / RECTANGLE / SQUARE)传递给AbstractFactory以获得所需的对象类型。
第1步 - 创建 Shape 接口 Shape.java 。
package com.jc2182.www;
public interface Shape {
void draw();
}
第2步 - 创建实现相同接口的具体类 RoundedRectangle.java。
package com.jc2182.www;
public class RoundedRectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside RoundedRectangle::draw() method.");
}
}
RoundedSquare.java
package com.jc2182.www;
public class RoundedSquare implements Shape {
@Override
public void draw() {
System.out.println("Inside RoundedSquare::draw() method.");
}
}
Rectangle.java
package com.jc2182.www;
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
Square.java
package com.jc2182.www;
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
第三步 - 创建一个Abstract类以获取常规的和圆角对象的工厂。 AbstractFactory.java
package com.jc2182.www;
public abstract class AbstractFactory {
abstract Shape getShape(String shapeType) ;
}
步骤4 - 创建扩展AbstractFactory的Factory类,以根据给定的信息生成具体类的对象。 ShapeFactory.java
package com.jc2182.www;
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;
}
}
RoundedShapeFactory.java
package com.jc2182.www;
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生成器/Producer类,以通过传递诸如Shape之类的信息来获取工厂 FactoryProducer.java
package com.jc2182.www;
public class FactoryProducer {
public static AbstractFactory getFactory(boolean rounded){
if(rounded){
return new RoundedShapeFactory();
}else{
return new ShapeFactory();
}
}
}
第6步
使用FactoryProducer来获取AbstractFactory,以便通过传递诸如类型之类的信息来获得具体类的工厂。 AbstractFactoryPatternDemo.java
package com.jc2182.www;
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
// 获取 shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory(false);
// 获取 一个 Rectangle 对象
Shape shape1 = shapeFactory.getShape("RECTANGLE");
// 调用 draw 方法
shape1.draw();
// 获取 一个 Square 对象
Shape shape2 = shapeFactory.getShape("SQUARE");
// 调用 draw 方法
shape2.draw();
// 获取 shape factory
AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true);
// 获取 一个 Rectangle 对象
Shape shape3 = shapeFactory1.getShape("RECTANGLE");
// 调用 draw 方法
shape3.draw();
// 获取 一个 Square 对象
Shape shape4 = shapeFactory1.getShape("SQUARE");
// 调用 draw 方法
shape4.draw();
}
}
步骤7 - 验证输出。
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside RoundedRectangle::draw() method.
Inside RoundedSquare::draw() method.