The Builder Pattern


The Builder Pattern separates the construction of a complex object from its representation so that several different representations can be created depending on the needs of the program.

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


namespace BuilderPattern
{
    public class Vehicle
    {
        public String GetVehicle(IVehicle iVehicle)
        {
            return iVehicle.GetModel();
        }
    }
}
namespace BuilderPattern
{
    public interface  IVehicle
    {
        String GetModel();
    }
}

namespace BuilderPattern
{
    public class Car :IVehicle
    {
        public string GetModel()
        {
            return "Car Model is Honda 2008";
        }
    }
}

namespace BuilderPattern
{
    public class Bike : IVehicle
    {
        public string GetModel()
        {
            return "Bike Model is Yamaha 2007";
        }
    }
}

Create an aspx page ,add the below in the page load
void Page_Load(object sender, EventArgs e)
{
   BuilderPattern.Vehicle vehicle = new BuilderPattern.Vehicle();
   BuilderPattern.IVehicle iVehicle = new BuilderPattern.Car();
   Response.Write(vehicle.GetVehicle(iVehicle));
}


Abstract Factory patterns


The Abstract Factory provides an interface to create and return one of several families of related objects.

Provide an interface for creating families of related or dependent objects without specifying their concrete classes”

The abstract factory is a factory object that returns one of several factories. It can be used to return one of several related classes of objects, each of which can return several different objects on request.


 

Create an Interface

namespace AbstractPattern
{
    public interface IModel
    {
        String GetModel();
    }
}

namespace AbstractPattern
{
    public interface IVehicle
    {
        IModel GetModel();
    }
}

Create Class and Implement Interfaces


namespace AbstractPattern
{
    public class Auto : IVehicle
    {
        public IModel GetModel()
        {
            return new Car();
        }
    }
}
namespace AbstractPattern
{
    public class MotorCycle : IVehicle
    {
        public IModel GetModel()
        {
            return new Bike();
        }
    }
}
namespace AbstractPattern
{
    public class Bike : IModel
    {
        public string GetModel()
        {
            return "YAMAHA";
        }
    }
}
namespace AbstractPattern
{
    public class Car : IModel
    {
        public string GetModel()
        {
            return "HONDA";
        }
    }
}

Create an aspx page ,add the below in the page load

protected void Page_Load(object sender, EventArgs e)
{
   AbstractPattern.IVehicle ivehicleAuto = new AbstractPattern.Auto();
   AbstractPattern.IModel iModelCar = ivehicleAuto.GetModel();
   AbstractPattern.IVehicle ivehicleMC = new AbstractPattern.MotorCycle();
   AbstractPattern.IModel iModelBike = ivehicleMC.GetModel();
   Response.Write("
Car Model is :"
+ iModelCar.GetModel());

   Response.Write("
Bike Model is :"
+ iModelBike.GetModel());