我来帮您设计一个使用工厂方法模式的图形绘制系统。
(1)UML类图
+------------------+
| <<interface>> |
| Shape |
+------------------+
| + draw(): void |
+------------------+
▲
|
+----------------+----------------+
| | |
+--------------+ +--------------+ +--------------+
| Circle | | Rectangle | | Square |
+--------------+ +--------------+ +--------------+
| + draw() | | + draw() | | + draw() |
+--------------+ +--------------+ +--------------+
+----------------------+
| <<abstract>> |
| ShapeFactory |
+----------------------+
| + createShape(): Shape|
+----------------------+
▲
|
+----------------+----------------+
| | |
+----------------+ +----------------+ +----------------+
| CircleFactory | |RectangleFactory| | SquareFactory |
+----------------+ +----------------+ +----------------+
| + createShape()| | + createShape()| | + createShape()|
+----------------+ +----------------+ +----------------+
+------------------+
| Client |
+------------------+
| - factory: ShapeFactory |
| + main() |
+------------------+
(2)Java代码实现
Shape接口
// Shape.java
public interface Shape {
void draw();
}
具体图形类
// Circle.java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("绘制圆形");
System.out.println(" *** ");
System.out.println(" * * ");
System.out.println("* *");
System.out.println(" * * ");
System.out.println(" *** ");
}
}
// Rectangle.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("绘制矩形");
System.out.println("* * * * * * *");
System.out.println("* *");
System.out.println("* *");
System.out.println("* * * * * * *");
}
}
// Square.java
public class Square implements Shape {
@Override
public void draw() {
System.out.println("绘制正方形");
System.out.println("* * * * *");
System.out.println("* *");
System.out.println("* *");
System.out.println("* *");
System.out.println("* * * * *");
}
}
抽象工厂类
// ShapeFactory.java
public abstract class ShapeFactory {
public abstract Shape createShape();
}
具体工厂类
// CircleFactory.java
public class CircleFactory extends ShapeFactory {
@Override
public Shape createShape() {
return new Circle();
}
}
// RectangleFactory.java
public class RectangleFactory extends ShapeFactory {
@Override
public Shape createShape() {
return new Rectangle();
}
}
// SquareFactory.java
public class SquareFactory extends ShapeFactory {
@Override
public Shape createShape() {
return new Square();
}
}
客户端类
// Client.java
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ShapeFactory factory = null;
while (true) {
System.out.println("\n======== 图形绘制系统 ========");
System.out.println("请选择要绘制的图形类型:");
System.out.println("1. 圆形");
System.out.println("2. 矩形");
System.out.println("3. 正方形");
System.out.println("0. 退出");
System.out.print("请输入选择(0-3):");
int choice = scanner.nextInt();
switch (choice) {
case 1:
factory = new CircleFactory();
break;
case 2:
factory = new RectangleFactory();
break;
case 3:
factory = new SquareFactory();
break;
case 0:
System.out.println("感谢使用,再见!");
scanner.close();
return;
default:
System.out.println("无效的选择,请重新输入!");
continue;
}
// 使用工厂创建并绘制图形
Shape shape = factory.createShape();
System.out.println("\n绘制结果:");
shape.draw();
}
}
}
扩展示例(演示系统的可扩展性)
如果需要添加新的图形类型(如三角形),只需要:
// Triangle.java
public class Triangle implements Shape {
@Override
public void draw() {
System.out.println("绘制三角形");
System.out.println(" * ");
System.out.println(" * * ");
System.out.println(" * * ");
System.out.println(" * * ");
System.out.println("* * * * *");
}
}
// TriangleFactory.java
public class TriangleFactory extends ShapeFactory {
@Override
public Shape createShape() {
return new Triangle();
}
}
工厂方法模式的优点:
- 符合开闭原则:新增图形类型时,只需添加新的产品类和对应的工厂类,无需修改现有代码
- 解耦:客户端不需要知道具体的产品类名,只需要知道对应的工厂即可
- 单一职责:每个工厂只负责创建一种产品
- 多态性:所有产品都实现同一接口,可以统一处理
这个设计充分体现了工厂方法模式的核心思想:将对象的创建委托给专门的工厂子类来完成。