zoukankan      html  css  js  c++  java
  • 命令模式

    1.命令模式是什么

    1.百度百科

    命令模式(Command Pattern)是一种设计模式,它尝试以物件来代表实际行动。

    2.维基百科

    In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
    Four terms always associated with the command pattern are command, receiver, invoker and client. A command object knows about receiver and invokes a method of the receiver. Values for parameters of the receiver method are stored in the command, the receiver object to execute these methods is also stored in the command object by aggregation. The receiver then does the work when the execute() method in command is called. An invoker object knows how to execute a command, and optionally does bookkeeping about the command execution. The invoker does not know anything about a concrete command, it knows only about the command interface. Invoker object(s), command objects and receiver objects are held by a client object, the client decides which receiver objects it assigns to the command objects, and which commands it assigns to the invoker. The client decides which commands to execute at which points. To execute a command, it passes the command object to the invoker object.

    3.lz理解

    在调用者和被调用者中间增加一个对象,使用调用者对象允许记录关于命令执行的便利,以及为由调用者对象管理的命令实现不同的模式,而不需要客户意识到存在簿记或模式。

    4.核心角色

    抽象命令角色 (Command):声明执行操作的接口。调用接收者相应的操作。

    具体命令角色(ConcreteCommand):命令接口实现对象,是“虚”的实现;通常会持有接收者,并调用接收者的功能来完成命令要执行的操作。创建一个具体命令对象并设定它的接收者。通常会持有接收者,并调用接收者的功能来完成命令要执行的操作。

    接收者角色(Receiver):真正执行命令的对象。任何类都可能成为一个接收者,只要它能够实现命令要求实现的相应功能。知道如何实施与执行一个请求相关的操作。任何类都可能作为一个接收者,只要它能够实现命令要求实现的相应功能。

    调用者角色(Invoker):要求命令对象执行请求,通常会持有命令对象,可以持有很多的命令对象。这个是客户端真正触发命令并要求命令执行相应操作的地方,也就是说相当于使用命令对象的入口。 要求该命令执行这个请求。通常会持有命令对象,可以持有很多的命令对象。

    2.命令模式解决了什么问题

    类间解耦:调用者角色与接收者角色之间没有任何依赖关系,调用者实现功能时只需调用Command 抽象类的execute方法就可以,不需要了解到底是哪个接收者执行。

    可扩展性:Command的子类可以非常容易地扩展,而调用者Invoker和高层次的模块Client不产生严 重的代码耦合。

    3.命令模式用法

    业务人员提出需求,交付经理,经理从中协调将任务分配给程序员。

    具体执行类,接收者角色。即具体处理功能的角色,也就是程序员。

    /**
     * 程序员接口 定义程序员能做的操作
     * (Receiver)任务接收者
     */
    public interface Programmer {
    
    	//编写bug
    	void modifyBug(String bugs);
    	//添加需求
    	void addRequests(String reqs);
    	//编写文档
    	void writeDocument(String docs);
    
    }
    
    public class ProgrammerImpl implements Programmer {
    
    	@Override
    	public void modifyBug(String bugs) {
    		System.out.println("程序员改bugs:" + bugs);
    	}
    
    	@Override
    	public void addRequests(String reqs) {
    		System.out.println("程序员写新需求:" + reqs);
    	}
    
    	@Override
    	public void writeDocument(String docs) {
    		System.out.println("程序员写文档:" + docs);
    	}
    
    }
    

    抽象命令角色和具体命令角色。也就是经理

    /**
     * 经理
     */
    public interface ProductManager {
    
    	public void modifyBug(String bugs);
    
    	public void addRequests(String reqs);
    
    	public void writeDocument(String docs);
    
    }
    
    //具体产品经理
    public class WangerProductManager implements ProductManager {
    	//程序员
    	private Programmer programmer;
    
    	public WangerProductManager() {
    		programmer = new ProgrammerImpl();
    	}
    
    	@Override
    	public void modifyBug(String bugs) {
    		programmer.modifyBug(bugs);
    	}
    
    	@Override
    	public void addRequests(String reqs) {
    		programmer.addRequests(reqs);
    	}
    
    	@Override
    	public void writeDocument(String docs) {
    		programmer.writeDocument(docs);
    	}
    
    }
    

    功能需求方,调用者角色。也就是业务人员、

    //业务人员
    public class BizPeople {
    	//产品经理
    	private ProductManager productManager;
    
    	public BizPeople(ProductManager productManager) {
    		this.productManager = productManager;
    	}
    
    	public void modifyBug(String bugs) {
    		this.productManager.modifyBug(bugs);
    	}
    	public void addRequests(String reqs) {
    		this.productManager.addRequests(reqs);
    	}
    	public void writeDocument(String docs) {
    		this.productManager.writeDocument(docs);
    	}
    
    }
    
    

    业务人员调用经理发出需求。

    
    public class Client {
    
    	public static void main(String[] args) {
    		//业务人员找来产品经理
    		ProductManager productManager = new WangerProductManager();
    		BizPeople bizPeople = new BizPeople(productManager);
    		//给产品经理一个需求
    		bizPeople.addRequests("添加一个页面");
    	}
    
    }
    
    

    4.命令模式的问题

    命令角色膨胀:如果有N个命令,问题就出来 了,Command的子类就可不是几个,而是N个,这个类膨胀得非常大,这个就需要读者在项 目中慎重考虑使用。

    具体命令类过多:用命令模式可能会导致某些系统有过多的具体命令类。因为针对每一个命令都需要设计一个具体命令类,因此某些系统可能需要大量具体命令类,这将影响命令模式的使用。

    5.命令模式总结

    命令模式应用场景:
    将命令发起者和命令执行者通过一层隔离开。

    请求-响应这种功能非常常见,而请求和响应中间是否需要加入一个命令层确实需要考虑,增加一层封装固然能使用项目耦合度降低,但是随之也带来了复杂性。业务的分散也造成代码难以理解。在面对是否要添加命令模式的时候还是那句话封装变化以保证业务的拓展性。如果变化比较小或者没有变化直接调用的方式可能比命令模式更加简洁明晰。

    http://blog.csdn.net/wwh578867817/article/details/51533263

  • 相关阅读:
    3 path核心模块
    2 http
    运输层:TCP 连接管理
    运输层:TCP 流量控制
    运输层:TCP 可靠数据传输
    计算及网路:性能指标
    MySQL 并发控制概述
    运输层:流水线可靠数据传输协议
    运输层:可靠数据传输原理(FSM 描述)
    计算机网络概述
  • 原文地址:https://www.cnblogs.com/yanlong300/p/8443867.html
Copyright © 2011-2022 走看看