工厂模式重温,包含抽象工厂

工厂模式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
步骤 1
创建一个接口:

Shape.java
public interface Shape {
void draw();
}
步骤 2
创建实现接口的实体类。

Rectangle.java
public class Rectangle implements Shape {

@Override
public void draw() {
System.out.println(“Inside Rectangle::draw() method.”);
}
}
Square.java
public class Square implements Shape {

@Override
public void draw() {
System.out.println(“Inside Square::draw() method.”);
}
}
Circle.java
public class Circle implements Shape {

@Override
public void draw() {
System.out.println(“Inside Circle::draw() method.”);
}
}
步骤 3
创建一个工厂,生成基于给定信息的实体类的对象。

ShapeFactory.java
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;
}
}
步骤 4
使用该工厂,通过传递类型信息来获取实体类的对象。

FactoryPatternDemo.java
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();

}
}
步骤 5
执行程序,输出结果:

Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
步骤 1
为形状创建一个接口。

Shape.java
public interface Shape {
void draw();
}
步骤 2
创建实现接口的实体类。

Rectangle.java

Rectangle.java
public class Rectangle implements Shape {

@Override
public void draw() {
System.out.println(“Inside Rectangle::draw() method.”);
}
}
Square.java
public class Square implements Shape {

@Override
public void draw() {
System.out.println(“Inside Square::draw() method.”);
}
}
Circle.java
public class Circle implements Shape {

@Override
public void draw() {
System.out.println(“Inside Circle::draw() method.”);
}
}
步骤 3
为颜色创建一个接口。

Color.java
public interface Color {
void fill();
}
步骤4
创建实现接口的实体类。

Red.java
public class Red implements Color {

@Override
public void fill() {
System.out.println(“Inside Red::fill() method.”);
}
}
Green.java
public class Green implements Color {

@Override
public void fill() {
System.out.println(“Inside Green::fill() method.”);
}
}
Blue.java
public class Blue implements Color {

@Override
public void fill() {
System.out.println(“Inside Blue::fill() method.”);
}
}
步骤 5
为 Color 和 Shape 对象创建抽象类来获取工厂。

AbstractFactory.java
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape);
}
步骤 6
创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。

ShapeFactory.java
public class ShapeFactory extends AbstractFactory {

@Override
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;
}

@Override
public Color getColor(String color) {
return null;
}
}
ColorFactory.java
public class ColorFactory extends AbstractFactory {

@Override
public Shape getShape(String shapeType){
return null;
}

@Override
public Color getColor(String color) {
if(color == null){
return null;
}
if(color.equalsIgnoreCase(“RED”)){
return new Red();
} else if(color.equalsIgnoreCase(“GREEN”)){
return new Green();
} else if(color.equalsIgnoreCase(“BLUE”)){
return new Blue();
}
return null;
}
}
步骤 7
创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。

FactoryProducer.java
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase(“SHAPE”)){
return new ShapeFactory();
} else if(choice.equalsIgnoreCase(“COLOR”)){
return new ColorFactory();
}
return null;
}
}
步骤 8
使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象。

AbstractFactoryPatternDemo.java
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {

  //获取形状工厂
  AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

  //获取形状为 Circle 的对象
  Shape shape1 = shapeFactory.getShape("CIRCLE");

  //调用 Circle 的 draw 方法
  shape1.draw();

  //获取形状为 Rectangle 的对象
  Shape shape2 = shapeFactory.getShape("RECTANGLE");

  //调用 Rectangle 的 draw 方法
  shape2.draw();
  
  //获取形状为 Square 的对象
  Shape shape3 = shapeFactory.getShape("SQUARE");

  //调用 Square 的 draw 方法
  shape3.draw();

  //获取颜色工厂
  AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

  //获取颜色为 Red 的对象
  Color color1 = colorFactory.getColor("RED");

  //调用 Red 的 fill 方法
  color1.fill();

  //获取颜色为 Green 的对象
  Color color2 = colorFactory.getColor("GREEN");

  //调用 Green 的 fill 方法
  color2.fill();

  //获取颜色为 Blue 的对象
  Color color3 = colorFactory.getColor("BLUE");

  //调用 Blue 的 fill 方法
  color3.fill();

}
}
步骤 9
执行程序,输出结果:

Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside Red::fill() method.
Inside Green::fill() method.
Inside Blue::fill() method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoyutoucom

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值