Showing posts with label design Pattern. Show all posts
Showing posts with label design Pattern. Show all posts

Prototype Design Pattern in C# with Examples

Prototype Design Pattern in C# with Examples

In this article, we are going to discuss prototype design patterns in C#.

prototype design patterns are the creational design pattern.

As per the Gang of Four Definition: "Prototype Design Pattern Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype"

As per the prototype design pattern, we need to create an object once and can use it across the application, creating an object again and again in an application is an expensive and time-consuming task so as per this pattern once we create an object, we can utilize it wherever we need it.

  • reducing the complexity of the application to create the objects.
  • reduce the need to creating of an object again and again.
  • We can remove the object in runtime.

Prototype Design Pattern in C# 

below is an example of creating a prototype design pattern using the shallow copy method. here we are using the inbuild ICloneable interface.

public class Student: ICloneable
{
    public string Name { get; set; }
    public string Address { get; set; }
    public int Age { get; set; }
    public string Gender { get; set; }
    public object Clone()
    {
        return this.MemberwiseClone();
    }
 
}
public class ProtoTypeDesignPattern
{
    static void Main(string[] args)
    {
        Student _student = new Student
        {
            Name = "dotnetOffice",
            Address = "XYZ",
            Age = 10,
            Gender = "Male"
        };

        Console.WriteLine($"Student Name:  {_student.Name}");

        Student cloneName = _student.Clone() as Student;
        Console.WriteLine($"Cloned Name:  {_student.Name}");

        cloneName.Name = "other name";
        Console.WriteLine($"origional name - {_student.Name}");
        Console.WriteLine($"Cloned Name - {cloneName.Name}");
    }
}

 

Fluent Interface Design Pattern in C# with Examples

Fluent Interface Design Pattern in C# with Examples


In this article, we will talk about the Fluent Interface Design Pattern in C# with examples. The Fluent Interface Design Pattern is a type of Creational Design Pattern.

Fluent Interface Design Pattern

The purpose of a fluent design pattern is to use multiple properties for the objects by connecting them with the .(dot) without giving the specifics of the object again.

A fluent interface design pattern is implemented by using the method cascading or concrete method chaining.

Implementation of Fluent Interface Design Pattern in C#
let's create a student class

public class Student
{

    public string Name { get; set; }

    public int Age { get; set; }

    public string Address { get; set; }

}

now let's assign values to student class

Student emp = new Student()
{

    Name  = "DotNet office",

    Age  = 12,

    Address  = "India"

};


What is Method Chaining?

Method chaining is the technique where each method returns an object and all of these methods can be chained together to create a single statement. to implement this, first, we need to create the wrapper class around the student class.

 public class StudentFluentExample


    {

        private Student obj = new Student();

        public StudentFluentExample NameOfTheStudent(string name)

        {

          obj.Name = name;

            return;

        }

        public StudentFluentExample Age(int age)

        {

            obj.Age = age;

            return;

        }

        public StudentFluentExample LivesIn(string address)

        {

            obj.Address = address;

            return;

        }

       

    }

now to access the above class methods we have to create an object of the class like below

as per fluent interfaces design pattern, we can access properties by connecting them with .(dot) like below
static void Main(string[] args)

        {
          StudentFluentExample obj = new StudentFluentExample();
          obj.NameOfTheStudent("Dotnet office").Age(12).LivesIn("India");

        }

uses of the Fluent Interface Design Pattern in C#
  1. Fluent code is more readable and allows us to vary a product's internal representation
  2. A fluent design pattern encapsulates the code for construction and representation and it Provides control over the steps of the object construction process.
  3. Sorting, Searching, and pagination using LINQ are some of the real-world examples of the fluent interface in the combination with the builder design pattern.


Builder Design Pattern in C# with Real-time Example in C#

 

In this article, we will discuss Builder Design Pattern in C# with models. it is part of the Creational Design Pattern.

Definition: "Separate the construction of a complex object from its representation so that the same construction process can create different representations"

Builder Design Pattern solves the situation of increasing constructor parameters and constructors of a given class by providing a step-by-step initialization of Parameters. After step-by-step initialization, it returns the resulting constructed object at once.

Implementation Guidelines: We need to Choose Builder Design Pattern when

  1. We need to break up the construction of a complex object
  2. We need to create a complex object and it should be independent of the parts that make up the object
  3. The construction process must allow multiple representations of the same class

As per the real-time example for the Builder design pattern, we are going build a laptop for an employee, and for doing a setup or building a laptop, we need a couple of the configurations and those are Builder RAM, SSD, USB Mouse, etc

So let's build a laptop

