Example #1
0
    public static void Main(string[] args)
    {
        // Console.WriteLine ("Hello World");
        ICar car = new ProxyCar(new Driver(16));

        car.DriveCar();

        car = new ProxyCar(new Driver(15));
        car.DriveCar();
    }
Example #2
0
        static void Main(string[] args)
        {
            ICar car = new ProxyCar(new Driver(16));

            car.DriveCar();

            car = new ProxyCar(new Driver(25));
            car.DriveCar();

            Console.ReadKey(true);
        }
Example #3
0
        public void ProxyUsage()
        {
            ICar car = new ProxyCar(new Driver(16));

            car.DriveCar();

            car = new ProxyCar(new Driver(25));
            car.DriveCar();

            Assert.IsTrue(true);
        }
Example #4
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Attempting to drive car without any age check...");

            Car car = new Car();

            car.DriveCar();

            Console.WriteLine("Attempting to drive proxy car as a 16 year old...");

            Driver driver = new Driver(16);

            ProxyCar proxyCar = new ProxyCar(driver);

            proxyCar.DriveCar();

            Console.ReadLine();
        }
Example #5
0
        public void AdultShouldCanDriveCar()
        {
            var car = new ProxyCar(new Driver(25));

            Assert.Equal("Car has been driven!", car.DriveCar());
        }
