加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[设计模式as3版]二.观察者模式

发布时间:2020-12-15 18:04:41 所属栏目:百科 来源:网络整理
导读:发布者 package {public interface Subject {function addObserver(observer:Observer):void;function removeObserver(observer:Observer):void;function notifyObservers():void;}} 观察者 package {public interface Observer {function update(data:Objec
发布者
package 
{
	public interface Subject {
		
		function addObserver(observer:Observer):void;
		function removeObserver(observer:Observer):void;
		function notifyObservers():void;
		
	}
}


观察者

package 
{
	public interface Observer {
		
		function update(data:Object=null):void;
	}
}

发布者具体实现类

package 
{	
	public class TimeData implements Subject {
		
		private var _now:Date;
		
		private var observers:Vector.<Observer>;
		
		public function TimeData() {
			observers = new Vector.<Observer>();	
		}
		
		public function addObserver(observer:Observer):void {
			observers.push(observer);	
		}
		
		public function removeObserver(observer:Observer):void {
			var index:int = observers.indexOf(observer);
			if(index >= 0) {
				observers.splice(index,1);	
			}
		} 
		
		public function notifyObservers():void {
			for each (var observer:Observer in observers) {
				observer.update(_now);
			}
		}

		public function get now():Date {
			return _now;
		}

		public function set now(value:Date):void {
			_now = value;
			notifyObservers();
		}
	}
}

观察者具体实现类

package
{
	import flash.display.Shape;
	
	public class GraphicDisplay extends Shape implements Observer {
		
		private var _subject:Subject;
		
		public function GraphicDisplay() {
			
		}
		
		public function set subject(value:Subject):void {
			if(_subject) {
				_subject.removeObserver(this);	
			}
			_subject = value;
			if(_subject) {
				_subject.addObserver(this);	
			}
		}
		
		public function update(data:Object=null):void {
			var now:Date = data as Date;
			if(now) {
				var width:Number = now.seconds * 5;
				graphics.clear();
				graphics.beginFill(0x000088);
				graphics.drawRect(0,width,5);
				graphics.endFill();
			}
		}
	}
}



观察者具体实现类

package
{
	import flash.display.Sprite;
	import flash.text.TextField;
	
	public class TextDisplay extends Sprite implements Observer {
		
		private var _subject:Subject;
		private var text:TextField;
		
		public function TextDisplay() {
			text = new TextField();
			text.text = "当前时间:00:00:00";
			addChild(text);
		}
		
		public function set subject(value:Subject):void {
			if(_subject) {
				_subject.removeObserver(this);	
			}
			_subject = value;
			if(_subject) {
				_subject.addObserver(this);	
			}
		}
		
		public function update(data:Object=null):void {
			var now:Date = data as Date;
			if(now) {
				var str:String = "当前时间:";
				str += (now.hours >= 10   ? now.hours   : "0" + now.hours);
				str += ":";
				str += (now.minutes >= 10 ? now.minutes : "0" + now.minutes);
				str += ":";
				str += (now.seconds >= 10 ? now.seconds : "0" + now.seconds);
				text.text = str;
			}
		}
	}
}



测试类

package 
{
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	[SWF(width="500",height="400")]
	public class ObserverTest extends Sprite {
		
		private var timeData:TimeData;
		private var graphicDisplay:GraphicDisplay;
		private var textDisplay:TextDisplay;
		
		public function ObserverTest() {
			timeData = new TimeData();
			textDisplay = new TextDisplay();
			textDisplay.subject = timeData;
			addChild(textDisplay);
			textDisplay.y = 10;
			graphicDisplay = new GraphicDisplay();
			graphicDisplay.subject = timeData;
			addChild(graphicDisplay);
			graphicDisplay.y = 40;
			
			var timer:Timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
			timer.start();
		}
		
		private function onTimer(event:TimerEvent):void {
			timeData.now = new Date();	
		}
		
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读