示例#1
0
        public void HondaFactory_ManufactureHondaCar()
        {
            var sut    = new HondaFactory();
            var result = sut.ManufactureCar();

            Assert.That(result.Name, Is.EqualTo("Honda"));
        }
    static void Main(string[] args)
    {
        VehicleFactory honda       = new HondaFactory();
        VehicleClient  hondaclient = new VehicleClient(honda, "Regular");

        Console.WriteLine("******* Honda **********");
        Console.WriteLine(hondaclient.GetBikeName());
        Console.WriteLine(hondaclient.GetScooterName());

        hondaclient = new VehicleClient(honda, "Sports");
        Console.WriteLine(hondaclient.GetBikeName());
        Console.WriteLine(hondaclient.GetScooterName());

        VehicleFactory hero       = new HeroFactory();
        VehicleClient  heroclient = new VehicleClient(hero, "Regular");

        Console.WriteLine("******* Hero **********");
        Console.WriteLine(heroclient.GetBikeName());
        Console.WriteLine(heroclient.GetScooterName());

        heroclient = new VehicleClient(hero, "Sports");
        Console.WriteLine(heroclient.GetBikeName());
        Console.WriteLine(heroclient.GetScooterName());

        Console.ReadKey();
    }
示例#3
0
    public static AbsCar CreateCar(CarType type)
    {
        AbsCar        target  = null;
        AbsCarFactory factory = null;

        switch (type)
        {
        case CarType.Benz:
            factory = new BenzFactory();
            break;

        case CarType.Bmw:
            factory = new BmwFactory();
            break;

        case CarType.Honda:
            factory = new HondaFactory();
            break;

        default:
            break;
        }
        if (null == factory)
        {
            return(null);
        }
        target = factory.CreateCar();
        return(target);
    }
示例#4
0
        static void Main(string[] args)
        {
            VehicleFactory honda = new HondaFactory();

            VehicleClient hondaclient = new VehicleClient(honda, "Regular");

            Console.WriteLine("******* Honda **********");
            Console.WriteLine(hondaclient.GetBikeName());
            Console.WriteLine(hondaclient.GetScooterName());

            hondaclient = new VehicleClient(honda, "Sports");
            Console.WriteLine(hondaclient.GetBikeName());
            Console.WriteLine(hondaclient.GetScooterName());

            hondaclient = new VehicleClient(honda, "Regular");
            Console.WriteLine(hondaclient.GetCar());

            VehicleFactory suzuki       = new SuzukiFactory();
            VehicleClient  suzukiClient = new VehicleClient(suzuki, "Regular");

            Console.WriteLine("******* Suzuki **********");
            Console.WriteLine(suzukiClient.GetBikeName());
            Console.WriteLine(suzukiClient.GetScooterName());

            suzukiClient = new VehicleClient(suzuki, "Sports");
            Console.WriteLine(suzukiClient.GetBikeName());
            Console.WriteLine(suzukiClient.GetScooterName());

            suzukiClient = new VehicleClient(honda, "Regular");
            Console.WriteLine(suzukiClient.GetCar());

            Console.ReadKey();
        }
示例#5
0
        public void Test1()
        {
            VehicleFactory honda       = new HondaFactory();
            VehicleClient  hondaclient = new VehicleClient(honda, "Regular");
            string         actual      = hondaclient.GetBikeName();
            string         expected    = "Regular Bike- Name";

            Assert.Equal(expected, actual);
        }
