public class ShapeFactory { //使用 getShape 方法获取形状类型的对象 public Shape getShape(String shapeType){ if(shapeType == null){ return null; } 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; } }
时间: 2024-04-18 17:31:12 浏览: 18
您好!根据您提供的代码,这是一个简单的工厂类 ShapeFactory。它提供了一个方法 getShape,根据传入的形状类型参数来获取对应的形状对象。
如果传入的形状类型是 "CIRCLE",它会返回一个 Circle 对象;如果是 "RECTANGLE",则返回一个 Rectangle 对象;如果是 "SQUARE",则返回一个 Square 对象。如果传入的形状类型不是这些值,它将返回 null。
这个工厂类的作用是为了简化创建不同形状对象的过程,通过调用 getShape 方法,可以方便地获取所需的形状对象。
相关问题
使用工厂模式设计一个可以创建不同几何形状(Shape)
好的,可以使用工厂模式来实现这个需求。首先需要定义一个 Shape 接口,然后定义不同的实现类,如 Circle、Rectangle 等。接着创建一个工厂类 ShapeFactory,其中包含一个静态方法 getShape(String shapeType),根据传入的参数来返回相应的 Shape 实例。
以下是示例代码:
```java
// Shape 接口
public interface Shape {
void draw();
}
// Circle 类
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}
// Rectangle 类
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a rectangle.");
}
}
// ShapeFactory 工厂类
public class ShapeFactory {
public static Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
}
return null;
}
}
```
使用示例:
```java
Shape circle = ShapeFactory.getShape("CIRCLE");
circle.draw(); // 输出 "Drawing a circle."
Shape rectangle = ShapeFactory.getShape("RECTANGLE");
rectangle.draw(); // 输出 "Drawing a rectangle."
```
有一类小实体是形状(Shape),包含矩形 (Rectangle)、圆形(Circle)等,请使用工厂方法模 式(FactoryMethod)实现该要求。 1)画出类结构图。 2)采用面向对象编程语言实现该工厂方法模式,并写测试程序main0进行测试。
好的,我知道了。以下是类结构图:
```
+--------------+
| Shape |
+--------------+
/\
|
+-------------+--------------+
| |
+------+ +-------+
|Rect | |Circle |
+------+ +-------+
```
以下是使用Java语言实现该工厂方法模式:
```java
// Shape.java
public interface Shape {
void draw();
}
// Rectangle.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
// Circle.java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
// ShapeFactory.java
public class ShapeFactory {
public Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
switch (shapeType) {
case "RECTANGLE":
return new Rectangle();
case "CIRCLE":
return new Circle();
default:
return null;
}
}
}
// Main.java
public class Main {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
// 获取Rectangle对象并调用它的draw方法
Shape shape1 = shapeFactory.getShape("RECTANGLE");
shape1.draw();
// 获取Circle对象并调用它的draw方法
Shape shape2 = shapeFactory.getShape("CIRCLE");
shape2.draw();
}
}
```
运行main方法后,控制台输出:
```
Inside Rectangle::draw() method.
Inside Circle::draw() method.
```