C # Examples of observer patterns

Source: Internet
Author: User

Using System;


using System.Collections.Generic;


using System.Text;


using System.Collections;





namespace ConsoleApplication1


{





public class Singleon


  {


public Singleon ()


    {


      //


//TODO: Add constructor logic here


      //


    }





  }


public Interface Xh_subject


  {





void Smile (observers obs);





  }


public Interface Observers


  {


void Say ();





  }





public class Xh:xh_subject


  {


private ArrayList all;





public XH ()


    {


all = new ArrayList ();





    }





#region Xh_subject Member





public void Smile (observers obs)


    {


all. ADD (OBS);





    }





#endregion





public void Cry ()


    {





Console.WriteLine ("Cry and all!");


foreach (observers obs in all)


      {


Obs.say ();





      }





    }


  }


public class Wo:observers


  {


private string name;





public wo (string name, Xh_subject xh_sub)


    {


this.name = name;


Xh_sub.smile (this);





    }





#region Observers member





public void Say ()


    {


Console.WriteLine ("Say something take care of her!");


    }





#endregion


  }


public class Jz:observers


  {





private string name;


public JZ (string name, Xh_subject xh_sub)


    {


this.name = name;


Xh_sub.smile (this);





    }





#region Observers member





public void Say ()


    {


Console.WriteLine ("I fu le you!");


    }





#endregion


  }


class Client_factory


  {





[STAThread]


static void Main (string[] args)


    {





Consoleapplication1.xh h = new XH ();


Xh_subject sub = h;


consoleapplication1.wo me = new Wo ("devil", sub);


CONSOLEAPPLICATION1.JZ j = new JZ ("N_man", sub);





h.cry ();





    }


  }





}

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.