Last active
December 20, 2015 16:48
-
-
Save yemrekeskin/6164001 to your computer and use it in GitHub Desktop.
Observer Design Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum ProductStatus | |
{ | |
Approvaled, | |
ExistInStock, | |
NotExistInStock, | |
SoldOut, | |
Delivered | |
} | |
public class Product | |
{ | |
public int ID { get; set; } | |
public string CreatedBy { get; set; } | |
public DateTime CreatedDate { get; set; } | |
public string UpdatedBy { get; set; } | |
public DateTime UpdatedDate { get; set; } | |
public long UnitPrice { get; set; } | |
public int StockCount { get; set; } | |
public ProductStatus Status{ get; set; } | |
} | |
public interface IProductObserver | |
{ | |
void Notify(Product product); | |
} | |
public class StakeHolderObserver | |
: IProductObserver | |
{ | |
public void Notify(Product product) | |
{ | |
if(product.StockCount < 10) | |
Console.WriteLine(); //........ for example | |
Console.WriteLine("Prodcut StockCount : {0} ", product.StockCount); | |
} | |
} | |
public class ManagerObserver | |
: IProductObserver | |
{ | |
public void Notify(Product product) | |
{ | |
Console.WriteLine("Prodcut StockCount : {0} ", product.StockCount); | |
} | |
} | |
public class CustomerObserver | |
: IProductObserver | |
{ | |
public void Notify(Product product) | |
{ | |
Console.WriteLine("Prodcut StockCount : {0} ", product.StockCount); | |
} | |
} | |
public abstract class ProductBaseHandler | |
{ | |
protected readonly List<IProductObserver> _list = null; | |
public ProductBaseHandler() | |
{ | |
this._list = new List<IProductObserver>(); | |
} | |
public abstract void Notify(); | |
public void Subscribe(IProductObserver observer) | |
{ | |
if (!_list.Contains(observer)) | |
_list.Add(observer); | |
} | |
public void UnSubscribe(IProductObserver observer) | |
{ | |
if (_list.Contains(observer)) | |
_list.Remove(observer); | |
} | |
} | |
public class ProductChangeHandler | |
: ProductBaseHandler | |
{ | |
private Product product; | |
public Product Product | |
{ | |
get { return product; } | |
set | |
{ | |
product = value; | |
Notify(); | |
} | |
} | |
public override void Notify() | |
{ | |
foreach (var item in _list) | |
item.Notify(product); | |
} | |
} | |
//using | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var sbj = new ProductChangeHandler(); | |
sbj.Subscribe(new ManagerObserver()); | |
sbj.Subscribe(new StakeHolderObserver()); | |
sbj.Subscribe(new CustomerObserver()); | |
Product Original = new Product() | |
{ | |
CreatedBy = "EMRE", | |
CreatedDate = DateTime.Now, | |
Status = ProductStatus.Delivered, | |
ID = 1, | |
StockCount = 15, | |
UnitPrice = 23, | |
UpdatedBy = "EMRE", | |
UpdatedDate = DateTime.Now | |
}; | |
sbj.Product = Original; | |
// process for product object | |
Original.Status = ProductStatus.NotExistInStock; | |
Original.StockCount = 5; | |
sbj.Product = Original; | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment