This article helps you to learn about design patterns and uses of them. I have tried to easily explain the problem statement where you can use these design patterns.
I have cover all below topics to understand the concepts of Design Pattern.
Table of Contexts -
What Is a Design Pattern?I have cover all below topics to understand the concepts of Design Pattern.
Table of Contexts -
1. What
is Design Pattern?
2. Why
should you use Design Patterns?
3. What
are the Advantages of Design Patterns?
4. What
are the Disadvantages of Design Patterns?
5. What
about Anti-patterns?
6. Are
Design Patterns the same thing as Frameworks?
7. What
are the Gang of Four (GoF) Design Patterns?
8. Which
Pattern is the Foundation of Design Pattern?
9. What
are the types of Design Patterns?
10. What
is Creational Design Pattern?
11. What
is Structural Design Pattern?
12. What
is Behavioural Design Pattern?
13. What
is Factory method pattern?
14. When
to use Factory method pattern?
15. What
is Abstract Factory Pattern?
16. When
you use Abstract Factory Pattern?
17. What
is the difference between factory and abstract factory patterns?
18. What
is Prototype Pattern?
20. What
is MVC Pattern?
The “design pattern” is a general reusable solution for a usually occurring problem in software design. Basically, it is a description for how to solve problems that can be used in different circumstances.
The “design patterns” are used for best practices that the programmers can use to solve common occurring problems when designing an application.
The “design patterns” can improve the development process by providing tested, proven development paradigms.
Stayed Informed – What is Repository Pattern?
Why should you use Design Patterns?
The “design patterns” provides us many more advantages when we are designing an application and some of the following advantages as given bellow,
1. A pattern provides us solution for the common problems which occur in software application.
2. A pattern provides us common platform to the developers and the developers can be used in any programming languages.
3. A pattern provides us code reusability, testability and extensibility.
4. A pattern helps us to improve developer communication.
What are the Advantages of Design Patterns?
The List of Advantages,
1. A pattern provides us code reusability, testability and extensibility.
2. A pattern provides us to enabling large scale reuse of S/W code.
3. A pattern helps us to improve developer communication.
4. Patterns capture expert knowledge and design. It’s make expertise widely available.
5. Pattern allows us to change the design of your application more readily.
6. Pattern allows us to easily test the seam of an application.
7. And so on..
What are the Disadvantages of Design Patterns?
The List of Disadvantages,
1. We don’t lead to direct code reuse and complex in nature. I mean we used extra layer in our applications.
2. The patterns consume more memory because it is in generalized format.
3. The patters are validated by experience and discussion.
4. And so on..
The selection of wrong design pattern can perform a negative effect on your application codes because it is required to choose appropriate design pattern for you and the selection of wrong design pattern is defined as “anti-pattern”.
Are Design Patterns the same thing as Frameworks?
No!, Both the design pattern and framework are different. The design pattern provides only the guideline, not codes but framework deals with codes and so on..
What are the Gang of Four (GoF) Design Patterns?
The “design patterns” gained popularity in computer science after the Design Patterns book. Basically, it was published in 1994/95 by four authors Erich Gamma, Richard Helm, Ralph Johnson and John. These four book authors are known as Gang of Four (GoF).
Which Pattern is the Foundation of Design Pattern?
The Gang of Four (GoF) design patterns is the foundation for all other design patterns that is called the mother of design patterns.
What are the types of Design Patterns?
OR
What are the main Categories of (GoF) Pattern?
The “design patterns” are divided in to three main categories and it’s depending on the problem and its solutions. The categories are,
1. Creational Patterns – The creational patterns are ones that create objects for you, rather than having you instantiate objects directly. This pattern gives us more flexibility in deciding which objects need to be created for a given case.
2. Structural Patterns - These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
3. Behavioural Patterns – The most of these design patterns are specifically concerned with communication between objects or classes.
What Is Creational Design Pattern?
The creational patterns are ones that create objects for you, rather than having you instantiate objects directly. This pattern gives us more flexibility in deciding which objects need to be created for a given case.
We have five types of Design Patterns i.e.
1. Singleton
2. Factory
3. Abstract Factory
4. Prototype
5. Builder
What Is Structural Design Pattern?
These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
We have 6th types of Design Patterns i.e.
1. Adapter
2. Bridge
3. Decorator
4. Facade
5. Flyweight
6. Proxy
What Is Behavioural Design Pattern?
The most of these design patterns are specifically concerned with communication between objects or classes.
We have 11th types of Design Patterns i.e.
1. Chain of responsibility
2. Command
3. Interpreter
4. Iterator
5. Mediator
6. Memento
7. Observer
8. State
9. Strategy
10. Template method
11. Visitor
What Is Factory method pattern?
The factory method pattern is used to create objects at run-time based on your demand. The factory pattern is most commonly used in modern programming languages like C# and Java.
For example, suppose that a bottle factory which produces us different types of bottles. If we went to bottle factory and the order for water-bottles, the factory method will produce water-bottle and will provide us.
When to use Factory method pattern?
The factory method pattern is used when the,
1. Creation of object is done when it is required.
2. Flexibility is important.
3. Sub-classes decide what objects should be created.
4. And so on..
What is Abstract Factory Pattern?
The abstract factory patterns acts as like “super factory” which creates other factories that is also called Factory of factories pattern.
The abstract factory patterns are used to create a set of dependent objects. At the time of creating objects this pattern internally use “factory method design pattern” and “prototype design pattern”.
When you use Abstract Factory Pattern?
The abstract factory pattern is used when we need to create a set of dependent objects or related objects.
At the time of creating objects this pattern internally use “factory method design pattern” and “prototype design pattern”.
What Is the difference between factory and abstract factory patterns?
The Factory Pattern is used to create objects at run-time based on your demand. The factory pattern is most commonly used in modern programming languages like C# and Java.
The Abstract Factory Patterns acts as like “super factory” which creates other factories that is also called Factory of factories pattern.
The Abstract Factory Patterns are used to create a set of dependent objects. At the time of creating objects this pattern internally use “factory method design pattern” and “prototype design pattern”.
What is Prototype Pattern?
The prototype patterns are used to create a new object from the existing object instance and these patterns are also used to create a duplicate object of the current object to enhance application performance.
When we use Prototype Pattern?
The prototype patterns are used when the creation of object is so complex.
The Singleton Pattern ensures that a class has only one instance in the across application and the object instance coordinate to across the app.
This pattern is the simplest design patterns and used to restrict the creation of multiple objects instance.
This pattern is the simplest design patterns and used to restrict the creation of multiple objects instance.
What Is MVC Pattern?
Table
of Contents - Implementing the Singleton Pattern
in C#
1. Introduction
- What Is Singleton Pattern?
2. Non-thread-safe
version
3. Simple
thread safety via locking
4. Double-checked
locking
5. Safety
through initialization
6. Safe
and fully lazy static initialization
7. Lazy<T>
Lest see the example for implementing the
Singleton Pattern in C# in depth –
//First version - not thread-safe
// Bad code! Do not use!
public
sealed class
Singleton
{
private static
Singleton instance=null;
private Singleton()
{ }
public static
Singleton Instance
{
get
{
if
(instance==null)
{
instance
= new Singleton();
}
return
instance;
}
}
}
Example 2 –
//Second version - simple
thread-safety
public
sealed class
Singleton
{
private static
Singleton instance
= null;
private static
readonly object
padlock = new
object();
Singleton() { }
public static
Singleton Instance
{
get
{
lock
(padlock)
{
if
(instance == null)
{
instance
= new Singleton();
}
return
instance;
}
}
}
}
Example 3-
//Third version - attempted
thread-safety using double-check locking
// Bad code! Do not use!
public
sealed class
Singleton
{
private static
Singleton instance
= null;
private static
readonly object
padlock = new
object();
Singleton() { }
public static
Singleton Instance
{
get
{
if
(instance == null)
{
lock
(padlock)
{
if
(instance == null)
{
instance
= new Singleton();
}
}
}
return
instance;
}
}
}
Example 4 –
//Fourth version - not quite as lazy,
but thread-safe without using locks
public
sealed class
Singleton
{
private static
readonly Singleton
instance = new
Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{ }
private Singleton()
{ }
public static
Singleton Instance
{
get
{
return
instance;
}
}
}
Example 5-
//Fifth version - fully lazy
instantiation
public
sealed class
Singleton
{
private Singleton() { }
public static
Singleton Instance
{ get { return
Nested.instance;
} }
private class
Nested
{
// Explicit static
constructor to tell C# compiler
// not to mark type
as beforefieldinit
static
Nested()
{
}
internal
static readonly
Singleton instance
= new Singleton();
}
}
Example 6 –
If you are using .NET 4 (or higher), you should
use the System.Lazy<T> type to
make the laziness really simple -
//Sixth version - using .NET 4's
Lazy<T> type
public
sealed class
Singleton
{
private static
readonly Lazy<Singleton>
lazy =
new
Lazy<Singleton>(()
=> new
Singleton());
public static
Singleton Instance
{ get { return
lazy.Value;
} }
private Singleton()
{ }
}
The Model View Controller (MVC) is a pattern used in software engineering. It is not a design pattern. It is a pattern only. The MVC is most popular pattern now.
These patterns separate the applications structure into three parts that are model, view and controller.
1. Model – The model is responsible for the data access layer and it responds to the requested view and controllers.
2. View – The view is the presentation layer of the application and also called users interface.
3. Controller – The controller is the business logic layer for the application and the controller is responsible for responding to user input and performs interactions on the data model objects and also controls to the models and view and controller work between the view and model.