Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

OBSERVER

using System;
using System.Collections.Generic;
 
namespace ObserverPatternDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Product Laptop = new Product("DELL", 1000);
            Console.WriteLine("--------Customer 1 and Customer 2 is subscribed to the Laptop Product----------");
            // attach or subscribe customer 1
            Customer Customer1 = new Customer("customer 1");
            Laptop.Subscribe(Customer1);
            // attach or subscribe customer 2
            Customer Customer2 = new Customer("customer 2");
            Laptop.Subscribe(Customer2);
            
            // publish notification to the subscribers
            Laptop.price = 800;
 
            Console.WriteLine("--------customer 2 is unsubscribed and customer 3 is subscribed to the Laptop Product----------");
            // customer 2 is unsubscribed and customer 3 is subscribed
            Laptop.Unsubscribe(Customer2);
            Customer Customer3 = new Customer("customer 3");
            Laptop.Subscribe(Customer3);
            Laptop.price = 900;
            Console.ReadLine();
        }
    }
    // ISubject Interface
    interface IProduct
    {
        // attach observer
        void Subscribe(Customer customer);
        // detach observer
        void Unsubscribe(Customer customer);
        // publish notification to the subscribers
        void Notify();
    }
    // ConcreteSubject class
    public class Product : IProduct
    {
        string name;
        float basePrice;
        float currentPrice;
        List<Customer> customers = new List<Customer>();
 
        public Product(string name, float basePrice)
        {
            this.name = name;
            this.basePrice = basePrice;
            this.currentPrice = basePrice;
        }
 
        public float price
        {
            get
            {
                return currentPrice;
            }
            set
            {
                // if price of the laptop will be less than the base price then 
                // notification will forwarded to the subscribers
                currentPrice = value;
                if (value <= basePrice)
                    Notify();
            }
        }
        public void Subscribe(Customer customer)
        {
            customers.Add(customer);
        }
 
        public void Unsubscribe(Customer customer)
        {
            customers.Remove(customer);
        }
 
        public void Notify()
        {
            foreach (Customer observer in customers)
            {
                observer.Update(this);
            }
        }
 
        public string Name
        {
            get { return name; }
        }
 
        public float discount
        {
            get { return (basePrice - currentPrice)*100/basePrice; }
        }
        public float CurrentPrice
        {
            get { return currentPrice; }
        }
    }
    // IObserver Interface
    interface ICustomer
    {
        void Update(Product product);
    }
    // ConcreteObserver Interface
    public class Customer : ICustomer
    {
        string name;
        public Customer(string name)
        {
            this.name = name;
        }
        public void Update(Product product)
        {
            Console.WriteLine("{0}: {1} Laptop is now available at {2}; Discount = {3}%",this.name, product.Name, product.CurrentPrice, product.discount);
        }
    }
 
    
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: get all animation clip animator unity 
Csharp :: c# string replace multiple matches with one charactar 
Csharp :: c# blazor update state 
Csharp :: global variable startup file .net core api 
Csharp :: how to get odd saturday in a month in c# 
Csharp :: alpahbet incremnet in c# 
Csharp :: VideoPlayer.isPlaying 
Csharp :: How to keep line breaks in SQL Server using ASP.NET and C#? 
Csharp :: c# list double min max 
Csharp :: cshtml page title 
Csharp :: catwherehouse 
Csharp :: c# list.except compare classes with IEqualityComparer 
Csharp :: convert excel to datatable without xml configuration 
Csharp :: check for held hey unity 
Csharp :: IOS app crashing on ios 15 unity 
Csharp :: moment script unity 
Csharp :: c# generate random key with specified length 
Csharp :: DefaultContractResolver .net exclude null values JsonSerializerSettings ContractResolver DefaultContractResolver 
Csharp :: ms transform 
Csharp :: Set property of control on form by name 
Csharp :: linq get values is not in other table 
Csharp :: cassandra Keyspaces repository .net 
Csharp :: how to get user control dropdown value in asp net 
Csharp :: c# unary operator 
Csharp :: How to retrieve a dead letter msg dotnet 
Csharp :: asp.net unregister client script 
Csharp :: most popular products code using asp.net core in visual studio code 
Csharp :: publish applications for linux 
Csharp :: client = matrice[indexselectedclient] as String[]; 
Csharp :: c# random change seed 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =