码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 设计模式——命令模式(Command Pattern)+ Spring相关源码


    文章目录

    • 一、命令模式定义
    • 二、例子
      • 2.1 菜鸟教程例子
        • 2.1.1 定义命令类接口
        • 2.1.2 定义命令执行者
        • 2.1.3 被处理对象Stock。
        • 2.1.4 封装处理Stock的命令
      • 2.2 JDK源码——Runnable
        • 2.2.1 命令接口
        • 2.2.2 命令处理者
        • 2.2.3 命令实现类
      • 2.3 SpringMVC——Controller
        • 2.3.1 请求对象 handler
        • 2.3.2 请求执行者 HandlerAdapter
    • 三、其他设计模式

    一、命令模式定义

    类型: 行为型模式
    目的: 将一个请求封装成一个对象,从而使您可以用不同的请求对客户进行参数化。

    二、例子

    2.1 菜鸟教程例子

    2.1.1 定义命令类接口

    public interface Order {
       void execute();
    }
    
    • 1
    • 2
    • 3

    2.1.2 定义命令执行者

    import java.util.ArrayList;
    import java.util.List;
     
    public class Broker {
       private List<Order> orderList = new ArrayList<Order>(); 
     
       public void takeOrder(Order order){
          orderList.add(order);      
       }
     
       public void placeOrders(){
          for (Order order : orderList) {
             order.execute();
          }
          orderList.clear();
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.1.3 被处理对象Stock。

    菜鸟教程定义被处理的对象是Stock。

    public class Stock {
       
       private String name = "ABC";
       private int quantity = 10;
     
       public void buy(){
          System.out.println("Stock [ Name: "+name+", 
             Quantity: " + quantity +" ] bought");
       }
       public void sell(){
          System.out.println("Stock [ Name: "+name+", 
             Quantity: " + quantity +" ] sold");
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.1.4 封装处理Stock的命令

    public class BuyStock implements Order {
       private Stock abcStock;
     
       public BuyStock(Stock abcStock){
          this.abcStock = abcStock;
       }
     
       public void execute() {
          abcStock.buy();
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    public class SellStock implements Order {
       private Stock abcStock;
     
       public SellStock(Stock abcStock){
          this.abcStock = abcStock;
       }
     
       public void execute() {
          abcStock.sell();
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.2 JDK源码——Runnable

    虽然 Runnable 接口本身并不是一个命令模式的实现,但结合Thread和自定义的Runnable 实现类可以认为它运用了命令模式。

    2.2.1 命令接口

    public interface Runnable {
        public abstract void run();
    }
    
    
    • 1
    • 2
    • 3
    • 4

    2.2.2 命令处理者

    
    public class Thread implements Runnable {
        private Runnable target;
        
        public synchronized void start() {
            if (threadStatus != 0)
                throw new IllegalThreadStateException();
    ​
            group.add(this);
    ​
            boolean started = false;
            try {
                start0();
                started = true;
            } finally {
                try {
                    if (!started) {
                        group.threadStartFailed(this);
                    }
                } catch (Throwable ignore) {
                }
            }
        }
        
        private native void start0();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    2.2.3 命令实现类

    public class OrderThread {
        public void run(){
    	   	 //todo
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.3 SpringMVC——Controller

    个人认为Controller也是运用了命令模式。

    2.3.1 请求对象 handler

    2.3.2 请求执行者 HandlerAdapter

    HandlerAdapter光看名字就知道运用了适配模式,我们就看handle方法部分就好了。

    public class SimpleControllerHandlerAdapter implements HandlerAdapter {
        public SimpleControllerHandlerAdapter() {
        }
    
        public boolean supports(Object handler) {
            return handler instanceof Controller;
        }
    
        @Nullable
        public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            return ((Controller)handler).handleRequest(request, response);
        }
    
        public long getLastModified(HttpServletRequest request, Object handler) {
            return handler instanceof LastModified ? ((LastModified)handler).getLastModified(request) : -1L;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    三、其他设计模式

    创建型模式
    结构型模式

    • 1、设计模式——装饰器模式(Decorator Pattern)+ Spring相关源码

    行为型模式

    • 1、设计模式——访问者模式(Visitor Pattern)+ Spring相关源码
    • 2、设计模式——中介者模式(Mediator Pattern)+ JDK相关源码
    • 3、设计模式——策略模式(Strategy Pattern)+ Spring相关源码
    • 4、设计模式——状态模式(State Pattern)
    • 5、设计模式——命令模式(Command Pattern)+ Spring相关源码
    • 6、设计模式——观察者模式(Observer Pattern)+ Spring相关源码
    • 7、设计模式——备忘录模式(Memento Pattern)
    • 8、设计模式——模板方法模式(Template Pattern)+ Spring相关源码
    • 9、设计模式——迭代器模式(Iterator Pattern)+ Spring相关源码
    • 10、设计模式——责任链模式(Chain of Responsibility Pattern)+ Spring相关源码
    • 11、设计模式——解释器模式(Interpreter Pattern)+ Spring相关源码
  • 相关阅读:
    元素跟随鼠标移动
    【js】-【贪心算法】-笔记
    超越OpenCV速度的MorphologyEx函数实现(特别是对于二值图,速度是CV的4倍左右)。
    机器学习介绍(上)
    网络安全(黑客)技术——自学2024
    Spring 源码(1)Spring IOC Bean 创建的整体流程
    使用shell脚本编写监控系统资源(CPU,内存,磁盘)使用情况
    CentOS 7 mysql 安装以及常用语句(select、update、alter、rename、drop等)速查
    数据结构实战开发教程(五)再论智能指针、循环链表的实现、双向链表的实现、双向循环链表的实现、Linux内核链表剖析
    vue 的 render 函数的用法:new Vue({ render: h => h(App), }).$mount(‘#app‘)
  • 原文地址:https://blog.csdn.net/malu_record/article/details/134225822
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号