设计模式学习笔记(二)之观察者模式、装饰者模式

观察者模式的简单理解:

多个观察者时刻关注主题的动态,主题一旦有变化及时反馈。

 

实现:

抽象观察者:

抽象类声明一个方法:对主题的变化做出反馈。

1 public abstract class Observer {
2 
3     public abstract void Update();
4     
5 }

 

抽象主题:

一个字段:抽象观察者的集合。一个主题对应多个观察者,是一对多的一种体现。

三个方法:addObserver(添加观察者),delObserver(删除观察者)。

Notify(唤醒观察者):提醒观察者已经发生变化。遍历集合里的所有观察者,使每个观察者做出反馈。

 1 public abstract class Subject {
 2 
 3     private List<Observer> list=new ArrayList<Observer>();
 4     
 5     public void addObserver(Observer observer){
 6         list.add(observer);
 7     }
 8     public void delObserver(Observer observer){
 9         list.remove(observer);
10     }
11     public void Notify(){
12         for(Observer o:list){
13             
14             o.Update();
15         }    
16     }
17 }

 

 

具体主题:

一个字段:主题的当前状态。

 1 public class ConcreteSubject extends Subject {
 2 
 3     private String subjectState;
 4     
 5     public void setSubjectState(String subjectState){
 6         
 7         this.subjectState=subjectState;
 8     }
 9     public String getSubjectState(){
10         return subjectState;
11     }
12 }

 

 

具体观察者:

Update方法:对主题的变化做出反馈的具体实现。

 1 public class ConcreteObserver extends Observer{
 2 
 3     private String observerState;
 4     private String name;
 5     private ConcreteSubject subject;
 6     
 7     public ConcreteObserver(ConcreteSubject subject,String name){
 8         this.subject=subject;
 9         this.name=name;
10     }
11     
12     public ConcreteSubject getSubject() {
13         return subject;
14     }
15 
16     public void setSubject(ConcreteSubject subject) {
17         this.subject = subject;
18     }
19     
20     @Override
21     public void Update() {
22         // TODO Auto-generated method stub
23         observerState=subject.getSubjectState();
24         System.out.println("The observer's state of "+name+" is "+ observerState);
25 
26     }
27 
28 }

 

测试类:

 1 public class Test {
 2     public static void main(String[] args) {
 3         ConcreteSubject subject=new ConcreteSubject();
 4         subject.addObserver(new ConcreteObserver(subject, "Observer A"));
 5         subject.addObserver(new ConcreteObserver(subject, "Observer B"));
 6         subject.addObserver(new ConcreteObserver(subject, "Observer C"));
 7         
 8 
 9         subject.setSubjectState("Ready");
10         subject.Notify();
11         
12         
13         subject.setSubjectState("Start");
14         subject.Notify();
15     }
16 
17 }

 

 输出:

 

The observer's state of Observer A is Ready
The observer's state of Observer B is Ready
The observer's state of Observer C is Ready
The observer's state of Observer A is Start
The observer's state of Observer B is Start
The observer's state of Observer C is Start

 

 

装饰者模式的简单理解:

动态的给一个对象增加一些装饰,使其拥有更多功能或属性。

 

实现:

抽象类Girl:被装饰者的父类,拥有Girl的属性和一个描述Girl的方法。

 1 public abstract class Girl {
 2 
 3     public String description="no particular";
 4     
 5     public String getDescription(){
 6         
 7         
 8         return description;
 9     }
10     
11     
12 }

 

 

被装饰者:中国Girl,美国Girl.为Girl添加属性,定义特定类型Girl。

 1 public class ChineseGirl extends Girl {
 2 
 3     public ChineseGirl(){
 4         super.description="Chinese Girl";
 5     }
 6 }
 7 
 8 public class AmericanGirl extends Girl{
 9 
10     public AmericanGirl(){
11         
12         super.description="American Girl";
13         
14     }
15 }

 

 

装饰者(抽象):继承Girl类的描述方法,并把该方法承接给自己的子类(装饰者)。

装饰者与被装饰者拥有共同的超类(Girl)

1 public abstract class GirlDecorator extends Girl {
2 
3     public abstract String getDescription();
4 
5 }

 

 

装饰者:为特定类型Girl添加描述。

 1 public class Art extends GirlDecorator{
 2     private Girl girl;
 3     public Art(Girl girl){
 4         this.girl=girl;
 5     }        
 6     @Override
 7     public String getDescription() {
 8         // TODO Auto-generated method stub
 9         return this.girl.getDescription()+"+like Art";
10     }
11     public void draw(){
12         System.out.println("draw pictures!");
13     }    
14 }
15 
16 
17 public class Music extends GirlDecorator{
18     private Girl girl;    
19     public Music(Girl girl){
20         this.girl=girl;
21     }    
22     @Override
23     public String getDescription() {
24         // TODO Auto-generated method stub
25         return this.girl.getDescription()+"+like Music";
26     }
27     public void Singing(){
28         System.out.println("Singing Music");
29     }
30 }

 

 

测试类:

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         
 5         Girl g1=new AmericanGirl();
 6         
 7         System.out.println(g1.getDescription());
 8         
 9         Art g2=new Art(g1);        
10         System.out.println(g2.getDescription());
11         
12         Music g3=new Music(g2);
13         System.out.println(g3.getDescription());
14         
15     }
16 }

 

 

输出:

American Girl
American Girl+like Art
American Girl+like Art+like Music

 

posted @ 2016-05-30 16:31  谈笑风生~  阅读(186)  评论(0编辑  收藏  举报