コード例 #1
0
        static void Main(string[] args)
        {
            AbstractDuckFactory _duckFactory  = new CountingDuckFactory();
            IQuackable          _mallardDuck  = _duckFactory.CreateMallarDuck();
            IQuackable          _readheadDuck = _duckFactory.CreateRedheadDuck();
            IQuackable          _duckCall     = _duckFactory.CreateDuckCall();
            IQuackable          _rubberDuck   = _duckFactory.CreateRubberDuck();
            IQuackable          _goose        = new GooseAdapter(new Goose());

            Console.WriteLine("\nDuck Simulator\n");

            //Стая уток
            DuckFlock _duckFlock = new DuckFlock();

            _duckFlock.Add(_readheadDuck);
            _duckFlock.Add(_rubberDuck);
            _duckFlock.Add(_duckCall);
            _duckFlock.Add(_goose);

            _mallardDuck.Quack();
            _readheadDuck.Quack();
            _duckCall.Quack();
            _rubberDuck.Quack();
            _goose.Quack();

            Console.WriteLine("\nThe ducks quacks " + QuackCounter.GetQuacks() + " times");

            //Вызов стаи
            _duckFlock.Quack();

            Console.ReadLine();
        }
コード例 #2
0
ファイル: DuckSimulator.cs プロジェクト: abakumov-v/patterns
        public void Simulate(AbstractDuckFactory duckFactory, AbstractGooseAdapterFactory gooseAdapterFactory)
        {
            var redheadDuck = duckFactory.CreateRedheadDuck();
            var duckCall    = duckFactory.CreateDuckCall();
            var rubberDuck  = duckFactory.CreateRubberDuck();
            var goose       = gooseAdapterFactory.CreateGooseAdapter();

            var ducksFlock = new DuckFlock();

            ducksFlock.Add(redheadDuck);
            ducksFlock.Add(duckCall);
            ducksFlock.Add(rubberDuck);
            ducksFlock.Add(goose);

            var mallardDuck1      = duckFactory.CreateMallardDuck();
            var mallardDuck2      = duckFactory.CreateMallardDuck();
            var mallardDuck3      = duckFactory.CreateMallardDuck();
            var mallardDuck4      = duckFactory.CreateMallardDuck();
            var mallardDucksFlock = new DuckFlock();

            mallardDucksFlock.Add(mallardDuck1);
            mallardDucksFlock.Add(mallardDuck2);
            mallardDucksFlock.Add(mallardDuck3);
            mallardDucksFlock.Add(mallardDuck4);

            ducksFlock.Add(mallardDucksFlock);

            Console.WriteLine("\n--- Duck simulator ---\n");

//            Console.WriteLine("\n---- Duck simulator: Whole Flock simulation\n");
//            Simulate(ducksFlock);
//
//            Console.WriteLine("\n---- Duck simulator: Whole Flock simulation\n");
//            Simulate(mallardDucksFlock);

//            Console.WriteLine($"The ducks quacked {QuackCounterDecorator.AllQuacksCount} times");

            Console.WriteLine("\n---- Duck simulator: with observer\n");
            var quackologist = new Quackologist();

            ducksFlock.RegisterObserver(quackologist);
            Simulate(ducksFlock);

            Console.WriteLine($"The ducks quacked {QuackCounterDecorator.AllQuacksCount} times");
        }