设计模式_观察者模式

问题:

       请模拟下列情形
         小孩在睡觉
        醒来后要求吃东西

 


package com.bjsxt.dp.observer.test1;

 class Child implements Runnable{

   private boolean wakeUp=false; //小孩处于睡着状态
   
	
	void wakeUp()
	{
		wakeUp=true;
		
	}
	public boolean isWakeUp() {
		return wakeUp;
	  }
	  public void setWakeUp(boolean wakeUp) {
		this.wakeUp = wakeUp;
	 }
	@Override
	public void run() {
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		wakeUp();
	}
	
}
 class Dad implements Runnable{
	 private Child c;
	 public Dad(Child c)
	 {
		 this.c=c;
	 }
	 public void feed(Child c)
	 {
		 System.out.println("feed chid");
	 }

	@Override
	public void run() {
		while (!c.isWakeUp())
		{
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		feed(c);
	}
	 
 }
 public class Test1 {
	    public static void main(String[] args)
		{
	
			Child c =new Child();
			new Thread(c).start(); 
		    new Thread(new Dad(c)).start();
			
		}

}
 

package com.bjsxt.dp.observer.test2;


 class Child implements Runnable{
  private Dad d; 
   private boolean wakeUp=false; //小孩处于睡着状态
   
	public Child(Dad d){
	 this.d=d;
  }
	void wakeUp()
	{
		wakeUp=true;
		d.feed(this);
	}
	@Override
	public void run() {
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.wakeUp();
		
	}
	public boolean isWakeUp() {
		return wakeUp;
	  }
	  public void setWakeUp(boolean wakeUp) {
		this.wakeUp = wakeUp;
	 }
	
}
 class Dad{
	 public void feed(Child c)
	 {
		 System.out.println("feed chid");
	 }
	 
 }
 public class Test2 {
	    public static void main(String[] args)
		{
			Dad d =new Dad();
			Child c =new Child(d);
			new Thread(c).start();

		}

	}
//使用了观察者模式之后

package com.bjsxt.dp.observer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


class WakenUpEvent { //==Buttion 发出ActionEvent这件事
	private long time;
	private String loc;
	private Child source;
	
	public WakenUpEvent(long time, String loc, Child source) {
		super();
		this.time = time;
		this.loc = loc;
		this.source = source;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	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 l) {
		wakenUpListeners.add(l);
	}
	
	void wakeUp() {
		for(int i=0; i<wakenUpListeners.size(); i++) {
			WakenUpListener l = wakenUpListeners.get(i);
			l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
		}
	}

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

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");
	}
	
}

class Dog implements WakenUpListener {

	public void ActionToWakenUp(WakenUpEvent arg0) {
		System.out.println("wang wang ...");
	}
	
}

interface WakenUpListener {
	public void ActionToWakenUp(WakenUpEvent wakenUpEvent);
}

public class Test {
	public static void main(String[] args) {
		Child c = new Child();
		
		
		String[] observers = PropertyMgr.getProperty("observers").split(",");

		for(String s : observers) {
			try {
				c.addWakenUpListener((WakenUpListener)(Class.forName(s).newInstance()));
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		
		new Thread(c).start();
	}
}

class PropertyMgr {
	private static Properties props = new Properties();
	
	static {
		try {
			props.load(Test.class.getClassLoader().getResourceAsStream                  ("com/bjsxt/dp/observer/observer.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getProperty(String key) {
		return props.getProperty(key);
	}
}

AWT的事件处理

package com.bjsxt.dp.observer.awt;

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestFrame extends Frame {
	public void launch() {
		Button b = new Button("press me");
		b.addActionListener(new MyActionListener());
		b.addActionListener(new MyActionListener2());
		this.add(b);
		this.pack();
		
		this.addWindowListener(new WindowAdapter(){

			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
			
		});
		
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new TestFrame().launch();
	}
	
	private class MyActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			System.out.println("button pressed!");
		}
		
	}
	
	private class MyActionListener2 implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			System.out.println("button pressed 2!");
		}
		
	}
}

模拟AWT的事件处理

ActionEvent
Button
ActionListener

package com.bjsxt.dp.observer.awt;

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


public class Test {
	public static void main(String[] args) {
		Button b = new Button();
		b.addActionListener(new MyActionListener());
		b.addActionListener(new MyActionListener2());
		b.buttonPressed();
	}
}

class Button {
	
	private List<ActionListener> actionListeners = new ArrayList<ActionListener>();
	
	public void buttonPressed() {
		ActionEvent e = new ActionEvent(System.currentTimeMillis(),this);
		for(int i=0; i<actionListeners.size(); i++) {
			ActionListener l = actionListeners.get(i);
			l.actionPerformed(e);
		}
	}
	
	public void addActionListener(ActionListener l) {
		actionListeners.add(l);
	}
}

interface ActionListener {
	public void actionPerformed(ActionEvent e);
}

class MyActionListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
		System.out.println("button pressed!");
	}
	
}

class MyActionListener2 implements ActionListener {

	public void actionPerformed(ActionEvent e) {
		System.out.println("button pressed 2!");
	}
	
}

class ActionEvent {
	
	long when;
	Object source;
	
	public ActionEvent(long when, Object source) {
		super();
		this.when = when;
		this.source = source;
	}
	
	
	public long getWhen() {
		return when;
	}

	public Object getSource() {
		return source;
	}
	
}
用了设计模式

        复杂度增加
       开发成本增加
       维护成本降低






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值