Step 1: Create a class file with the name SystemDetails.cs and then copy and paste the following code into it. This is going to be our product and in this class, we put all the attributes like SSD, RAM, USB, and ProductName which are common to prepare a system(i.e. laptop or desktop). We also create one method (i.e. ShowProductInfo() ) which returns the System details.

    public class SystemDetails

    {

        public int RAM { get; set; }

        public int SSD { get; set; }

        public int HDD { get; set; }

        public string USB { get; set; }

        public string ProductName { get; set; }

 

        public string ShowProductInfo()

        {

            return "SystemName:  " + ProductName + " Contain these features " + RAM + " GB RAM, " + SSD + "GB SSD, " + HDD

                            + " GB HDD, " + USB + " USB " + ProductName + "]\n";

        }

    }


Step2: Creating Abstract Builder (SystemBuilder)
Create a class file with the name SystemBuilder.cs and then copy and paste the following into it. This SystemBuilder class is going to be an abstract class and will act as a blueprint for any subclasses that want to create a System. So, it will have different subclass implementations for different system types such as laptops, desktops,  etc. This class is having different abstract methods to Assign the RAM, HDD, SSD, USB, and SystemType.

public abstract class SystemBuilder

    {

            protected SystemDetails details;

 

            public void CreateBeverage()

            {

            details = new SystemDetails();

            }

            public SystemDetails GetBeverage()

            {

                return details;

            }

 

            public abstract void SetSystemType();

            public abstract void AssignRam();

            public abstract void AssignSSD();

            public abstract void AssignHDD();

            public abstract void AssignUSB();

       

    }


Step3: Creating Concrete Builder (LaptopBuilder and DesktopBuilder)

LaptopBuilder.cs

public class LaptopBuilder : SystemBuilder

    {

        public override void SetSystemType()

        {

            Console.WriteLine("Step 1 : Assign System Type");

            GetSystem().ProductName = "Laptop";

        }

 

        public override void AssignRam()

        {

            Console.WriteLine("Step 2 : Assign RAM");

            GetSystem().RAM = 32;

        }

        public override void AssignHDD()

        {

            Console.WriteLine("Step 3 : Assign HDD");

            GetSystem().HDD = 30;

        }

 

        public override void AssignSSD()

        {

            Console.WriteLine("Step 4 : Assign SSD");

            GetSystem().SSD = 8;

        }

 

        public override void AssignUSB()

        {

            Console.WriteLine("Step 5 : Assign USB");

            GetSystem().USB = "USB";

        }

    }

DesktopBuilder.cs

  public class DesktopBuilder : SystemBuilder

    {

        public override void SetSystemType()

        {

            Console.WriteLine("Step 1 : Assign System Type");

            GetSystem().ProductName = "Desktop";

        }

 

        public override void AssignRam()

        {

            Console.WriteLine("Step 2 : Assign RAM");

            GetSystem().RAM = 32;

        }

        public override void AssignHDD()

        {

            Console.WriteLine("Step 3 : Assign HDD");

            GetSystem().HDD = 30;

        }     

 

        public override void AssignSSD()

        {

            Console.WriteLine("Step 4 : Assign SSD");

            GetSystem().SSD = 8;

        }

 

        public override void AssignUSB()

        {

            Console.WriteLine("Step 5 : Assign USB");

            GetSystem().USB = "USB";

        }

 

       

   

    }

Step4: Creating the Director (SystemDirector)

Once you created the concrete builder classes, then you need to need to create the director. The director is the component that will execute the required steps in a particular order to create a beverage.

So, create a class file with the name SystemDirector.cs and then copy and paste the following code in it. This class is having one generic method which will take SystemBuilder as an input parameter and then create and return a particular System object.

    public class SystemDirector

    {

        public SystemDetails MakeSystem(SystemBuilder builder)

        {

            builder.CreateSystem();

            builder.SetSystemType();

            builder.AssignRam();

            builder.AssignHDD();

            builder.AssignSSD();

            builder.AssignUSB();

 

            return builder.GetSystem();

        }

    }


Step 5: Client Code.

Now let's write the below code in the main method. Here, first, we will create an instance of the SystemDirector class and then create an instance of the LaptopBuilder class. Once we have the SystemDirector and LaptopBuilder instances, then we call the MakeSystem method on the SystemDirector instance bypassing the LaptopBuilder instance as an argument that will create and return the laptop. Again you need to follow the same process to make a Desktop.

static void Main(string[] args)

        {

            SystemDetails details;

            SystemDirector director = new SystemDirector();

 

            LaptopBuilder tea = new LaptopBuilder();

            details = director.MakeSystem(tea);

            Console.WriteLine(details.ShowProductInfo());

            DesktopBuilder coffee = new DesktopBuilder();

            details = director.MakeSystem(coffee);

            Console.WriteLine(details.ShowProductInfo());

 

            Console.ReadLine();

        }


Output


reference:-https://csharp-video-tutorials.blogspot.com/2017/09/builder-design-pattern-implementation.html