using System; namespace DPObserver { /// ------------------------------------------------------------------ /// <summary> /// Represents the observer object. /// </summary> /// ------------------------------------------------------------------ public interface IClient { /// --------------------------------------------------------------- /// <summary> /// The article has been updated. /// </summary> /// --------------------------------------------------------------- void Update(DefaultArticle defaultArticle); } }
namespace DPObserver { /// ------------------------------------------------------------------ /// <summary> /// Represents the concrete observer. /// </summary> /// ------------------------------------------------------------------ public class Client : IClient { private string _name = null; /// --------------------------------------------------------------- /// <summary> /// Create a new client. /// </summary> /// --------------------------------------------------------------- public Client(string name) { this._name = name; } /// --------------------------------------------------------------- /// <summary> /// Get the client's name. /// </summary> /// --------------------------------------------------------------- public string Name { get { return this._name; } } /// --------------------------------------------------------------- /// <summary> /// /// </summary> /// --------------------------------------------------------------- public void Update(DefaultArticle defaultArticle) { Console.WriteLine("Client {0} notified! Article {1}: new price = {2}", this._name, defaultArticle.Name, defaultArticle.Price); } } }
namespace DPObserver { /// ------------------------------------------------------------------ /// <summary> /// Represents the subject object. /// </summary> /// ------------------------------------------------------------------ public abstract class DefaultArticle { private List<IClient> _observers = new List<IClient>(); protected float _price = 0f; protected string _name = String.Empty; /// --------------------------------------------------------------- /// <summary> /// Get or set the price. /// </summary> /// --------------------------------------------------------------- public float Price { get { return this._price; } set { this._price = value; this.NotifyObservers(); } } /// --------------------------------------------------------------- /// <summary> /// Get the name. /// </summary> /// --------------------------------------------------------------- public string Name { get { return this._name; } } /// --------------------------------------------------------------- /// <summary> /// Attach an observer. /// </summary> /// <param name="client">The observer to attach.</param> /// --------------------------------------------------------------- public void Attach(IClient client) { if (!this._observers.Contains(client)) this._observers.Add(client); } /// --------------------------------------------------------------- /// <summary> /// Detach an observer. /// </summary> /// <param name="client">The observer to dettach.</param> /// --------------------------------------------------------------- public void Detach(IClient client) { if (this._observers.Contains(client)) this._observers.Remove(client); } /// --------------------------------------------------------------- /// <summary> /// Notify all observers. /// </summary> /// --------------------------------------------------------------- protected void NotifyObservers() { foreach (IClient obs in this._observers) obs.Update(this); } } }
namespace DPObserver { /// ------------------------------------------------------------------ /// <summary> /// Represents the concrete subject. /// </summary> /// ------------------------------------------------------------------ public class Article : DefaultArticle { /// --------------------------------------------------------------- /// <summary> /// Create a new article. /// </summary> /// <param name="name"></param> /// <param name="price"></param> /// --------------------------------------------------------------- public Article(string name) { this._name = name; } } }
namespace DPObserver { public class Program { public static void Main(string[] args) { var article1 = new Article("CodeS-SourceS") { Price = 1234.50f }; var article2 = new Article("Livre C#") { Price = 432.10f }; var article3 = new Article("Abonnement MSDN") { Price = 12345.60f }; var article4 = new Article("Camion poubelle") { Price = 123.4f }; var article5 = new Article("Sifflet") { Price = 12f }; var client1 = new Client("Bidou"); var client2 = new Client("Nix"); article1.Attach(client1); article1.Attach(client2); article1.Price = 88.5f; article1.Detach(client1); article1.Price = 73.3f; article2.Attach(client1); article2.Attach(client2); article2.Price = 400f; article3.Attach(client1); article3.Price = 14000f; article5.Attach(client2); article5.Price = 12f; } } }
Publié par cs_Bidou. Dernière mise à jour le par BunoCS.
Ce document intitulé « Design Pattern Observer » issu de CodeS-SourceS (https://codes-sources.commentcamarche.net/) est mis à disposition sous les termes de la licence Creative Commons. Vous pouvez copier, modifier des copies de cette page, dans les conditions fixées par la licence, tant que cette note apparaît clairement.
Actualités, Avis de décès, Carte de voeux, Bricolage, Coloriages, Cinéma, Coiffure, Cuisine, Déco, Dictionnaire, Horoscope, Jeux en ligne, Programme TV, Recettes, Restaurant, SMIC, Test débit, Voyage, Signification prénom