public void FactoryMethod_CheckInstance()
        {
            var fm   = new ConcreteFactory();
            var prod = fm.ProductFactoryMethod();

            Assert.IsFalse(prod as ConcreteProduct == null);
        }
        public void IsMySqlDbConnection()
        {
            var factory    = new ConcreteFactory("MySQL");
            var connection = factory.FactoryMethod();

            Assert.IsInstanceOf <MySqlDbConnection>(connection);
        }
        public void IsOracleDbConnection()
        {
            var factory    = new ConcreteFactory("Oracle");
            var connection = factory.FactoryMethod();

            Assert.IsInstanceOf <OracleDbConnection>(connection);
        }
        static void Main(string[] args)
        {
            string          linha;
            ConcreteFactory factory = new ConcreteFactory();

            Console.WriteLine("Digite OK para sair");
            do
            {
                Console.Write("Digite uma opção AloMundo(alo) ou Connectar(con): ");
                linha = Console.ReadLine();

                if (linha == "alo")
                {
                    Console.Write("Digite uma lingua(en/sp/de): ");
                    linha = Console.ReadLine();
                    IAloMundo lingua = factory.CriaAloMundo(linha);
                    lingua.falaAlo();

                    IConnection conexao = factory.CriaConnection("my");
                    conexao.FunctionB(lingua);
                    Console.WriteLine(conexao.conectar());
                }
                else
                {
                    Console.Write("Digite uma conexão(my/or/ss): ");
                    linha = Console.ReadLine();
                    IConnection conexao = factory.CriaConnection(linha);
                    Console.WriteLine(conexao.conectar());
                }
            } while (linha != "OK" && linha != "alo" && linha != "con");

            Console.ReadKey();
        }
        public void IsSqlServerDbConnection()
        {
            var factory    = new ConcreteFactory("SqlServer");
            var connection = factory.FactoryMethod();

            Assert.IsInstanceOf <SqlServerDbConnection>(connection);
        }
Exemple #6
0
        public static void ExecuteNonQueryStoredProcedure(string CommandString, DbParameter[] parameters)
        {
            ConcreteFactory factory = new ConcreteFactory();
            var             DAL     = factory.GetDataAccessLayer((DataProviderType)Enum.Parse(typeof(DataProviderType), DbType), ConnectionString);
            IDbCommand      cmd     = DAL.GeDataProviderCommand();

            try
            {
                if (parameters != null)
                {
                    foreach (DbParameter param in parameters)
                    {
                        cmd.Parameters.Add(param);
                    }
                }
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = CommandString;
                cmd.Connection  = DAL.OpenConnection();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                factory = null;
                DAL     = null;
                cmd.Dispose();
            }
        }
Exemple #7
0
        public static DataTable ExecuteText(string CommandString)
        {
            DataSet         ds      = new DataSet();
            ConcreteFactory factory = new ConcreteFactory();
            var             DAL     = factory.GetDataAccessLayer((DataProviderType)Enum.Parse(typeof(DataProviderType), DbType), ConnectionString);
            IDbCommand      cmd     = DAL.GeDataProviderCommand();
            IDbDataAdapter  adapter = DAL.GetDataProviderDataAdapter();

            try
            {
                cmd.CommandType       = CommandType.Text;
                cmd.CommandText       = CommandString;
                cmd.Connection        = DAL.OpenConnection();
                adapter.SelectCommand = cmd;
                adapter.Fill(ds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                factory = null;
                DAL     = null;
                cmd.Dispose();
                adapter = null;
                ds.Dispose();
            }
            return(ds.Tables[0]);
        }
Exemple #8
0
        static void Main(string[] args)
        {
            var exceptionMessage = string.Empty;

            try
            {
                throw new System.ArgumentException("Parameter cannot be null", "original");
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    AbstractFactory factory            = new ConcreteFactory();
                    Client          clientElmahLogging = new Client(factory, ex);
                    clientElmahLogging.RunElmahLogging(ex);

                    Client clientFileStreamLogging = new Client(factory, ex);
                    clientFileStreamLogging.RunFileStreamLogging(ex);

                    Client clientFileLogging = new Client(factory, ex);
                    clientFileLogging.RunFileLogging(ex);

                    Client clientEventViewerLogging = new Client(factory, ex);
                    clientEventViewerLogging.RunEventViewerLogging(ex);
                }
            }
            Console.WriteLine("Press any key to close the application");
            Console.ReadKey();
        }
