Observer mode-traffic lights

Source: Internet
Author: User

Namespace trafficlights
{
Class Program
{
# Region defines global variables
Static string lightcol = "";
Static trafficlight TR = NULL;
# Endregion

Static void main (string [] ARGs)
{
# Region completed by event
// While (true)
//{
// Trafficlight tra = new trafficlight ("green light ");

// Person P = new person ("Jason", tra );

// Tra. lightbright ();

// System. Threading. thread. Sleep (5*1000 );
// Console. writeline ();
/// Trafficlight T = new trafficlight ("red light ");
/// Person p1 = new person ("Mike", t );
/// T. lightbright ();
// Trafficlight1 T = new trafficlight1 ("red light ");
// Person1 p1 = new person1 ("Jason", t );
// T. lightbright ();
// System. Threading. thread. Sleep (5*1000 );
// Console. writeline ();

//}
# Endregion

# Region is completed through an abstract class

// String lightcol = "";
// While (true)
//{
// System. Threading. thread. Sleep (5*1000 );

// If (lightcol = "")
//{
// Lightcol = "red ";
//}
// Else if (lightcol = "red ")
//{
// Lightcol = "green ";
//}
// Else if (lightcol = "green ")
//{
// Lightcol = "red ";
//}

// Trafficlight TR = new trafficlight ();
// Car = new car (TR );
// Person P = new person (TR );

// Tr. lightbright (lightcol );
// Console. writeline ();

 

//}

# Endregion

# Region is completed by timer

Tr = new trafficlight ();
Car car = new car (TR );
Person P = new person (TR );

Timer time = new timer (5*1000 );
Time. elapsed + = new elapsedeventhandler (time_elapsed );
Time. Start ();

Console. Read ();

 

# Endregion
}

# Region is completed by timer
Static void time_elapsed (Object sender, elapsedeventargs E)
{
If (lightcol = "")
{
Lightcol = "red ";
}
Else if (lightcol = "red ")
{
Lightcol = "green ";
}
Else if (lightcol = "green ")
{
Lightcol = "red ";
}
Tr. lightbright (lightcol );
Console. writeline ();
}
# Endregion
}

# Region completed by event
/// The person crossing the road
// Public class person
//{
// Private string name;

// Public person ()
//{}
// Public person (string name, trafficlight tra)
//{
// This. Name = Name;
// Tra. lightevent + = new eventhandler <lighteventargs> (tra_lightevent );

//}

// Void Redlight (Object sender, lighteventargs E)
//{
// Stop ();
//}

// Public void stop ()
//{
// Console. writeline (name + "standing in situ ");
//}

// Void tra_lightevent (Object sender, lighteventargs E)
//{
// Run ();
//}

// Private void run ()
//{
// Console. writeline (name + "crossing the road ");
//}
//}

// Public class person1
//{
// Private string name;

// Public person1 ()
//{}
// Public person1 (string name, trafficlight1 tra)
//{
// This. Name = Name;
// Tra. lightevent + = new eventhandler <lighteventargs1> (Redlight );

//}

// Void Redlight (Object sender, lighteventargs1 E)
//{
// Stop ();
//}

// Public void stop ()
//{
// Console. writeline (name + "standing in situ ");
//}

//}

/// Lamp
// Public class trafficlight
//{
//// Lamp name
// Private string lightname;

//// Lamp event
// Public event eventhandler <lighteventargs> lightevent;

//// Constructor
// Public trafficlight ()
//{}

// Public trafficlight (string name)
//{
// This. lightname = Name;
//}

//// Events triggered by lighting
// Public void lightbright ()
//{
// Lighteventargs ARGs = new lighteventargs (lightname );
// Console. writeline (lightname );
// Lightevent (this, argS );
//}

//}

//// Lighting event
// Public class lighteventargs: eventargs
//{
//// Lamp name
// Private string lightname;

//// Constructor
// Public lighteventargs ()
//{}

// Public lighteventargs (string name)
//{
// This. lightname = Name;
//}

//// Output parameter content
// Public override string tostring ()
//{
// Return lightname + "highlighted! ";
//}


//}

// Public class trafficlight1
//{
//// Lamp name
// Public String lightname;

//// Lamp event
// Public event eventhandler <lighteventargs1> lightevent;

//// Constructor
// Public trafficlight1 ()
//{}

// Public trafficlight1 (string name)
//{
// This. lightname = Name;
//}

//// Events triggered by lighting
// Public void lightbright ()
//{
// Lighteventargs1 ARGs = new lighteventargs1 (lightname );
// Console. writeline (lightname );
// Lightevent (this, argS );
//}

//}

//// Lighting event
// Public class lighteventargs1: eventargs
//{
//// Lamp name
// Private string lightname;

//// Constructor
// Public lighteventargs1 ()
//{}

// Public lighteventargs1 (string name)
//{
// This. lightname = Name;
//}

//// Output parameter content
// Public override string tostring ()
//{
// Return lightname + "highlighted! ";
//}

//}
# Endregion

# Region is completed through an abstract class

# Region definition active
// Define active
Public abstract class observer
{
Public abstract void notice (receive RECE );
}
# Endregion

# Region definition receiving
// Define the receipt
Public abstract class receive
{
Public abstract void show (string lightcolor );
}
# Endregion

# Region defines the active person
// Define the active person
Public class trafficlight: Observer
{
Arraylist Al = new arraylist ();

Public override void notice (receive RECE)
{
Al. Add (RECE );
}
/// <Summary>
/// Trigger Method
/// </Summary>
Public void lightbright (string lightcolor)
{
Console. writeline (string. Format ("It's {0} lit now", lightcolor ));
Foreach (receive R in Al)
{

R. Show (lightcolor );
}
}
}
# Endregion

# Region defines the vehicle Receiver

// Define the receiver
Public class car: receive
{
Public Car ()
{}

Public Car (Observer obs)
{
Obs. Notice (this );
}

Public override void show (string lightcolor)
{
If (lightcolor = "red ")
{
Console. writeline ("brake, stop! ");
}
Else if (lightcolor = "green ")
{
Console. writeline ("engine launch, pass channel! ");
}
}
}

# Endregion

# Region defines the receiver of a person

Public class person: receive
{
Public Person ()
{}

Public Person (Observer obs)
{
Obs. Notice (this );
}

Public override void show (string lightcolor)
{
If (lightcolor = "red ")
{
Console. writeline ("Stop Motion ");
}
Else if (lightcolor = "green ")
{
Console. writeline ("crossing the road ");
}
}
}

# Endregion

# Endregion
}

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.