示例#6
0
        /// <summary>
        /// abstract factory pattern is a creational pattern in which interfaces are defined
        /// for creating families of related objects without specifying their actual implementations
        ///
        /// whe using this pattern, you create factories which return many kinds of related objects.
        /// Participants : 1-abstract Factory, 2-Conceret Factory, 3-Abstract Product, 4-Conceret Product, 5-Client [uses abstract factory, and abstract products]
        ///
        /// Example:
        /// lets say we want to model 2 kinsds of recipes: a sandwich and a dessert, and let is make assumption that adults and kids
        /// do not eat the same things, and so we want one of each kind of reecipe for adults and for kids.
        /// AbstractProduct [sandwich, dessert] : abstract class represnts generic kind of recipes.
        /// Abstract Factory : abstract class that will return a Sandwich and a Dessert.
        /// ConcreteProduct  : implementing the actual objects.
        /// ConcreteFactory  : implements the AbstractFactory
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            VechileFactory honda       = new HondaFactory();
            ClientVechile  hondaClient = new ClientVechile(honda, "regular");

            Console.WriteLine("*** Honda ***");
            Console.WriteLine(hondaClient.GetBikeName());
            Console.WriteLine(hondaClient.GetCarName());

            hondaClient = new ClientVechile(honda, "sport");
            Console.WriteLine(hondaClient.GetBikeName());
            Console.WriteLine(hondaClient.GetCarName());


            Console.WriteLine("Who are you? (A)dult or (C)hild?");
            char input = Console.ReadKey().KeyChar;


            Recipe factory;

            switch (input)
            {
            case 'A': factory = new Adult();
                break;

            case 'B': factory = new Kids();
                break;

            default: throw new NotImplementedException();
            }

            var sandwich = factory.CreateSandwich();
            var dessert  = factory.CreateDessert();

            Console.WriteLine("\n sandwich" + sandwich.GetType().Name);
            Console.WriteLine("\n dessert" + dessert.GetType().Name);

            Console.ReadLine();
        }
