ITeye博客

我工作的那点事--学习《设计模式》实例应用(decorator模式)

biqing0427 · 2008-01-24 12:52 · Design Pattern · 工作 · 设计模式 · Markdown

今天是我们公司写周报的日子,又要回顾一下一周的工作、学习任务了,感觉这周没有做什么事情,项目刚结束,只有看看书了。

上午刚好看到Decorator这个模式,为了增强学习,就将周报这个实例和它强行绑在一起了,呵呵。

这里还是简单的介绍一下Decorator模式吧: 定义: 在不改变原来对象的结构基础上,动态给一个对象添加一些额外的职责.如果使用子类继承的话,对于每个类的改变都要产生新类,增加了开销。现在这些功能需要由用户动态决定加入的方式和时机.

下面用实例说明一下:

公司的周报汇报关系是:开发员->项目经理->部门经理->CTO

先生成周报吧:

  1. WeeklyReport
public interface WeeklyReport {
//这里为了简单说明 只理出周报内容
public String decription();
}

下面我们假设有FellowOne,FellowTwo,FellowThree三个开发员

每个员工只有写周报内容这一个功能。

//FellowOne
public class FellowOne implements WeeklyReport {
 private String content = "";

 public void setContent(String content) {
  this.content = content;
 }

 public String getContent() {
  return content;
 }

 public String decription() {
  String retStr = "";
  retStr = "FellowOne:" + content;
  return retStr;
 }

}

//FellowTwo
public class FellowTwo implements WeeklyReport {
  private String content = "";

  public void setContent(String content) {
    this.content = content;
  };

 public String getContent() {
   return content;
 }

 public String decription() {
   String retStr = "";
   retStr = "FellowTwo:" + content;
   return retStr;
 }
}

  //FellowThree
public class FellowThree implements WeeklyReport{
  private String content = "";

  public void setContent(String content) {
    this.content = content;
  };
 
  public String decription() {
    String retStr = "";
    retStr = "FellowThree:" + content;
    return retStr;
  }

  public String getContent() {
    return content;
  }
}

现在开发员的周报写好了,将要发给项目经理,经理肯定要对每个人的周报进行修改,再加上自己的工作内容,才能一起发给部门经理。

如果经理对每个员工的周报进行修改都产生一个类,那么将新增加3个类,如果有100个员工,那将增加100个子类,太庞大了,下面看看Decorator的作用吧,一个类搞定。

2.经理的周报:Decorator 当然也是周报的一种,所以肯定也是implements WeeklyReport了。

public class Decorator implements WeeklyReport {

private WeeklyReport report;
private List list = new ArrayList();

//默认对一份周报进行修改
public Decorator(WeeklyReport report) {
  this.report = report;
}

//一个经理下面肯定有很多开发者的,这里增加他们的周报
public void addWeeklyReport(WeeklyReport report) {
  list.add(report);
}

//经理将所有的周报整理 加上自己的东西,形成周报
public String decription() {
  String retStr = "nmy Team this week do works:n";
  retStr += report.decription() + "n";
  for (int i = 0; i < list.size(); i++) {
    retStr += ((WeeklyReport) list.get(i)).decription()+"n";
  }
  System.out.println(retStr);
  return retStr;
}
}

3.让我们看看部门经理的周报 是怎么写的吧: 同理部门经理面对的是各个项目经理

public class SubDecorator implements WeeklyReport {

//很明显,部门经理关心的也是周报,
//不过只是从项目经理那边发过来的
private WeeklyReport report;

public SubDecorator(WeeklyReport report) {
  this.report = report;
}

//对每份周报进行处理
public String decription() {
  String retStr = "nmy department this week finish:";
  System.out.println(retStr+report.decription());
  return retStr;
}
}

为了简化程序,这里就认为是一个部门经理下面对应一个项目经理,而且默认部门经理不再向上汇报了。

4.让我们看看整个周报的流程吧:

FellowOne one = new FellowOne();
FellowTwo two = new FellowTwo();
FellowThree three = new FellowThree();

one.setContent("study");//fellowOne写周报
two.setContent("nothing");//fellowTwo写周报
three.setContent("finish part A");//fellowThree的周报

//看看项目经理怎么写周报的
Decorator decorator = new Decorator(one);
decorator.addWeeklyReport(two);
decorator.addWeeklyReport(three);

//部门经理得到的只是项目经理的周报,多简单,
//不用管开发者的具体工作,
//项目经理已经写好了
SubDecorator subdec = new SubDecorator(decorator);
subdec.decription();

//项目经理的周报 my Team this week do works: FellowOne:study FellowTwo:nothing FellowThree:finish part A

//部门经理的周报 my department this week finish: my Team this week do works: FellowOne:study FellowTwo:nothing FellowThree:finish part A

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。