Exemple #9
0
        public void Test()
        {
            IAbstractFactory factory  = new ConcreteFactory(typeof(ProductA1), typeof(ProductB1));
            IProductA        productA = factory.CreateProducctA();
            IProductB        productB = factory.CreateProducctB();

            Assert.AreEqual <Type>(typeof(ProductA1), productA.GetType());
            Assert.AreEqual <Type>(typeof(ProductB1), productB.GetType());
        }
Exemple #10
0
        static void Main()
        {
            var factory = new ConcreteFactory();
            var client  = new Client.Client(factory);

            client.DoWork();
            // delay
            Console.Read();
        }
Exemple #11
0
        public void ShowSample()
        {
            IAbstractFactory factory = new ConcreteFactory();

            var product1 = factory.CreateProductA();
            var product2 = factory.CreateProductB();

            Console.WriteLine($"{nameof(IAbstractFactory)} {nameof(ConcreteFactory)} creates {nameof(IProductA)} {factory.CreateProductA().GetType().Name}");
            Console.WriteLine($"{nameof(IAbstractFactory)} {nameof(ConcreteFactory)} creates {nameof(IProductB)} {factory.CreateProductB().GetType().Name}");
        }
Exemple #12
0
        public void Test()
        {
            IFactoryWithNotifier factory   = new ConcreteFactory();
            Subscribe            subcriber = new Subscribe();

            Assert.IsNull(subcriber.Product);
            factory.Create((x) => { subcriber.Product = x; });
            Assert.IsNotNull(subcriber.Product);
            Assert.IsTrue(subcriber.Product is ConcreteProduct);
        }
        public void RegisterTest()
        {
            ConcreteFactory<int, Stream> factory =
                new ConcreteFactory<int, Stream>();

            factory.Register<MemoryStream>(0);

            Assert.AreEqual(factory.Create(0).GetType(), typeof(MemoryStream));
            Assert.AreEqual(factory.Create(2), null);
        }
        public void UnregisterTest()
        {
            ConcreteFactory <int, Stream> factory =
                new ConcreteFactory <int, Stream>();

            factory.Register <MemoryStream>(0);
            factory.Unregister <MemoryStream>();

            Assert.AreEqual(factory.Create(0), null);
        }
        public void RegisterTest()
        {
            ConcreteFactory <int, Stream> factory =
                new ConcreteFactory <int, Stream>();

            factory.Register <MemoryStream>(0);

            Assert.AreEqual(factory.Create(0).GetType(), typeof(MemoryStream));
            Assert.AreEqual(factory.Create(2), null);
        }
        public void Test1()
        {
            IFactoryWithNotifier           factory   = new ConcreteFactory();
            Subscribe                      subscribe = new Subscribe();
            ObjectCreateHandler <IProduct> callback  = new ObjectCreateHandler <IProduct>(subscribe.SetProduct);

            Assert.IsNull(subscribe.GetProduct());
            factory.Create(callback);
            Assert.IsNotNull(subscribe.GetProduct());
        }
Exemple #17
0
        public void AbstractFactoryWithMapperTest()
        {
            IDictionary <Type, Type> dictionary = new Dictionary <Type, Type>();

            dictionary.Add(typeof(IProductA), typeof(ProductA1));
            dictionary.Add(typeof(IProductB), typeof(ProductB1));

            IAbstractFactoryWithMapper factory = new ConcreteFactory(dictionary);
            IProductA productA = factory.Create <IProductA>();
            IProductB productB = factory.Create <IProductB>();

            Assert.AreEqual(typeof(ProductA1), productA.GetType());
            Assert.AreEqual(typeof(ProductB1), productB.GetType());
        }
        static void ExampleFurniture()
        {
            ConcreteFactory concreteFactory = new ConcreteFactory();

            Console.WriteLine("Select (1) for sofa or (2) for coffe table");
            int option    = int.Parse(Console.ReadLine());
            int newOption = 0;

            switch (option)
            {
            case 1:
                Console.WriteLine("Select (1) for modern sofa or (2) for victorian sofa");
                newOption = int.Parse(Console.ReadLine());
                if (newOption == 1)
                {
                    ClientSofaProduct(concreteFactory.CreateModernSofa());
                }
                else
                {
                    ClientSofaProduct(concreteFactory.CreateVictorianSofa());
                }
                break;

            case 2:
                Console.WriteLine("Select (1) for modern coffe table or (2) for victorian coffe table");
                newOption = int.Parse(Console.ReadLine());
                if (newOption == 1)
                {
                    ClientCoffeTableProduct(concreteFactory.CreateModerCoffeTable());
                }
                else
                {
                    ClientCoffeTableProduct(concreteFactory.CreateVictorianCoffeTable());
                }
                break;

            default:
                Console.WriteLine("Goodbye!!!");
                break;
            }
        }
Exemple #19
0
        public PlotModel CreatePlotModel()
        {
            string title = "Bike thefts a month";
              var plotModel = new PlotModel
              {
            Title = title,
            TitleFontSize = 24,
            LegendFontSize = 24,
            LegendPlacement = LegendPlacement.Inside,
            LegendPosition = LegendPosition.RightTop,
            LegendOrientation = LegendOrientation.Horizontal,
            LegendBorderThickness = 0

              };
              IFactory factory = new ConcreteFactory();
              var linearAxis = factory.CreateLinearAxisBasic("Total amount");
              var categoryAxis = factory.CreateCategoryAxisMonths();

              var series1 = new LineSeries
              {
            MarkerType = MarkerType.Circle,
            MarkerSize = 4,
            MarkerStroke = OxyColors.White,
            Title = "total"
              };

              Dictionary<int, int> fd = preLoad.csvFD.getLinechart();
              foreach (KeyValuePair<int, int> item in fd)
              {
            series1.Points.Add(new DataPoint(item.Key - 1.5, item.Value));
              }

              plotModel.Series.Add(series1);
              plotModel.Axes.Add(categoryAxis);
              plotModel.Axes.Add(linearAxis);

              return plotModel;
        }
Exemple #20
0
        public static DataTable ExecuteStoredProcedure(string CommandString, DbParameter[] parameters)
        {
            DataSet         ds      = new DataSet();
            ConcreteFactory factory = new ConcreteFactory();
            var             DAL     = factory.GetDataAccessLayer((DataProviderType)Enum.Parse(typeof(DataProviderType), DbType), ConnectionString);
            IDbCommand      cmd     = DAL.GeDataProviderCommand();
            IDbDataAdapter  adapter = DAL.GetDataProviderDataAdapter();

            try
            {
                if (parameters != null)
                {
                    foreach (DbParameter param in parameters)
                    {
                        cmd.Parameters.Add(param);
                    }
                }
                cmd.CommandType       = CommandType.StoredProcedure;
                cmd.CommandText       = CommandString;
                cmd.Connection        = DAL.OpenConnection();
                adapter.SelectCommand = cmd;
                adapter.Fill(ds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                factory = null;
                DAL     = null;
                cmd.Dispose();
                adapter = null;
                ds.Dispose();
            }
            return(ds.Tables[0]);
        }
Exemple #21
0
        public static void ExecuteNoneQueryText(string CommandString)
        {
            ConcreteFactory factory = new ConcreteFactory();
            var             DAL     = factory.GetDataAccessLayer((DataProviderType)Enum.Parse(typeof(DataProviderType), DbType), ConnectionString);
            IDbCommand      cmd     = DAL.GeDataProviderCommand();

            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = CommandString;
                cmd.Connection  = DAL.OpenConnection();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                factory = null;
                DAL     = null;
                cmd.Dispose();
            }
        }
Exemple #22
0
        private static Dictionary <Type, List <IPredictionStrategy> > BuildInputData()
        {
            IAbstractFactory abstractFactory = new ConcreteFactory();
            var dictionary = new Dictionary <Type, List <IPredictionStrategy> >();

            var irisToPredict = new List <IPredictionStrategy> {
                new FlowerTypePredictionStrategy(abstractFactory.CreateInputData(3.3f, 1.6f, 0.2f, 5.1f)),
                new FlowerTypePredictionStrategy(abstractFactory.CreateInputData(0.2f, 0.5f, 0.2f, 0.5f)),
                new FlowerTypePredictionStrategy(abstractFactory.CreateInputData(1.3f, 0.6f, 0.4f, 1.0f))
            };

            dictionary.Add(typeof(FlowerTypeData), irisToPredict);

            var adequacyLevelsToPredict = new List <IPredictionStrategy> {
                new AdequacyLevelPredictionStrategy(abstractFactory.CreateInputData(2.1f, 3.2f, 2.2f, 210.00f, 64f, 110f)),
                new AdequacyLevelPredictionStrategy(abstractFactory.CreateInputData(1f, 4.9f, 2f, 100f, 98.0f, 40f)),
                new AdequacyLevelPredictionStrategy(abstractFactory.CreateInputData(0.4f, 1.9f, 0f, 40.00f, 38f, 0f)),
                new AdequacyLevelPredictionStrategy(abstractFactory.CreateInputData(0.3f, 4.2f, 1.2f, 30f, 84.0f, 24f)),
                new AdequacyLevelPredictionStrategy(abstractFactory.CreateInputData(0.9f, 4.8f, 1.8f, 90.00f, 96f, 36f))
            };

            dictionary.Add(typeof(AdequacyLevelData), adequacyLevelsToPredict);
            return(dictionary);
        }
        public void UnregisterTest()
        {
            ConcreteFactory<int, Stream> factory =
                new ConcreteFactory<int, Stream>();

            factory.Register<MemoryStream>(0);
            factory.Unregister<MemoryStream>();

            Assert.AreEqual(factory.Create(0), null);
        }
        public void ConcreteFactoryTest()
        {
            var concreteFactory = new ConcreteFactory();

            Assert.Equal(typeof(ConcreteProduct), concreteFactory.MakeProduct().GetType());
        }
        private static void Main()
        {
            // Facade.
            {
                Console.WriteLine("----    Testing Facade pattern    ----");
                var swimmingPoodSystemsFacade = new SwimmingpoolFacade();
                swimmingPoodSystemsFacade.GoToASwimmingPool();
                Console.WriteLine("----    Testing Facade pattern finished    ----");
                Console.WriteLine(".. press any key to continue testing ..");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Mediator
            {
                Console.WriteLine("----    Testing Mediator pattern    ----");

                var brain = new BrainMediator();
                var ear   = new Ear(brain);
                var face  = new Face(brain);

                brain.Ear  = ear;
                brain.Face = face;

                ear.HearSounds();

                Console.WriteLine("----    Testing Mediator pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Singleton
            {
                Console.WriteLine("----    Testing Singleton pattern    ----");

                var loggerSingleton = LoggerSingleton.GetInstance();
                loggerSingleton.TestId = 7;

                var loggerSingleton2 = LoggerSingleton.GetInstance();

                Console.WriteLine(loggerSingleton2.ToString());

                Console.WriteLine("----    Testing Singleton pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Builder
            {
                Console.WriteLine("----    Testing Builder pattern    ----");

                var gamingBuilder    = new GamingLaptopBuilder();
                var travelingBuilder = new TravelingLaptopBuilder();
                var director         = new BuilderDirector(travelingBuilder);

                var laptop = director.ConstructLaptop();

                Console.WriteLine(laptop.ToString());

                director.ChangeBuilderTo(gamingBuilder);

                var laptop2 = director.ConstructLaptop();

                Console.WriteLine(laptop2.ToString());

                Console.WriteLine("----    Testing Builder pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Factory Method
            {
                Console.WriteLine("----    Testing Factory Method pattern    ----");

                Console.WriteLine(ConcreteFactory.CreateProduct(ProductType.Type2));

                Console.WriteLine("----    Testing Factory Method pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Abstract Factory
            {
                Console.WriteLine("----    Testing Abstract Factory pattern    ----");

                AbstractToysFactory factory = new TeddyToyFactory();
                var createdToy = factory.CreateToy();
                Console.WriteLine(createdToy.ToString());

                Console.WriteLine("----    Testing Abstract Factory pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Prototype
            {
                Console.WriteLine("----    Testing Prototype pattern    ----");

                var prototype = ProtorypeDirector.GetFilledPrototype();
                prototype.Parameter2 = prototype.Parameter2 + " changed!";
                Console.WriteLine(prototype.ToString());

                Console.WriteLine("----    Testing Prototype pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Strategy
            {
                Console.WriteLine("----    Testing Strategy pattern    ----");

                var strategyOne = new StrategyOne();
                var strategyTwo = new StrategyTwo();

                StrategyClient.DoJobUsingSomeStrategy(strategyOne, "data parameter");
                StrategyClient.DoJobUsingSomeStrategy(strategyTwo, "data parameter");

                Console.WriteLine("----    Testing Strategy pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Template Method
            {
                Console.WriteLine("----    Testing Template Method pattern    ----");

                var templateMethodImplementationClassObject = new ConcreteClassWithTemplateMethodUsing();
                templateMethodImplementationClassObject.DoSomeTemplateMethodJob();

                Console.WriteLine("----    Testing Template Method pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Iterator
            {
                Console.WriteLine("----    Testing Iterator pattern    ----");

                var iterator = new EnumerableDataSource(new[] { "one", "two" });

                foreach (var item in iterator)
                {
                    Console.WriteLine(item);
                }

                Console.WriteLine("----    Testing Iterator pattern finished    ----");
                Console.ReadKey();
            }
        }
Exemple #26
0
 public void Run()
 {
     AbstractFactory factory = new ConcreteFactory();
     factory.Calculator.Calculate();
     factory.Logger.LogInfo("Logger prints it");
 }
 public PocAbstractFactory()
 {
     concreteFactory = new ConcreteFactory();
 }