Observer pattern (Observer behavioral) C # Simple Example

Source: Internet
Author: User

Observer pattern (Observer behavioral) C # Simple Example

A few key points: the pattern makes the dependency between the target and the observation both loosely coupled and the notification automatically propagated

Example: A series of changes occurred after the player hit the enemy: After the explosion, the enemy less than 1 ....

namespace adapterpattern{public partial class Observerform:form {public Observerform () {        InitializeComponent ();            } private void Btndisplay_click (object sender, EventArgs e) {basedata.enemynumber = 100;            IObserver explosionevent = new Explosionevent ();            IObserver enemy = new Enemy ();            Player P1 = new Player1 (); P1.            Addobserver (explosionevent); P1.            Addobserver (enemy); P1.            Notify (); ListBox1.Items.Add (P1.        Display ());        }} public static class basedata//data broker {public static string displaystring {get; set;}    public static int Enemynumber {get; set;}        Public abstract class Player {list<iobserver> observers = new list<iobserver> ();//Observer list public virtual void Notify () {foreach (IObserver Observer in observers) {OB Server.        Update ();//A Notice to the Observer    }} public virtual void Addobserver (IObserver observer)//Add observer {observers.        ADD (Observer); } public virtual void Delet (IObserver observer)//delete observer {observers.        REMOVE (Observer);    } public abstract string Display (); } public class Player1:player {public override string Display () {return basedata.displa        Ystring + BaseData.EnemyNumber.ToString ();    }} public interface iobserver//Observer interface {void Update (); } public class Explosionevent:iobserver//Observer 1 {public void Update () {basedata.displays        Tring = "show explosion"; }} public class Enemy:iobserver//Observer 2 {public void Update () {Basedata.enemynumber       -= 1;         "The number of enemies decreased by 1"; }    }}


Observer pattern (Observer behavioral) C # Simple Example

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.