[design mode AS3 version] two. Observer mode

Source: Internet
Author: User
Tags addchild current time
Published by
Package 
{Public
	interface Subject {
		
		function Addobserver (observer:observer): void;
		function Removeobserver (observer:observer): void;
		function notifyobservers (): void;
		
	}
}


Observers

Package 
{Public
	interface Observer {
		
		function Update (data:object=null): void;
	}
}

Publisher-Specific Implementation class

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 ():D ate {
			return _now;
		}

		Public function set Now (value:date): void {
			_now = value;
			Notifyobservers ();}}}

Viewer-Specific implementation class

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, 0, Width, 5);
				Graphics.endfill ();}}}



Viewer-Specific implementation class

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 = "Current time: 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 = "Current time:";
				str + = (now.hours >=   now.hours   : "0" + now.hours);
				str + = ":";
				str + = (now.minutes >= now.minutes: "0" + now.minutes);
				str + = ":";
				str + = (now.seconds >= now.seconds: "0" + now.seconds);
				Text.text = str;}}}



Test class

Package 
{
	import flash.display.Sprite;
	Import flash.events.TimerEvent;
	Import Flash.utils.Timer;
	
	[SWF (width= ", height=")]
	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 = ten;
			Graphicdisplay = new Graphicdisplay ();
			Graphicdisplay.subject = Timedata;
			AddChild (graphicdisplay);
			GRAPHICDISPLAY.Y = +;
			
			var timer:timer = new timer (+);
			Timer.addeventlistener (Timerevent.timer, onTimer);
			Timer.start ();
		}
		
		Private Function OnTimer (event:timerevent): void {
			timedata.now = new Date ();}}
}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.