0
点赞
收藏
分享

微信扫一扫

【设计模式】创建型模式:抽象工厂模式


文章目录

  • ​​【设计模式】创建型模式:抽象工厂模式​​
  • ​​形状​​
  • ​​颜色​​
  • ​​构建工厂​​
  • ​​测试类​​

【设计模式】创建型模式:抽象工厂模式

定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行

【设计模式】创建型模式:抽象工厂模式_ide

简要分析,通过一个 Shape 类,有三个实现的子类,然后针对 Shape 创建一个工厂,实际上使用的时候,直接使用工厂即可,也就是工厂统一管理

形状

package cn.tellsea.designmode.shape;

/**
* 形状
*
* @author Tellsea
* @date 2022/9/26
*/
public interface Shape {

/**
* 绘画
*/
void draw();
}

package cn.tellsea.designmode.shape;

/**
* 圆形
*
* @author Tellsea
* @date 2022/9/26
*/
public class Circle implements Shape {

@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}

package cn.tellsea.designmode.shape;

/**
* 正方形
*
* @author Tellsea
* @date 2022/9/26
*/
public class Square implements Shape {

@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}

package cn.tellsea.designmode.shape;

/**
* 长方形
*
* @author Tellsea
* @date 2022/9/26
*/
public class Rectangle implements Shape {

@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}

构建工厂,根据传入的类型判断,需要构建哪种类型的实例

package cn.tellsea.designmode.shape;

/**
* 形状工厂
*
* @author Tellsea
* @date 2022/9/26
*/
public class ShapeFactory {

/**
* 获取指定类型的对象
*
* @param shapeType
* @return
*/
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;
}
}

颜色

package cn.tellsea.designmode.color;

/**
* 颜色
*
* @author Tellsea
* @date 2022/9/26
*/
public interface Color {

/**
* 填充
*/
void fill();
}

package cn.tellsea.designmode.color;

/**
* 蓝色
*
* @author Tellsea
* @date 2022/9/26
*/
public class Blue implements Color {

@Override
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}

package cn.tellsea.designmode.color;

/**
* 绿色
* @author Tellsea
* @date 2022/9/26
*/
public class Green implements Color {

@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}

package cn.tellsea.designmode.color;

/**
* 红色
*
* @author Tellsea
* @date 2022/9/26
*/
public class Red implements Color {

@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}

package cn.tellsea.designmode.color;

import cn.tellsea.designmode.AbstractFactory;
import cn.tellsea.designmode.shape.Shape;

/**
* 颜色工厂
*
* @author Tellsea
* @date 2022/9/26
*/
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;
}
}

构建工厂

在工厂模式的前提下,构建抽象工厂,实际上就是工厂模式的进一步封装

package cn.tellsea.designmode;

import cn.tellsea.designmode.color.Color;
import cn.tellsea.designmode.shape.Shape;

/**
* 抽象工厂
*
* @author Tellsea
* @date 2022/9/26
*/
public abstract class AbstractFactory {

/**
* 获取Color对象
*
* @param color
* @return
*/
public abstract Color getColor(String color);

/**
* 获取Shape对象
*
* @param shape
* @return
*/
public abstract Shape getShape(String shape);
}

package cn.tellsea.designmode;

import cn.tellsea.designmode.color.ColorFactory;
import cn.tellsea.designmode.shape.ShapeFactory;

/**
* 工厂创造器/生成器类
*
* @author Tellsea
* @date 2022/9/26
*/
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;
}
}

package cn.tellsea.designmode.shape;

/**
* 工厂模式
*
* @author Tellsea
* @date 2022/9/26
*/
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();
}
}

测试类

package cn.tellsea.designmode;

import cn.tellsea.designmode.color.Color;
import cn.tellsea.designmode.shape.Shape;

/**
* 抽象工厂模式
*
* @author Tellsea
* @date 2022/9/26
*/
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();
}
}


举报

相关推荐

0 条评论