Monday, November 15, 2010

Implement Factory Design Pattern using C#.Net

Create an application with four classes. Three of the classes should contain data and behavior characteristics for circle, rectangle, and cylinder. the fourth class should allow the user to input a figure type from a menu of options. prompt for appropiate values based on the inputted figure type, instantiate an object of the type entered, and display characteristics about the object.
The main class should work with the other classes (circle, rectangle, and cylinder)
To achieve this scenario it is required to implement the factory design pattern.

    class Program
    {
        static void Main(string[] args)
        {
            int shapeType = GetShapeType();

            if (shapeType>3)
            {
                
                Console.WriteLine("Invalid shape type. Please select either 1, 2 or 3");
                shapeType = GetShapeType();
            }

            IShape objShape;
            objShape = ShapeFactory.GetShape(shapeType);
            objShape.DisplayShapeDetails();
            Console.ReadKey();
        }

        private static int GetShapeType() 
        {
            Console.Clear();
            int shapeType;
            Console.Write("Please enter the type of Shape:");
            shapeType = Convert.ToInt16(Console.ReadLine());
            return shapeType;
        }
    }

This enum will generalize the implementation for various classes required related to other different shapes:

    public enum Shape
    { 
        Circle=1,
        Rectangle=2,
        Cylinder=3
    }

This interface will be used for defining the contract for various shapes

    public interface IShape 
    {
        void DisplayShapeDetails();
    }

As per the requirement specified ShapeFactory is the fourth class which is responsible for centralized creation of the object of type of class of the shape specified by the user.

    public class ShapeFactory
    {
        public static IShape GetShape(int _shapeType) 
        {
            IShape objShape;

            switch (_shapeType)
            {
                case ((int)Shape.Circle):
                    int radius;
                    Console.Write("Please enter the Radius of the circle:");
                    radius = Convert.ToInt16(Console.ReadLine());
                    objShape= new Circle(radius);
                    break;

                case ((int)Shape.Rectangle):
                    int length, width;
                    Console.Write("Please enter the Length of the Rectangle:");
                    length = Convert.ToInt16(Console.ReadLine());
                    Console.Write("Please enter the Width of the Rectangle:");
                    width = Convert.ToInt16(Console.ReadLine());
                    objShape = new Rectangle(length, width);
                    break;

                case ((int)Shape.Cylinder):
                    int height;
                    Console.Write("Please enter the Height of the Cylinder:");
                    height = Convert.ToInt16(Console.ReadLine());
                    Console.Write("Please enter the Radius of the Cylinder:");
                    radius = Convert.ToInt16(Console.ReadLine());
                    objShape = new Cylinder(height, radius);
                    break;

                default:
                    objShape = null;
                    break;
            }
            return  objShape;
        }
    }

Circle class

    public class Circle  : IShape
    {
        private Shape _shapeType;
        private double _radius;

        public Circle() { }

        public Circle(double Radius) 
        {
            _radius = Radius;
            _shapeType = Shape.Circle;
        }

        public void DisplayShapeDetails()
        {
            Shape test = (Shape)_shapeType;
            Console.WriteLine("Area of the selected shape '{0}' is {1}", Enum.GetName(typeof(Shape), _shapeType), CalculateArea().ToString());
        }

        private double CalculateArea() 
        {
            return Math.PI * (_radius * _radius);
        }
    }

Rectangle Class

    public class Rectangle : IShape
    {
        private Shape _shapeType;
        private double _length;
        private double _width;

        public Rectangle() { }

        public Rectangle(double Length, double Width)
        {
            _length = Length;
            _width = Width;
            _shapeType = Shape.Rectangle;
        }

        public void DisplayShapeDetails()
        {
            Shape test = (Shape)_shapeType;
            Console.WriteLine("Area of the selected shape '{0}' is {1}", Enum.GetName(typeof(Shape), _shapeType), CalculateArea().ToString());
        }

        private double CalculateArea()
        {
            return _length * _width;
        }
    }

Cylinder class

    public class Cylinder : IShape
    {
        private Shape _shapeType;
        private double _height;
        private double _radius;

        public Cylinder() { }

        public Cylinder(double Height, double Radius)
        {
            _height = Height;
            _radius = Radius;
            _shapeType = Shape.Cylinder;
        }

        public void DisplayShapeDetails()
        {
            Shape test = (Shape)_shapeType;
            Console.WriteLine("Area of the selected shape '{0}' is {1}", Enum.GetName(typeof(Shape), _shapeType), CalculateVolume().ToString());
        }

        private double CalculateVolume()
        {
            return (Math.PI * _height * _radius * _radius);
        }
    }

If it is required to add another shape (For example Cube) then you have define a corresponding class named 'Cube' which implements the 'IShape' interface and need to add a case for the same into the ShapeFactory class.

No comments:

Post a Comment