好长时间没有学习设计模式了,主要是最近一直在忙德国的一个需求,现在终于可以休息下了,不过下周又要开始新的需求了。唉,不断的项目,不停的工作。
现在闲下来
回头看看设计模式,发现我们的工作模式,其实可以用
Command
的模式实现的,我们都是按
boss
的
Command
办事的,呵呵。
1.
下面先讲讲我们的工作模式吧:
首先,德国那边
boss
提出要求,然后发给德国的经理,经理按照
boss
的意思,形成具体的需求样式,然后将这些样式发给我们,我们按照需求完成任务。
Boss---------->manager-------
>
developer
2.
看看设计模式中对
Command
的定义吧:
书上对
Command
的定义过于复杂,个人的理解是这样的:就是将一些操作封装成
command
命令,每个命令中含有
(execute)
,然后发给接受者,接受者不管
command
的类型或者内容,只要打开
command
命令,然后安装它的意图执行
(execute)
就可以了。
安装以上的理解,一个
Command
模式应该包括以下部分:
1.
Command:
2.
Client:
创建一个命令,并且设定它的接受者
3.
invoker
:要求执行这个命令
4.
Receiver
:接到命令,并且按照执行。
他们之间的静态图如下:
[Command
模式类图出自:
wikimedia.org]
3.
对照这个模式,就很容易将我的工作模式用这个模式实现了,下面看看整个过程吧:
很明显,我工作中的几个角色如下:
1.Command:
Requirement:
这里就是各种需求了。
2.Client:
创建一个命令,并且设定它的接受者
Boss:Boss
的作用
3.invoker
:
Manager:
根据老板的意思形成命令,并交给
developer
开发
4.Receiver
:接到命令,并且按照执行。
Developer:
得到需求后,找到需求的入口,然后按照这个要求一步步完成。
1.
Boss
类:
public class Boss {
String order;// 用于产生命令
Manager manager;// 指定命令的接受者
public Manager getReceiver() {
return manager;
}
public void setReceiver(Manager manager) {
this.manager = manager;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public void orderCommand() {
manager.setOrder(order);// 给manager下达命令
}
}
2.
Manager
类:
public class Manager {
String order;
Developer developer;
Requirement requirement = null;
public String getOrder() {
return order;
}
// 用于接受命令
public void setOrder(String order) {
this.order = order;
}
// 根据boss的命令 产生需求。
public void setRequirement() {
if (order.equals("A")) {
requirement = new RequirementA();
} else if (order.equals("B")) {
requirement = new RequirementB();
}
}
// 通知让developer工作
public void invoker() {
developer.setRequirement(requirement);// 告诉developer具体需求
developer.action();// 唤起developer
}
public Developer getDeveloper() {
return developer;
}
public void setDeveloper(Developer developer) {
this.developer = developer;
}
}
3.
Developer
类:
public class Developer {
Requirement requirement = null;
public Requirement getRequirement() {
return requirement;
}
// 得到需求。
public void setRequirement(Requirement requirement) {
this.requirement = requirement;
}
public void action() {
requirement.execute();// 只要按照要求做就可以了。
}
}
4.
Requirment
类:
public interface Requirement {
public void execute();
}
public class RequirementA implements Requirement {
private void stepFirst() {
System.out.println("stepFirst runs");
}
private void stepSecond() {
System.out.println("stepSecond runs");
}
public void execute() {
System.out.println("now operate requirementA");
// 先执行第一步,然后再执行第二步。
stepFirst();
stepSecond();
}
}
public class RequirementB implements Requirement {
private void stepFirst() {
System.out.println("stepFirst runs");
}
private void stepSecond() {
System.out.println("stepSecond runs");
}
public void execute() {
System.out.println("now operate requirementB");
// 不同于第一步
stepSecond();
stepFirst();
}
}
5.
看看测试过程吧:
这里我用的是
junit4
具体使用请参考我的另一篇文章
(junit4
快速入门
)
。
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
public class WorkflowTest {
String order = "";
Boss boss;
Developer developer;
Manager manager;
Requirement requirement;
@Before
public void init(){
boss = new Boss();
developer= new Developer();
manager = new Manager();
}
@Test
public void runRequirmentA(){
requirement = new RequirmentA();
order = "A";
//boss的工作
boss.setOrder(order);
boss.setReceiver(manager);
boss.orderCommand();
//manager的工作
manager.setDeveloper(developer);
manager.setRequirement(requirement);
manager.invoker();
}
@Test
public void runRequirmentB(){
requirement = new RequirmentB();
order = "B";
//boss的工作
boss.setOrder(order);
boss.setReceiver(manager);
boss.orderCommand();
//manager的工作
manager.setDeveloper(developer);
manager.setRequirement(requirement);
manager.invoker();
}
@After
public void destory(){
boss = null;
developer= null;
manager = null;
}
}
6. 输出结果:
now operate requirementA
stepFirst runs
stepSecond runs
now operate requirementB
stepSecond runs
stepFirst runs
分享到:
相关推荐
命令模式 Command 设计模式 若理解有问题,还请大S指教。
Command设计模式ppt 代码示例 设计模式 Command ppt 代码实例 撤销重做实例
VTK 作为一套开源的三维可视化开发库在国外越来越被广泛使用....本文从设计模式的角度,详细阐述了VTK 中消息事件响应的机制,并对VTK 这种设计模式的优点进行了分析,最后通过实例说明了VTK 设计模式的应用.
1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...
因此我们欢迎广大读者的批评与指正,无论从书中采用的实例、参考,还是我们遗漏的已知应用,或应该包含的设计模式等方面。你可以通过Addison-Wesley写信给我们,或发送电子邮件到:design-patterns@cs.uiuc.edu。你...
1.1 什么是设计模式 2 1.2 Smalltalk MVC 中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...
1.1什么是设计模式 2 1.2设计模式的作用 3 1.3GRASP模式的分类 4 1.4GoF设计模式的分类 4 1.5模式的学习阶段 6 第2章负责任地设计对象——GRASP 9 2.1InformationExpert(信息专家) 11 2.2Creator(创造者)...
本书设计实例从面向对象的设计中精选出23个设计模式,总结了面向对象设计中最有价值的经验,并且用简洁可复用的形式表达出来。本书分类描述了一组设计良好,表达清楚的软件设计模式,这些模式在实用环境下有特别有用...
设计模式实例,包含AbstractFactory、adapter、bridge、builder、chain、command等22个设计模式
摘要:数字地图制图实际上是...文中从数字地图制图软件的需求出发,对制图软件设计中常用设计模式(包括MVC模式、OBSERVER模式、COMPOSITE模式、COMMAND模式)的一般原理和结构进行简要介绍,并通过实例进行具体的说明。
1.1什么是设计模式 2 1.2设计模式的作用 3 1.3GRASP模式的分类 4 1.4GoF设计模式的分类 4 1.5模式的学习阶段 6 第2章负责任地设计对象——GRASP 9 2.1InformationExpert(信息专家) 11 2.2Creator(创造者)...
本书设计实例从面向对象的设计中精选出23个设计模式,总结了面向对象设计中最有价值的经验,并且用简洁可复用的形式表达出来。本书分类描述了一组设计良好,表达清楚的软件设计模式,这些模式在实用环境下有特别有用...
这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用 new 运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加灵活。 工厂模式(Factory Pattern) 抽象工厂模式...
1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...
1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...
1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 ...
最出名的设计模式,语言诙谐明了。 目 录 序言 前言 读者指南 第1章 引言 1 1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决...
1.2 Smalltalk MVC中的设计模式 1.3 描述设计模式 1.4 设计模式的编目 1.5 组织编目 1.6 设计模式怎样解决设计问题 1.7 怎样选择设计模式 1.8 怎样使用设计模式 第二章 实例研究:设计一个文档编辑器 2.1 设计问题...
1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...
1、命令模式 Command (人,开关,电灯) 2、策略模式 Strategy (用户选择各种排序方法进行排序) 3、简单工厂 Simple Factory (很多的产品,由一个工厂出产) 4、抽象工厂 Abstract Factory (很多的产品,分别由不同...