Observer Pattern C#

Observer

When do we go for event driven programming in our application? Why Action driven programming.  — When a state change in object(Data is altered, then certain algorithm should be executed) should execute certain piece of logics, then we should go for event driven programming. SendSMS is the algorithm to be executed when balance is credited/debited.

Data : Account Balance (which undergo change because of publisher)

Publisher : Update Method : who will raise the signal

Signal : Event/Listener

Observer : MessageCreator.SendSms

One to many : One publisher raises the signal to many Observers.

Decorator Pattern C#

Problem : lets say we have Class A and Class B.

Class B source code is not available for change. & it has a method that is being called in class A. What if we need to add additional logic to method in B. What to be done?

we can have a derived class B1 from B & add our own logic in method after calling the base.Echo().

    class A

{

public B b = new B();

public void M1()

{

b.Echo();

}

}

class B1 : B

{

public void Echo()

{

base.Echo();

//New steps

}

}

/// <summary>

/// Source code cannot be touched to change

/// </summary>

class B

{

public void Echo()

{

//Line 1

//Line 2

}

}

 

But what if class B is final/Sealed???  we can achieve what achieve with inheritance using Has-a relationship.

Decorator

class A

{

public IB b = new B1();

public void M1()

{

b.Echo();

}

}

class B1 : IB

{

public void Echo()

{

new B().Echo();

//New steps

}

}

/// <summary>

/// Source code cannot be touched to change

/// </summary>

class B : IB

{

public void Echo()

{

//Line 1

//Line 2

}

}

public interface IB    {

void Echo();

}

Composite1

 

It is again a wrapper class, mostly like proxy implementing the same  interface of the actual service provider. But the only difference is that it has additional logic apart from calling the serviceProvider. where as proxy is an exact call to actual service provider.

A Decorator is a change management utility which comes in hand if we are working with classes that are final and you don;t have the source code or you are not the owner of the source code to change. so you can create a wrapper around the source code & add your own extra logics.

Facade Pattern C#

Facade

Problem/Scenario

Facade1

 

Solution

Facade2

Facade represents the client side logics in server side. It is not object oriented representation, instead it is a structural implementation. It has multi level representations of different logics, not single responsibility. Still it is usefull in a way, that any change in logic, we need to change the facade instead of client code. So change management is easy. SO any remote client dealing with multiple clients, its better to root it through facade instead of tight coupling.

Facade is all about accessing different objects. Facade doesn’t even expose anything, it does portion of your job unlike Builder. (builder is all about creating the different objects.)