设计模式-观察者模式

2018-02-28  本文已影响0人  MarkShen

1. OOA and OOD

1.1 模拟程序

   package com.alan.planA;
   class Child implements Runnable {
       private boolean flag;
   
       public boolean isWakenUp() {
           return flag;
       }

       public void run() {
           try {
               Thread.sleep(5000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           flag = true;
       }
   }

   class Dad implements Runnable {
       private Child c;
   
       public Dad(Child c) {
           this.c = c;
       }

       public void run() {
           while (!c.isWakenUp()) {
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
           feed(c);
       }

       private void feed(Child c2) {
           System.out.println("feed child.");
       }   
   }

   public class FeedChildSimulator {
       public static void main(String[] args) {
           Child c = new Child();
           Dad d = new Dad(c);
           Thread thread1 = new Thread(d);
           Thread thread2 = new Thread(c);
           thread1.start();
           thread2.start();
       }
   }
package com.alan.planB;
class Child implements Runnable {
   private boolean flag;
   private Dad d;
   
   public Child(Dad d) {
       this.d = d;
   }
   
   public void isWakenUp() {
       flag = true;
       d.feed(this);
   }

   public void run() {
       try {
           Thread.sleep(5000);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       isWakenUp();
   }
}

class Dad {
   public void feed(Child c2) {
       System.out.println("feed child.");
   }
}

public class FeedChildSimulator {
   public static void main(String[] args) {
       Dad d = new Dad();
       Child c = new Child(d);
       Thread thread = new Thread(c);
       thread.start();
   }
}
package com.alan.planC;

class WakenUpEvent {
    private long time;
    private String location;
    private Child source;

    public WakenUpEvent(long time, String location, Child source) {
        super();
        this.time = time;
        this.location = location;
        this.source = source;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Child getSource() {
        return source;
    }

    public void setSource(Child source) {
        this.source = source;
    }

}

class Child implements Runnable {
    private Dad d;

    public Child(Dad d) {
        this.d = d;
    }

    public void isWakenUp() {
        d.actionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
    }

    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isWakenUp();
    }
}

class Dad {
    public void actionToWakenUp(WakenUpEvent wakenUpEvent) {
        // 根据时间,地点,事件源来具体做出处理
        System.out.println("feed child.");
    }
}

public class FeedChildSimulator {
    public static void main(String[] args) {
        Dad d = new Dad();
        Child c = new Child(d);
        Thread thread = new Thread(c);
        thread.start();
    }
}
package com.alan.planD;

import java.util.ArrayList;
import java.util.List;

class WakenUpEvent {
    private long time;
    private String location;
    private Child source;

    public WakenUpEvent(long time, String location, Child source) {
        super();
        this.time = time;
        this.location = location;
        this.source = source;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Child getSource() {
        return source;
    }

    public void setSource(Child source) {
        this.source = source;
    }

}

class Child implements Runnable {
    private List<WakenUpListener> wakenUpListeners = new ArrayList<WakenUpListener>();

    public void addWakenUpListener(WakenUpListener wul) {
        wakenUpListeners.add(wul);
    }
    
    public void isWakenUp() {
        for (WakenUpListener l : wakenUpListeners) {
            l.actionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
        }
    }

    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isWakenUp();
    }
}

class Dad implements WakenUpListener {
    public void actionToWakenUp(WakenUpEvent wakenUpEvent) {
        // 根据时间,地点,事件源来具体做出处理
        System.out.println("feed child.");
    }
}

class GrandFather implements WakenUpListener {
    public void actionToWakenUp(WakenUpEvent wakenUpEvent) {
        // 根据时间,地点,事件源来具体做出处理
        System.out.println("hug child.");
    }
}

/**
 * 实现这个接口的类,一定会监听WakenUp醒过来的这件事 
 */
interface WakenUpListener {
    // 对这样的事件进行响应,事件本身和事件源脱离 ,灵活度最高
    public void actionToWakenUp(WakenUpEvent wakenUpEvent);
}

public class FeedChildSimulator {
    public static void main(String[] args) {
        Dad d = new Dad();
        GrandFather gf = new GrandFather();
        Child c = new Child();
        c.addWakenUpListener(d);
        c.addWakenUpListener(gf);
        
        Thread thread = new Thread(c);
        thread.start();
    }
}

package design.pattern.behavior.observer;

import java.util.Observable;
public class ConcreteSubject extends Observable {
    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        this.setChanged();
        this.notifyObservers();
    }
}

package design.pattern.behavior.observer;
import java.util.Observable;
import java.util.Observer;

public class MyObserver implements Observer {
    
    private int state;
    
    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public void update(Observable o, Object arg) {
        state = ((ConcreteSubject)o).getState();
    }
}

package design.pattern.behavior.observer;

public class Client {
    public static void main(String[] args) {
        MyObserver o1 = new MyObserver();
        MyObserver o2 = new MyObserver();
        MyObserver o3 = new MyObserver();
        
        System.out.println(o1.getState());
        System.out.println(o2.getState());
        System.out.println(o3.getState());
        
        ConcreteSubject s = new ConcreteSubject();
        s.addObserver(o1);
        s.addObserver(o2);
        s.addObserver(o3);
        
        s.setState(3000);
        
        System.out.println("state changed...");
        System.out.println(o1.getState());
        System.out.println(o2.getState());
        System.out.println(o3.getState());
    }
}

1.2 弄清楚用户到底要什么, 具体什么要求。

上一篇 下一篇

猜你喜欢

热点阅读