Exemplo n.º 1
0
        public void Ex1()
        {
            var collection = new WordsCollection();

            collection.AddItem("First");
            collection.AddItem("Second");
            collection.AddItem("Third");

            Console.WriteLine("Straight traversal:");

            // WordsCollection måste ha metoden "IEnumerator GetEnumerator()" för att kunna göra foreach
            foreach (var element in collection)
            {
                Console.WriteLine(element);
            }

            Console.WriteLine("\nReverse traversal:");

            // Inget sker här förutom att kollektionen vet att vi ändrar riktning
            collection.ReverseDirection();

            foreach (var element in collection)
            {
                Console.WriteLine(element);
            }
        }
Exemplo n.º 2
0
        public Client()
        {
            // The client code may or may not know about the Concrete Iterator
            // or Collection classes, depending on the level of indirection you
            // want to keep in your program.
            var collection = new WordsCollection();

            collection.AddItem("First");
            collection.AddItem("Second");
            collection.AddItem("Third");

            Console.WriteLine("Straight traversal:");

            foreach (var element in collection)
            {
                Console.WriteLine(element);
            }

            Console.WriteLine("\nReverse traversal:");

            collection.ReverseDirection();

            foreach (var element in collection)
            {
                Console.WriteLine(element);
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            // Singleton getting instance
            Singleton.Singleton s = Singleton.Singleton.getInstance();

            // Factory method
            Factory.TransportFactory tovarna = new Factory.TransportFactory();
            Factory.ITransport       auto    = tovarna.Vytvor(255);
            Factory.ITransport       lod     = tovarna.Vytvor(250, "Ship");
            auto.Deliver();
            lod.Deliver();

            // Object pool
            ObjectPool.ObjectPool pool       = new ObjectPool.ObjectPool();
            SqlConnection         connection = pool.GetConnection();

            // work with connection
            pool.ReturnConnection(connection);

            // Iterator
            var collection = new Iterator.WordsCollection();

            collection.AddItem("A");
            collection.AddItem("B");
            collection.AddItem("C");
            foreach (var item in collection)
            {
                Console.WriteLine(item);
            }
            // reverse
            collection.reverseDirection();
            foreach (var item in collection)
            {
                Console.WriteLine(item);
            }

            // Command
            List <Command.ICommand> commands = new List <Command.ICommand>();

            commands.Add(new Command.PrintCommand("Prvni radek"));
            commands.Add(new Command.NewLineCommand());
            commands.Add(new Command.PrintCommand("Radek po newLine"));
            foreach (var command in commands)
            {
                command.Execute();
            }

            // Strategy
            Strategy.Cashdesk cashdesk = new Strategy.Cashdesk();
            cashdesk.AddDrink(45);
            cashdesk.AddDrink(25);
            cashdesk.AddDrink(15);
            Console.WriteLine("Classic price :" + cashdesk.CountPrice());
            Strategy.IStrategy happy = new Strategy.HappyHourStrategy();
            cashdesk.ChangeStrategy(happy);
            Console.WriteLine("Happy hour price :" + cashdesk.CountPrice());

            //Decorator
            Decorator.Notifier stack = new Decorator.Notifier();
            stack = new Decorator.WhatsAppDecorator(stack);
            stack = new Decorator.SlackDecorator(stack);
            stack.SendMessage("Zprava");

            // Flyweight
            string document = "AAA";

            char[] chars = document.ToCharArray();
            Flyweight.CharacterFactory factory = new Flyweight.CharacterFactory();
            int pointSize = 10;

            foreach (char c in chars)
            {
                pointSize++;
                Flyweight.Character character = factory.GetCharacter(c);
                character.Display(pointSize);
            }

            // Proxy
            Proxy.RealDatabase  database = new Proxy.RealDatabase();
            Proxy.ProxyDatabase proxy    = new Proxy.ProxyDatabase(database);
            proxy.Request();
        }