举个Java工厂模式的实例
时间: 2023-05-20 16:05:09 浏览: 57
一个典型的Java工厂模式的实例是创建不同类型的汽车。我们可以定义一个抽象的汽车工厂接口,然后实现不同类型的汽车工厂类,如轿车工厂、SUV工厂等。每个工厂类都可以生产不同类型的汽车,如轿车工厂可以生产小轿车、中型轿车、豪华轿车等。这样,我们就可以通过工厂模式来创建不同类型的汽车,而不需要在客户端代码中直接实例化汽车对象。
相关问题
java抽象工厂模式实例
以下是一个Java抽象工厂模式的实例,该实例演示了如何使用抽象工厂模式创建不同操作系统下的按钮和文本框:
```java
// 创建抽象工厂接口
interface GUIFactory {
Button createButton();
TextField createTextField();
}
// 创建具体工厂类 - Windows工厂
class WindowsFactory implements GUIFactory {
public Button createButton() {
return new WindowsButton();
}
public TextField createTextField() {
return new WindowsTextField();
}
}
// 创建具体工厂类 - MacOS工厂
class MacOSFactory implements GUIFactory {
public Button createButton() {
return new MacOSButton();
}
public TextField createTextField() {
return new MacOSTextField();
}
}
// 创建抽象产品接口 - 按钮
interface Button {
void paint();
}
// 创建具体产品类 - Windows按钮
class WindowsButton implements Button {
public void paint() {
System.out.println("Windows Button");
}
}
// 创建具体产品类 - MacOS按钮
class MacOSButton implements Button {
public void paint() {
System.out.println("MacOS Button");
}
}
// 创建抽象产品接口 - 文本框
interface TextField {
void paint();
}
// 创建具体产品类 - Windows文本框
class WindowsTextField implements TextField {
public void paint() {
System.out.println("Windows TextField");
}
}
// 创建具体产品类 - MacOS文本框
class MacOSTextField implements TextField {
public void paint() {
System.out.println("MacOS TextField");
}
}
// 创建客户端类
class Application {
private Button button;
private TextField textField;
public Application(GUIFactory factory) {
button = factory.createButton();
textField = factory.createTextField();
}
public void paint() {
button.paint();
textField.paint();
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
// 创建Windows工厂
GUIFactory factory = new WindowsFactory();
// 创建应用程序
Application app = new Application(factory);
// 绘制UI
app.paint();
// 创建MacOS工厂
factory = new MacOSFactory();
// 创建应用程序
app = new Application(factory);
// 绘制UI
app.paint();
}
}
```
Java工厂模式代码实例
当然可以,以下是一个简单的 Java 工厂模式代码实例:
```
// 定义一个接口
interface Shape {
void draw();
}
// 实现接口的类
class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
// 工厂类
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;
}
}
// 使用工厂类获取对象
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
// 获取 Circle 的对象,并调用它的 draw 方法
Shape shape1 = shapeFactory.getShape("CIRCLE");
// 调用 Circle 的 draw 方法
shape1.draw();
// 获取 Rectangle 的对象,并调用它的 draw 方法
Shape shape2 = shapeFactory.getShape("RECTANGLE");
// 调用 Rectangle 的 draw 方法
shape2.draw();
// 获取 Square 的对象,并调用它的 draw 方法
Shape shape3 = shapeFactory.getShape("SQUARE");
// 调用 Square 的 draw 方法
shape3.draw();
}
}
```
希望这个例子能够帮助你理解工厂模式的实现方式。