Skip to main content

Design Patterns

                                                      Design pattern


1. Factory Design Pattern 

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.



Step 1

Create an interface

IShape.cs

public interface IShape {
   void draw();
}

Step 2

Create concrete classes implementing the same interface.
Rectangle.cs
public class Rectangle : IShape {

   @Override
   public void draw() {
      Console.WriteLine("Inside Rectangle::draw() method.");
   }
}
Square.cs
public class Square : IShape {

   @Override
   public void draw() {
      Console.WriteLine("Inside Square::draw() method.");
   }
}
Circle.cs
public class Circle : IShape {

   @Override
   public void draw() {
      Console.WriteLine("Inside Circle::draw() method.");
   }
}

Step 3

Create a Factory to generate object of concrete class based on given information.
ShapeFactory.cs
public class ShapeFactory {
 
   //use getShape method to get object of type shape 
   public IShape getShape(String shapeType){
      if(shapeType == null){
         return null;
      }  
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
         
      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();
         
      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
      
      return null;
   }
}

Step 4

Use the Factory to get object of concrete class by passing an information such as type.
FactoryPatternDemo.cs
public class FactoryPatternDemo {

   public static void main(String[] args) {
      ShapeFactory shapeFactory = new ShapeFactory();

      //get an object of Circle and call its draw method.
      Shape shape1 = shapeFactory.getShape("CIRCLE");

      //call draw method of Circle
      shape1.draw();

      //get an object of Rectangle and call its draw method.
      Shape shape2 = shapeFactory.getShape("RECTANGLE");

      //call draw method of Rectangle
      shape2.draw();

      //get an object of Square and call its draw method.
      Shape shape3 = shapeFactory.getShape("SQUARE");

      //call draw method of circle
      shape3.draw();
   }
}

Step 5

Verify the output.
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.

Comments

Popular posts from this blog

Claims Class For security in .NET4.5 with C#

What is Claim ? A claim in the world of authentication and authorization can be defined as a statement about an entity, typically a user. A claim can be very fine grained: Ram is an admin Ram’s email address is Ram@yahoo.com Ram lives in Mumbai Tom’s allowed to view sales figures between 2009 and 2012 Tom’s allowed to wipe out the universe Claims originate from a trusted entity other than the one that is being described. This means that it is not enough that Tom says that he is an administrator. If your company’s intranet runs on Windows then Tom will most likely figure in Active Directory/Email server/Sales authorisation system and it will be these systems that will hold these claims about Tom.  The idea is that if a trusted entity such as AD tells us things about Tom then we believe them a lot more than if Tom himself comes with the same claims . This external trusted entity is called an  Issuer . The KEY in the key-value pairs is called a  Typ...

C# 6

New feature No.1 in C# 6.0   1. using Static This is a new concept in C# 6.0 that allows us to use any class that is static as a namespace that is very usefull for every developer in that code file where we need to call the static methods from a static class like in a number of times we need to call many methods from Convert.ToInt32() or Console.Write(),Console.WriteLine() so we need to write the class name first then the method name every time in C# 5.0. In C# 6.0 however Microsoft announced a new behavior to our cs compiler that allows me to call all the static methods from the static class without the name of the classes because now we need to first use our static class name in starting with all the namespaces. In C# 5.0 In C# 6.0 Code in 5.0 using  System;   using  System.Collections.Generic;   using  System.Linq;   using  System.Text;   using  System.Threading.Tasks;   namespace...

Why To Use C#

Which one to use, C++ or C#?”, and many more similar questions. It is harder to provide an answer to such questions on multiple platforms and to multiple questions. That is why, I thought, why not post a blog post to cover most of the aspects of the question?  In this post I am going to cover a few of the concepts that you may want to understand to choose either language from the most widely used languages: C or C++ Java C#  (or .NET framework itself) Basically, my own preference in many cases is C#, but that depends on what I am going to build. I have been using C, C++, Java, and C # in many applications depending on their needs, depending on how they allow me to write the programs,  and depending on whether it is the suitable programming language for this type of project and application architecture. The way I do that,is just a trick which I am going to share with you in this blog post. Among these a few of the very basic things to consider are: How simple an...