示例#7
0
        static void Main(string[] args)
        {
            //Behavioral Patterns
            Console.WriteLine("Behavioral");
            // Wait for user
            Console.ReadKey();

            //1 - Command
            Console.WriteLine("Command");
            Console.WriteLine("Enter Commands (ON/OFF) : ");
            string   cmd        = Console.ReadLine();
            Light    lamp       = new Light();
            ICommand switchUp   = new FlipUpCommand(lamp);
            ICommand switchDown = new FlipDownCommand(lamp);
            Switch   s          = new Switch();

            if (cmd == "ON")
            {
                s.StoreAndExecute(switchUp);
            }
            else if (cmd == "OFF")
            {
                s.StoreAndExecute(switchDown);
            }
            else
            {
                Console.WriteLine("Command \"ON\" or \"OFF\" is required");
            }
            // Wait for user
            Console.ReadKey();

            //2 - Chain of responsability
            Console.WriteLine("Chain of responsability");
            Approver rohit = new Clerk();
            Approver rahul = new AssistantManager();
            Approver manoj = new Manager();

            rohit.Successor = rahul;
            rahul.Successor = manoj;
            var loan = new Loan {
                Number = 2034, Amount = 24000.00, Purpose = "Laptop Loan"
            };

            rohit.ProcessRequest(loan);
            loan = new Loan {
                Number = 2035, Amount = 42000.10, Purpose = "Bike Loan"
            };
            rohit.ProcessRequest(loan);
            loan = new Loan {
                Number = 2036, Amount = 156200.00, Purpose = "House Loan"
            };
            rohit.ProcessRequest(loan);
            // Wait for user
            Console.ReadKey();

            //3 - Memento
            Console.WriteLine("Memento");
            SalesProspect sp = new SalesProspect();

            sp.Name   = "Noel van Halen";
            sp.Phone  = "(412) 256-0990";
            sp.Budget = 25000.0;
            // Store internal state
            ProspectMemory m = new ProspectMemory();

            m.Memento = sp.SaveMemento();
            // Continue changing originator
            sp.Name   = "Leo Welch";
            sp.Phone  = "(310) 209-7111";
            sp.Budget = 1000000.0;
            // Restore saved state
            sp.RestoreMemento(m.Memento);
            // Wait for user
            Console.ReadKey();


            //Creational Patterns
            Console.WriteLine("Creational");
            // Wait for user
            Console.ReadKey();

            //1-Singleton
            Console.WriteLine("Singleton");
            Singleton.Instance.Show();
            // Wait for user
            Console.ReadKey();

            //2-Prototype
            Console.WriteLine("Prototype");
            Developper dev = new Developper {
                Name = "Antoine", Role = "Team Leader", PrefferedLanguage = "C#", WordsPerMinute = 42
            };
            Typist typ = new Typist {
                Name = "Casper", Role = "Typist", WordsPerMinute = 175
            };

            Console.WriteLine(typ.GetDetails());
            Console.WriteLine(dev.GetDetails());
            // Wait for user
            Console.ReadKey();

            //3-AbstractFactory
            Console.WriteLine("Abstract Factory");
            DesignPatterns.Creational.AbstractFactory.Sample.VehiculeFactory honda = new HondaFactory();
            VehiculeClient hondaclient = new VehiculeClient(honda, "Regular");

            Console.WriteLine("****** Honda ******");
            Console.WriteLine(hondaclient.GetBikeName());
            Console.WriteLine(hondaclient.GetScooterName());
            VehiculeClient hondaclient2 = new VehiculeClient(honda, "Sports");

            Console.WriteLine(hondaclient.GetBikeName());
            Console.WriteLine(hondaclient.GetScooterName());
            DesignPatterns.Creational.AbstractFactory.Sample.VehiculeFactory hero = new HondaFactory();
            VehiculeClient heroclient = new VehiculeClient(hero, "Regular");

            Console.WriteLine("****** Hero ******");
            Console.WriteLine(heroclient.GetBikeName());
            Console.WriteLine(heroclient.GetScooterName());
            VehiculeClient heroclient2 = new VehiculeClient(hero, "Sports");

            Console.WriteLine(heroclient.GetBikeName());
            Console.WriteLine(heroclient.GetScooterName());
            // Wait for user
            Console.ReadKey();

            //4- Factory Method
            Console.WriteLine("Factory Method");
            DesignPatterns.Creational.FactoryMethod.Sample.VehiculeFactory factory = new ConcreteVehiculeFactory();
            IFactory scooter = factory.GetVehicule("Scooter");

            scooter.Drive(10);
            IFactory bike = factory.GetVehicule("Bike");

            bike.Drive(20);
            // Wait for user
            Console.ReadKey();

            //5- Builder
            Console.WriteLine("Builder");
            var vehicleCreator = new VehicleCreator(new HeroBuilder());

            vehicleCreator.CreateVehicle();
            var vehicle = vehicleCreator.GetVehicle();

            vehicle.ShowInfo();
            Console.WriteLine("---------------------------------------------");
            vehicleCreator = new VehicleCreator(new HondaBuilder());
            vehicleCreator.CreateVehicle();
            vehicle = vehicleCreator.GetVehicle();
            vehicle.ShowInfo();
            // Wait for user
            Console.ReadKey();

            //Structural
            Console.WriteLine("Structural");
            // Wait for user
            Console.ReadKey();

            //1 - Proxy
            Console.WriteLine("Proxy");
            ProxyClient proxy = new ProxyClient();

            Console.WriteLine("Data from Proxy Client = {0}", proxy.GetData());
            // Wait for user
            Console.ReadKey();

            //2 - Flyweight
            Console.WriteLine("Flyweight");
            ShapeObjectFactory sof   = new ShapeObjectFactory();
            IShape             shape = sof.GetShape("Rectangle");

            shape.Print();
            shape = sof.GetShape("Rectangle");
            shape.Print();
            shape = sof.GetShape("Rectangle");
            shape.Print();
            shape = sof.GetShape("Circle");
            shape.Print();
            shape = sof.GetShape("Circle");
            shape.Print();
            shape = sof.GetShape("Circle");
            shape.Print();
            int NumObjs = sof.TotalObjectsCreated;

            Console.WriteLine("\nTotal No of Objects created = {0}", NumObjs);
            // Wait for user
            Console.ReadKey();

            //3 - Facade
            Console.WriteLine("Facade");
            CarFacade facade = new CarFacade();

            facade.CreateCompleteCar();
            // Wait for user
            Console.ReadKey();

            //4 - Bridge
            Console.WriteLine("Bridge");
            IMessageSender email   = new EmailSender();
            IMessageSender queue   = new MSMQSender();
            IMessageSender web     = new WebServiceSender();
            Message        message = new SystemMessage();

            message.Subject       = "Test Message";
            message.Body          = "Hi, This is a Test Message";
            message.MessageSender = email;
            message.Send();
            message.MessageSender = queue;
            message.Send();
            message.MessageSender = web;
            message.Send();
            UserMessage usermsg = new UserMessage();

            usermsg.Subject       = "Test Message";
            usermsg.Body          = "Hi, This is a Test Message";
            usermsg.UserComments  = "I hope you are well";
            usermsg.MessageSender = email;
            usermsg.Send();
            // Wait for user
            Console.ReadKey();

            //5 - Adapter
            Console.WriteLine("Adapter");
            ITarget Itarget = new EmployeeAdapter();
            ThirdPartyBillingSystem client = new ThirdPartyBillingSystem(Itarget);

            client.ShowEmployeeList();
            // Wait for user
            Console.ReadKey();

            //6 - Decorator
            Console.WriteLine("Decorator");
            HondaCity car = new HondaCity();

            Console.WriteLine("Honda City base price are : {0}", car.Price);
            SpecialOffer offer = new SpecialOffer(car);

            offer.DiscountPercentage = 25;
            offer.Offer = "25 % discount";
            Console.WriteLine("{1} @ Diwali Special Offer and price are : {0} ", offer.Price, offer.Offer);
            // Wait for user
            Console.ReadKey();
        }
示例#8
0
    static void Main(string[] args)
    {
        VehiculoFactory honda = new HondaFactory();
        VehiculoClient hondaclient = new VehiculoClient(honda, "Regular");

        Console.WriteLine("******* Honda **********");
        Console.WriteLine(hondaclient.obtenerbiciNombre());
        Console.WriteLine(hondaclient.GetScooterNombre());

        hondaclient = new VehiculoClient(honda, "Deportes");
        Console.WriteLine(hondaclient.obtenerbiciNombre());
        Console.WriteLine(hondaclient.GetScooterNombre());

        VehiculoFactory hero = new HeroFactory();
        VehiculoClient heroclient = new VehiculoClient(hero, "Regular");

        Console.WriteLine("******* Hero **********");
        Console.WriteLine(heroclient.obtenerbiciNombre());
        Console.WriteLine(heroclient.GetScooterNombre());

        heroclient = new VehiculoClient(hero, "Deportes");
        Console.WriteLine(heroclient.obtenerbiciNombre());
        Console.WriteLine(heroclient.GetScooterNombre());

        Console.ReadKey();
    }
        static void Main(string[] args)
        {
            // AUDI
            CarFactory carFactory = new AudiFactory();

            Console.WriteLine();

            var audiCoupe = carFactory.CreateCoupe();

            audiCoupe.GetName();
            audiCoupe.GetMaxSpeed();
            Console.WriteLine();

            var audiLimousine = carFactory.CreateLimousine();

            audiLimousine.GetName();
            audiLimousine.HasAirCon();
            Console.WriteLine();

            var audiSuw = carFactory.CreateSuW();

            audiSuw.GetName();
            audiSuw.SetWheelSize();

            // BMW
            carFactory = new BmwFactory();
            Console.WriteLine();

            var bmwCoupe = carFactory.CreateCoupe();

            bmwCoupe.GetName();
            bmwCoupe.GetMaxSpeed();
            Console.WriteLine();

            var bmwLimousine = carFactory.CreateLimousine();

            bmwLimousine.GetName();
            bmwLimousine.HasAirCon();
            Console.WriteLine();

            var bmwSuw = carFactory.CreateSuW();

            bmwSuw.GetName();
            bmwSuw.SetWheelSize();
            Console.WriteLine();

            // HONDA
            ICarFactory hondaFactory = new HondaFactory();

            Console.WriteLine();

            var hondaCoupe = hondaFactory.CreateCoupe();

            hondaCoupe.GetName();
            hondaCoupe.GetMaxSpeed();
            Console.WriteLine();

            var hondaLimousine = hondaFactory.CreateLimousine();

            hondaLimousine.GetName();
            hondaLimousine.HasAirCon();
            Console.WriteLine();

            var hondaSuw = hondaFactory.CreateSuW();

            hondaSuw.GetName();
            hondaSuw.SetWheelSize();
            Console.WriteLine();

            Console.ReadKey();
        }