Tom and Jerry in observer Mode

Source: Internet
Author: User

Namespace tomandjerry
{
Class Program
{
Static void main (string [] ARGs)
{
# Region is completed through the interface
//// Cat
// Tom T = new Tom ();
/// Mouse
// Jerry J = new Jerry (t );
// Jerry J1 = new Jerry (t );
/// Owner
// Owner o = new owner (t );

// T. Cry ();
// Console. Read ();
# Endregion

# Region is completed by event

// Cat cat = new CAT ("Tom ");
/// Establish a link
// Mouse Jerry = new mouse ("Jerry", CAT );

// Owner o = new owner ("Jason", CAT );

/// Tom trigger event
// Cat. catcry ();

// Console. Read ();

# Endregion

# Region is completed through an abstract class

Cat cat = new CAT ();
Mouse Jerr = new mouse (CAT );
Owner o = new owner (CAT );

Cat. Cry ();
Console. Read ();

# Endregion

}
}

# Region completed through the interface

/// Define the initiator Interface
// Public interface iInitiative
//{
//// Notification method, which must be related to the recipient
// Void notice (ireceivenotice irnotice );
//}

/// Define the receiver Interface
// Public interface ireceivenotice
//{
//// There is a way to express
// Void show ();
//}

/// Define the Tom class. Tom is the active operator and inherits the active operator interface. It sends and triggers corresponding events.
// Public class Tom: iInitiative
//{
//// The initiator is not sure, so it is placed in an array.
// Arraylist alist = new arraylist ();

// # Region iInitiative Member

// Public void notice (imo-enotice irnotice)
//{
/// Create a notification relationship
// Alist. Add (irnotice );
//}

//// The trigger event
// Public void cry ()
//{
// Console. writeline ("Tom is crying ");
//// The notification sent by the active user. All the responses made by the receiver are placed in alist, and the response of the receiver is called.
// Foreach (imo-enotice notice in alist)
//{

// Notice. Show ();

//}
//}

// # Endregion
//}

/// Defines the Jerry class. Jerry is the receiver and inherits the receiver's interface.
// Public class Jerry: ireceivenotice
//{
// Public Jerry ()
//{}

/// Specify who will notify you. Generally, the interface parent class will notify you.
// Public Jerry (iInitiative iiative)
//{
//// This indicates the instance of this interface
// Iiative. Notice (this );
//}

// # Region imo-enotice Member

// Public void show ()
//{
//// After receiving the notification, an activity is triggered.
// Console. writeline ("Jerry is running crazy! ");
//}

// # Endregion
//}

/// Define the owner class. The owner is the receiver and inherits the receiver's interface.
// Public class owner: ireceivenotice
//{
// Public owner ()
//{}

/// Specify who will notify you. Generally, the interface parent class will notify you.
// Public owner (iInitiative iiative)
//{
// Iiative. Notice (this );
//}

// # Region imo-enotice Member

// Public void show ()
//{
///// After receiving the notification, an activity is triggered.
// Console. writeline ("the owner is wakeup, cry: 'Tom, what are you doing? '");
//}

// # Endregion
//}

# Endregion

# Region completed by event

# Region cat
// Public class cat
//{
//// Cat name
// Private string _ name;

//// The trigger event
// Public event eventhandler <catcryeventargs> catcryevent;

//// Constructor
// Public CAT ()
//{}
// Public CAT (string name)
//{
// This. _ name = Name;
//}

//// Trigger the call event
// Public void catcry ()
//{
// Catcryeventargs ARGs = new catcryeventargs (_ name );
// Console. writeline (ARGs );
// Catcryevent (this, argS );
//}
//}
# Endregion

# Region cat call events

// Public class catcryeventargs: eventargs
//{
//// The name of the cat that makes the call
// Private string _ name;
//// Constructor
// Public catcryeventargs ()
//{}
// Public catcryeventargs (string name)
//{
// This. _ name = Name;
//}

//// Output parameter content
// Public override string tostring ()
//{
// Return _ name + "crying! ";
//}
//}

# Endregion

# Region Master

// Public class owner
//{
//// Host Name
// Private string name;

//// Constructor
// Public owner ()
//{}
// Public owner (string _ name, CAT)
//{
// This. Name = _ name;
// Cat. catcryevent + = new eventhandler <catcryeventargs> (catcryeventhandle );
//}

//// How to handle a cat call event
// Void catcryeventhandle (Object sender, catcryeventargs E)
//{
// Wakeup ();
//}

//// Method in which the master is awakened
// Private void wakeup ()
//{
// Console. writeline (name + "is wakeup and cry 'Tom what are you doing? '");
//}
//}

# Endregion

# Region mouse

// Public class mouse
//{
//// Mouse name
// Private string _ name;

//// Constructor
// Public mouse ()
//{}

// Public mouse (string name, CAT)
//{
// This. _ name = Name;
// Cat. catcryevent + = new eventhandler <catcryeventargs> (catcryhandlerun );
//}

//// Method for handling a cat call event
// Void catcryhandlerun (Object sender, catcryeventargs E)
//{
// Run ();
//}

//// How to run the mouse
// Private void run ()
//{
// Console. writeline (_ name + "is running! ");
//}
//}

# Endregion

# Endregion

# Region is completed through an abstract class, similar to interface completion

// Define active
Public abstract class observer
{
// Notification method
Public abstract void notice (reobj robj );
}

// Define the receipt
Public abstract class reobj
{
Public abstract void show ();
}

// Define the active person
Public class Cat: Observer
{
Arraylist Al = new arraylist ();

Public override void notice (reobj robj)
{
Al. Add (robj );
}

Public void cry ()
{
Console. writeline ("Tom shouted! ");
Foreach (reobj o in Al)
{
O. Show ();
}
}
}

// Define the receiver
Public class mouse: reobj
{
Public mouse ()
{}
Public mouse (Observer obs)
{
Obs. Notice (this );
}

Public override void show ()
{
Console. writeline ("Jerry is running! ");
}
}

// Define the owner
Public class owner: reobj
{
Public owner ()
{}

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

Public override void show ()
{
Console. writeline ("the host woke up and shouted: Tom what are you doing? ");
}
}

# 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.