示例#1
0
        static void Main(string[] args)
        {
            //================FACTORY====================================================
            AnswerFactory yesFactory = AnswerFactory.getFactory(typeOfFactory.yesFactory);
            bool          yesAnswer  = yesFactory.getAnswer();

            Console.WriteLine("YES ANSWER: " + yesAnswer);
            AnswerFactory noFactory = AnswerFactory.getFactory(typeOfFactory.noFactory);
            bool          noAnswer  = noFactory.getAnswer();

            Console.WriteLine("NO ANSWER: " + noAnswer);
            //===========================================================================
            Console.WriteLine();
            Console.WriteLine();
            //================SINGLETON==================================================
            Singleton s1 = Singleton.getInstance();

            Console.WriteLine("Singleton state: " + s1.state);
            Singleton s2 = Singleton.getInstance();

            s2.state = true;
            Console.WriteLine("Singleton state: " + s1.state);
            //===========================================================================
            Console.WriteLine();
            Console.WriteLine();
            //================DECORATOR==================================================
            ConcreteComponent component = new ConcreteComponent();

            component.Operation();
            ConcreteDecorator decorator = new ConcreteDecorator();

            decorator.SetComponent(component);
            decorator.Operation();
            //===========================================================================
            Console.WriteLine();
            Console.WriteLine();
            //================STRATEGY===================================================
            Context context;

            context = new Context(new StrategyA());
            context.ContextInterface();
            context = new Context(new StrategyB());
            context.ContextInterface();
            //===========================================================================
            Console.WriteLine();
            Console.WriteLine();
            //================OBSERVER===================================================
            Observable provider = new Observable();
            Observer   reciever = new Observer();

            reciever.Subscribe(provider);
            provider.sendString();
            //===========================================================================

            Console.ReadLine();
        }
示例#2
0
        private static void invokeSingleton()
        {
            Singleton instanceOne = Singleton.getInstance();
            Singleton instanceTwo = Singleton.getInstance();

            if (instanceOne == instanceTwo)
            {
                Console.WriteLine("The singleton instances are equal");
            }
        }
        static void Main(string[] args)
        {
            Singleton instance1 = Singleton.getInstance();

            Console.WriteLine(instance1.GetHashCode());
            Singleton instance2 = Singleton.getInstance();

            Console.WriteLine(instance2.GetHashCode());
            Console.ReadKey();
        }
示例#4
0
        static void Main(string[] args)
        {
            Console.WriteLine("*********************** Singleton *************************");
            Singleton instance = Singleton.getInstance();

            instance.PrintOutput("This is an implementation of singleton which belongs to the creational design pattern");
            instance.PrintOutput("Only single instance is created throughout the application");

            Console.WriteLine("*********************** Factory *************************");
            IEmployeeType emp;

            FactoryController.chooseType();
            try
            {
                emp = FactoryController.getEmployee(Convert.ToInt32(FactoryController.getEmployeeType().ToString()));
                emp.setEmployeeType();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }