Introduction
This article explains following C# design Patterns with example.
1. Decorator
2. Strategy
3. Singleton
Description:
1. Decorator Pattern
Any application often needs to augment the services provided by methods of some class, perhaps by inserting some preprocessing and post-processing tasks before and after the method calls, respectively.
One way to accomplish this is to bracket each method invocation with calls to functions that achieve the desired effect. However, this approach is not only cumbersome, it also limits the framework's extensibility. For instance, if distinct pre- and post-processing tasks were to be carried out for different clients, the application logic would be obscured by conditional statements, leading to a maintenance nightmare.
The question, then, is how to enhance the functionality offered by a class in a manner that does not cause repercussions in client code. The Decorator pattern is just what's needed.
Consider a common example of creating new account. We usually check for account existence before creating a new one. Here comes the decorator pattern in action. we add additional responsibility to class NewAccount to check for account existence. but while calling CreateNewAccount method we need not bother about checking
for the existing account. Additional responsibility added to NewAccount class will do this every time u call it.
The Decorator pattern thus allows dynamic and transparent addition and removal of responsibilities without affecting client code. It is particularly useful when a range of extensions or responsibilities can be applied to existing classes, and when defining subclasses to accommodate all those extensions is impractical.
public class NewAccount
{
}
public class NewAccountDecorator : NewAccount
{
}
2. Strategy Pattern
Applications are often written so that the way they perform a particular task varies, depending on user input, the platform it's running on, the environment it's been deployed in, and so on. An example is method of payment : the customer may either pay by cash or card in that case the application should choose it's functionality depending upon the user choice given.
In short the strategy pattern allows us choose from the multiple alogorithms to perform the operation.
PaymentMethod objMethodOfPayment = new PaymentMethod();
switch objMethodOfPayment .SelectedText)
{
case "Cash":
objMethodOfPayment .setMethodOfPayment(new PayByCash());
break;
case "Card":
objMethodOfPayment .setMethodOfPayment(new PayByCard());
break;
}
objMethodOfPayment .Pay(amount);
3. Singleton Pattern
In some situations we may want to allow single instance of an class to create thoughout the application.The Singleton class allows us to implement single instance of the class.
One other advantage to this pattern is that creation of the singleton can be delayed until it is actually needed. A variable declared at global scope will be created on startup regardless of whether it is needed—it may very
well be that the object isn't always needed as shown below
private static SingletonClass instance;
public static SingletonClass Instance()
{
if (instance == null)
{
instance = new SingletonClass();
}
return instance;
}