Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Full Tutorial on indiedevart.wordpress.com
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication5
- {
- class Program
- {
- static void Main(string[] args)
- {
- Enemy subject = new Enemy();
- IObserver observer1 = new UserInteface();
- subject.Subscribe(observer1);
- subject.Subscribe(new DifferentUnit());
- subject.Health++;
- Console.WriteLine("--------------------");
- subject.Unsubscribe(observer1);
- subject.Health--;
- Console.ReadLine();
- }
- }
- public class Enemy : IEnemy
- {
- private List<IObserver> observers = new List<IObserver>();
- private int _health=10;
- public int Health
- {
- get { return _health; }
- set
- {
- _health = value;
- Notify();
- }
- }
- public void Notify()
- {
- observers.ForEach(x => x.Update());
- }
- public void Subscribe(IObserver observer)
- {
- observers.Add(observer);
- }
- public void Unsubscribe(IObserver observer)
- {
- observers.Remove(observer);
- }
- }
- interface IEnemy
- {
- void Subscribe(IObserver observer);
- void Unsubscribe(IObserver observer);
- void Notify();
- }
- public class UserInteface : IObserver
- {
- public void Update()
- {
- Console.WriteLine("Interface Updated");
- }
- }
- public class DifferentUnit : IObserver
- {
- public void Update()
- {
- Console.WriteLine("Relation Updated");
- }
- }
- public interface IObserver
- {
- void Update();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement