当前位置: 首页 > article >正文

通过策略模式实现对象创建工厂

目录

案例:通过策略模式实现对象创建工厂

场景描述:

设计步骤:

注:


案例:通过策略模式实现对象创建工厂

场景描述:

假设要设计一个系统,用来创建不同类型的 Shape 对象(如 CircleSquareRectangle)。通常会使用工厂模式来封装对象创建的过程。但是,我们尝试用策略模式来实现这个功能,使得对象的创建方式在运行时动态调整。

设计步骤:
  • 定义Shape接口及其实现类:
interface Shape {
    void draw();
}

class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a Circle");
    }
}

class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a Square");
    }
}

class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a Rectangle");
    }
}
  • 定义创建策略接口及其实现类:
interface ShapeCreationStrategy {
    Shape createShape();
}

class CircleCreationStrategy implements ShapeCreationStrategy {
    @Override
    public Shape createShape() {
        return new Circle();
    }
}

class SquareCreationStrategy implements ShapeCreationStrategy {
    @Override
    public Shape createShape() {
        return new Square();
    }
}

class RectangleCreationStrategy implements ShapeCreationStrategy {
    @Override
    public Shape createShape() {
        return new Rectangle();
    }
}
  • 定义一个上下文类,用来选择和使用不同的策略:
    class ShapeFactoryContext {
        private ShapeCreationStrategy strategy;
    
        public void setStrategy(ShapeCreationStrategy strategy) {
            this.strategy = strategy;
        }
    
        public Shape createShape() {
            return strategy.createShape();
        }
    }
  • 客户端代码:
    public class Main {
        public static void main(String[] args) {
            ShapeFactoryContext context = new ShapeFactoryContext();
    
            // 使用CircleCreationStrategy
            context.setStrategy(new CircleCreationStrategy());
            Shape shape1 = context.createShape();
            shape1.draw(); // 输出: Drawing a Circle
    
            // 使用SquareCreationStrategy
            context.setStrategy(new SquareCreationStrategy());
            Shape shape2 = context.createShape();
            shape2.draw(); // 输出: Drawing a Square
    
            // 使用RectangleCreationStrategy
            context.setStrategy(new RectangleCreationStrategy());
            Shape shape3 = context.createShape();
            shape3.draw(); // 输出: Drawing a Rectangle
        }
    }

    注:

       以上通过策略模式实现了一个动态可变的“工厂”。没有使用传统的工厂模式,而是利用策略模式的思想,动态选择创建对象的策略。

 


http://www.kler.cn/news/304079.html

相关文章:

  • QMainWindow,调用exec()实现QDialog阻塞效果
  • HTTPS和HTTP区别是什么?
  • 【Python机器学习】循环神经网络(RNN)——利用Keras实现循环神经网络
  • 【XR】AR HUD
  • 交换机vlan配置实现
  • Java、python、php三个版本 抗震救灾物资管理系统 抗洪救灾物资分配系统 救援物资申请平台(源码、调试、LW、开题、PPT)
  • Android Framework(四)WMS-窗口显示流程——窗口创建与添加
  • TON的两种地址
  • Linux环境下运行 KF-GINS(GNSS+IMU松组合) 详细步骤
  • CGAL中的网格
  • PL/SQL 继承Oracle Database 的可靠性、安全性和可移植性
  • 初始爬虫1(补充)
  • 某讯/企鹅滑块验证码逆向(一)
  • 阿里云专业翻译api对接
  • OpenXR Monado创建跨进程通信通道 ipc_connect
  • SpringBoot常见面试题
  • 【前端】 flex布局详解
  • JavaScript StartsWith()方法
  • 从用户数据到区块链:Facebook如何利用去中心化技术
  • Postgresql 删除数组中的元素
  • Linux系统使用Docker安装DockerUI并实现远程管理本地容器无需公网IP
  • Prompt最佳实践|善用分隔符,让你的Prompt更清晰
  • 解决VScode中每次git push或者pull都需要重新输入账户名和密码问题
  • PL/SQL Developer工具的使用
  • 【数据结构初阶】队列接口实现及用队列实现栈超详解
  • 基于SpringBoot的高校电动车租赁服务管理系统
  • 智能化等保测评工具的兴起与应用实践
  • 房产销售系统:SpringBoot技术优化方案
  • 在已安装Python环境的基础上安装anaconda或者其他版本Python
  • 使用 RabbitMQ 实现秒杀订单系统的异步消息处理