Example #6
0
        public void ChildrenShouldCannotDriveCar()
        {
            var car = new ProxyCar(new Driver(15));

            Assert.Equal("Sorry, the driver is too young to drive.", car.DriveCar());
        }
        static void Main(string[] args)
        {
            #region singleton
            var conexao1 = Conexao.GetInstance();
            var conexao2 = Conexao.GetInstance();

            Console.WriteLine(conexao1.Equals(conexao2) ? "Conexões iguais" : "Conexões diferentes");
            #endregion

            #region polimorfismo com interface
            var matrix     = new Matrix();
            var crepusculo = new Crepusculo();

            var cinema = new Cinema();
            cinema.EntrarNoCinema(matrix);
            cinema.EntrarNoCinema(crepusculo);
            #endregion

            #region Factory method

            #region exemplo com enum

            //exemplo do factory com mau uso de enum,
            //pois a cada moto nova será incrementado o enum, ferindo o princípio de aberto/fechado

            var fabricaMoto = new FabricaMoto();
            var cg          = fabricaMoto.FabricarMoto(EMotos.CG150);
            var f800        = fabricaMoto.FabricarMoto(EMotos.F800);

            Console.WriteLine(cg.ExibirModelo());
            Console.WriteLine(f800.ExibirModelo());

            #endregion

            #region exemplo sem uso de enum

            Fabrica[] fabricas = new Fabrica[4];
            fabricas[0] = new FabricaCG150();
            fabricas[1] = new FabricaF800();
            fabricas[2] = new FabricaNinja250R();
            fabricas[3] = new FabricaYBR();

            foreach (var fabricador in fabricas)
            {
                var moto = fabricador.FabricarMoto();
                Console.WriteLine("Criado {0}", moto.ExibirModelo());
            }

            #endregion

            #endregion

            #region Facade
            var game = new SistemasFacade();
            game.InicializarSubSistemas();
            game.ReproduzirAudio("AC/DC-Back_in_Black.mp3");

            var venda = new VendaFacade();
            venda.FecharPedido();
            #endregion

            #region Proxy
            IImagem imagem = new ImagemProxy("c:/teste.png");
            imagem.MostrarImagem();

            var Car = new ProxyCar(new Driver(26));
            Car.DriveCar();

            var motorista = new MotoristaProxy(18);
            motorista.Dirigir();
            #endregion

            #region State
            try
            {
                IOrdemServicoState os = new Elaboracao();
                var agendamento       = os.Agendar();
                var execucao          = agendamento.Executar();
                var finalizacao       = execucao.Finalizar();
                finalizacao.Finalizar();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion

            #region strategy

            var localizacaoEnsaioPM = new EnsaioPlanoManutencaoContext(new LocalizacaoEnsaioPlanoManutencao()).GetEnsaios();
            var equipmentoEnsaioPM  = new EnsaioPlanoManutencaoContext(new EquipamentoEnsaioPlanoManutencao()).GetEnsaios();
            var familiaEnsaioPM     = new EnsaioPlanoManutencaoContext(new FamiliaEnsaioPlanoManutencao()).GetEnsaios();

            new GeradorBoletoContext(new GeradorBoletoBB()).Gerar();
            new GeradorBoletoContext(new GeradorBoletoBradesco()).Gerar();
            new GeradorBoletoContext(new GeradorBoletoItau()).Gerar();
            new GeradorBoletoContext(new GeradorBoletoCaixa()).Gerar();

            #endregion
        }
Example #8
0
        static void Main(string[] args)
        {
            #region Singleton
            //Console.WriteLine("***Singleton Pattern Demo***");
            //Console.WriteLine("Trying to create instance s1.");
            //Singleton s1 = Singleton.Instance;
            //Singleton s2 = Singleton.Instance;
            //if (s1 == s2)
            //{
            //    Console.WriteLine("Only on instance exists.");

            //}
            //else
            //{
            //    Console.WriteLine("Different instances exist.");
            //}
            #endregion

            #region Prototype
            //Console.WriteLine("***Prototype Pattern Demo***\n");
            //BasicCar nano_base = new Nano("Green Nano") { Price = 100000 };
            //BasicCar ford_base = new Ford("Ford Yellow") { Price = 500000 };

            //BasicCar bc1;
            ////Nano
            //bc1 = nano_base.Clone();
            //bc1.Price = nano_base.Price + BasicCar.SetPrice();
            //Console.WriteLine($"Car is:{bc1.ModelName},and it's price is Rs.{bc1.Price}");
            ////Ford
            //bc1 = ford_base.Clone();
            //bc1.Price = ford_base.Price + BasicCar.SetPrice();
            //Console.WriteLine($"Car is:{bc1.ModelName},and it's price is Rs.{bc1.Price}");
            #endregion

            #region BuiderPattern
            //Console.WriteLine("***Builder Pattern Demo***\n");
            //Director director = new Director();
            //IBuilder b1 = new Car("Ford");

            //director.Construct(b1);
            //Product p1 = b1.GetVehicle();
            //p1.Show();
            #endregion

            #region FactoryMethodPattern
            Console.WriteLine("***Factory Pattern Demo***\n");

            //Creating a Tiger Factory
            DesignPattern.FactoryMethodPattern.IAnimalFactory tigerFactory = new TigerFactory();
            //Createing a tiger using the factory method
            IAnimal aTiger = tigerFactory.MakeAnimal();
            //aTiger.Speak();
            //aTiger.Action();

            FactoryMethodPattern.IAnimalFactory dogFactory = new DogFactory();
            IAnimal aDog = dogFactory.CreateAnimal();
            aDog.Speak();
            aDog.Action();
            #endregion

            #region Simple Factory
            //Console.WriteLine("*** Simple Factory Pattern Demo***\n");
            //IAnimal preferredType = null;
            //ISimpleFactory simpleFactory = new SimpleFactory();
            //preferredType = simpleFactory.CreateAnimal();
            //preferredType.Speak();
            //preferredType.Action();
            #endregion

            #region AbstractFactory Pattern
            Console.WriteLine("***Abstract Factory Pattern Demo***");
            DesignPattern.AbstractFactoryPattern.IAnimalFactory wildAnimalFactory = new WildAnimalFactory();
            IDog wildDog = wildAnimalFactory.GetDog();
            wildDog.Speak();
            wildDog.Action();
            ITiger wildTiger = wildAnimalFactory.GetTiger();
            wildTiger.Speak();
            wildTiger.Action();
            Console.WriteLine("below is create pet animal ");
            AbstractFactoryPattern.IAnimalFactory petAnimalFactory = new PetAnimalFactory();
            IDog petDog = petAnimalFactory.GetDog();
            petDog.Speak();
            petDog.Action();
            ITiger petTiger = petAnimalFactory.GetTiger();
            petTiger.Speak();
            petTiger.Action();
            #endregion

            #region Proxy Pattern
            Console.WriteLine("***Proxy Pattern Demo***\n");
            Proxy px = new Proxy();
            px.DoSomeWork();

            ICar car = new ProxyCar(new Driver(15));
            car.DriveCar();
            car = new ProxyCar(new Driver(25));
            car.DriveCar();
            #endregion

            Console.WriteLine("***Decorator pattern Demo...\n");
            ConcreteComponent cc = new ConcreteComponent();


            ConcreteDecoratorEx1 decoratorEx1 = new ConcreteDecoratorEx1(cc);
            //  decoratorEx1.SetTheComponent(cc);
            decoratorEx1.MakeHouse();

            #region Adapter Pattern
            CalculatorAdapter cal = new CalculatorAdapter();
            Triangle          t   = new Triangle(20, 10);
            Console.WriteLine($"Area of Triangle is:{cal.GetArea(t)} Square unit");


            Rect r = new Rect(20, 10);
            Console.WriteLine($"Area of Rectangle is :{r.CalculateAreaOfRectangle()}");
            Triangle t0 = new Triangle(20, 10);
            Console.WriteLine($"Area of Triangle is :{t0.CalculateAreaOfTriangle()} Square unit.");
            RectInterface adapter = new TriangleAdapter(t0);
            Console.WriteLine($"Area of Triangle using the triangle adapter is :{adapter.CalculateAreaOfRectangle()} square unit");
            #endregion

            Console.WriteLine("***Flyweight Pattern Demo***");
            RobotFactory factory = new RobotFactory();
            IRobot       shape   = factory.GetRobotFromFactory("Small");
            shape.Print();
            for (int i = 0; i < 2; i++)
            {
                shape = factory.GetRobotFromFactory("Small");
                shape.Print();
            }
            int NumOfDistinctRobots = factory.TotalObjectsCreated;
            Console.WriteLine($"Now,there has {NumOfDistinctRobots} Robots.");

            Console.WriteLine("***Composite Pattern Demo***");
            CompositeEmployee Principal = new CompositeEmployee("Derily(Principal)", "Planning-Supervising-Managing");
            CompositeEmployee hodMaths  = new CompositeEmployee("Mrs.S.Das(HOD-Maths)", "Maths");
            CompositeEmployee hodCompSc = new CompositeEmployee("Mr. V.Sarcar(HOD-CSE)", "Computer Sc.");

            Employee mathTeacher1 = new Employee("Math Teacher-1", "Maths");
            Employee mathTeacher2 = new Employee("Math Teacher-2", "Maths");

            Employee cseTeacher1 = new Employee("CSE Teacher-1", "Computer Sc.");
            Employee cseTeacher2 = new Employee("CSE Teacher-2", "Computer Sc.");
            Employee cseTeacher3 = new Employee("CSE Teacher-3", "Computer Sc.");

            hodMaths.Add(mathTeacher1);
            hodMaths.Add(mathTeacher2);

            hodCompSc.Add(cseTeacher1);
            hodCompSc.Add(cseTeacher2);
            hodCompSc.Add(cseTeacher3);

            Principal.Add(hodMaths);
            Principal.Add(hodCompSc);

            Console.WriteLine("\n Testing the structure of a Principal object");
            Principal.PrintStructures();

            Console.Read();
        }