×

C# Design Patterns

What are the Design Patterns?

  • Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.
  • Design patterns are the templates provided in order to solve the programming problem.
  • They are generally used to solve the object creation and its integration
  • When user is working on some project, he/she knows the requirements of the clients. So user selects the design pattern and use it for the project.

Types of Design Patterns

The design patterns are classified as:

  1. Creational Design Pattern
  2. Structural Design Pattern 
  3. Behavioral Design Pattern

1. Creational Design Patterns

  • The Creational Design Pattern helps the programmer to decide which object or group of objects need to be created by understanding the project scope and requirements.
  • It only involves the object instantiation login and nothing other than this like implementation is not involved in this design pattern.
  • It generally involves creation and instantiation of object.
  • Creational design pattern involve different types of sub-categories which are as follows:
  • Factory Method design pattern: It is used to create the objects of sub-classes. For example, programmer has one base class and 2 derived classes and he wants to create and return the object of derived class, factory method pattern.
  • Builder design pattern: Builder design pattern is used when user creates a complex object using multiple simple/generic objects. It separate the construction from representation
  • Prototype design pattern: It creates a new object from existing instance of object.  User can clone the existing object/model into new object and the changes performed by user into the new object won’t affect the original/existing object.
  • Singleton design pattern: In this type of pattern, only one intake of class is created and entire application uses the same object to perform different operations.

2. Structural Design Patterns

  • In order to maintain the structure of classes and interfaces, structural design patterns are used.
  • For example, we have two classes in our project namely Student and Books and Book class is defined within the Student class and they have one to many relationships between them. But now as client’s requirement changes programmer want Student and Book class should be independent of each other. So this is call structural change which affects the project. And here structural design pattern helps us.
  • To maintain the relationship between classes and interfaces, structural design pattern is used.
  • Adapter design pattern: Two independent or incompatible classes/objects can work together with the help of Adapter class. And this is how adapter class helps the user to perform communication between two independent classes.
  • Bridge design pattern: In this design pattern, two terms namely abstraction and implementation are involved. And these two terms are not dependent on each other. The change in one won’t affect the other part. And here client can access only abstraction part only.
  • Composite design pattern: In Composite, the group of objects are represented using a tree structure where each node represents an object. In Composite design pattern, all group of objects are treated with similar way as a single object.
  • Decorator design pattern: The Decorator is used to add the feature/functionality to an object without affecting the existing functionalities. It is used to dynamically add functionalities at runtime without disturbing the existing functionality.
  • Façade design pattern: It provides an interface to the client which is easy-to-use and through which client can access the system easily.
  • Flyweight design pattern: This design pattern is used where there is needed to create a large number of objects which are of similar type. As user creates the large number of objects, eventually the memory required for those objects is also large. So here flyweight is used which decreases the number of objects of similar type and eventually reduces the size of memory required.
  • Proxy design pattern: It is termed as an object which is used to access real-time object behind the scene. It is act as third party object or interface in order to access the original object or class.

3. Behavioral Design Patterns

  • To define the communication between interfaces, classes and objects, behavioral design patterns are used.
  • If you change the definition of an interface or class, then that change should be reflected to the other parts of program as well.
  • For example, Product is one class in project and later on programmer add some other properties to the Product class so this is called behavioral change and here behavioral design patterns helps us to do such changes without disturbing the project flow.
  • Following are the sub-categories of Behavioral design pattern:
  • Chain of Responsibility design pattern: As its name suggests, it creates the chain of receiver objects. And if the first receiver is not able to handle request, the request is sent to the next receiver. Here, a chain is developed and request is sent from one receiver to the next and it continues further.
  • Command design pattern: In Command design pattern, a request is encapsulated as an object in form of command, then this command is sent to invoker then the invoker finds an appropriate object which can handle/execute the command.
  • Iterator design pattern: In Iterator design pattern, user can access the elements of the aggregate object without knowing the internal representation of the object. And access of elements is sequential, it means user can access the elements sequentially.
  • Mediator design pattern: In mediator design pattern, a mediator object is defined and this mediator object is responsible for the communication between different objects. Here, the mediator object defines the loose coupling which means the interaction between the objects is loosely coupled that is less interaction between the objects so that the change in one object won’t affect the other object that much.
  • Memento design pattern: This pattern is used to perform the rollback or undo type of operation. It stores the internal state of an object.
  • Observer design pattern: In Observer design pattern, there is one-to-many relationship between the objects. In one-to-many relationship, one (called as Subject) and many (called as Dependents) and one and many are nothing but the one object and many objects. So if there some changes occurs in one object, all its dependents get notification about the change and get updated automatically. It is also knows as Dependent design pattern.
  • State design pattern: Using State design pattern, an object can its behavior depending on its internal state. It means that this design pattern help user to change the state of an object and eventually its behavior also get changed.
  • Strategy design pattern: In Strategy design pattern, to solve one problem set of solution are made. And at runtime, one solution is selected out of those set of solutions. This design pattern is also known as Policy design pattern.
  • Template method design pattern: In template design pattern, an algorithm is defined which contains the sequence of steps. And template method allows subclasses to redefine some steps of the algorithm without changing the structure/sequence of an algorithm.
  • Visitor design pattern: Visitor object is used in visitor design pattern. Visitor object changes the algorithm of an object. Hence, the algorithm element object varies as the visitor object varies.

Summary:

Design pattern play an important role while solving any development problem. Design pattern provides away to approach a problem. In C#, there are three types of design patterns are available and these patterns are also classified further into sub categories. Programmer has to identify this definition and needs to understand all design patterns they have to take a design pattern and apply it to the solution. This is how programmer can use these design patterns in real time to solve the problem. To summarize an entire thing we can say that design patterns are important code snippets that are reusable and solves the problem. Hence, C# developers look at the design patterns as an important thing as C# is dynamic programming language.


Related Topics

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.