Exemplo n.º 1
0
        static void Main( string[] args )
        {
            /*
             * Factory Method Pattern
             */
            Creator c = new Creator( );
            IProduct product;
            for ( int i = 1; i <= 12; i++ )
            {
                product = c.FactoryMethod(i);
                Console.WriteLine("Product " + product.ShipFrom( ));
            }
            /*
             * Factory Method Ends
             */

            /*
             * Observer pattern
             */
            Subject subject = new Subject( );
            Observer.Observer observer1 = new Observer.Observer("Observer 1");
            subject.Subscribe(observer1);

            Observer.Observer observer2 = new Observer.Observer("observer 2");
            subject.Subscribe(observer2);

            subject.Inventory++;
            subject.Unsubscribe(observer2);

            subject.Subscribe(new Observer.Observer("observer 3"));
            subject.Inventory++;
        }
Exemplo n.º 2
0
        /// <summary>
        /// DEFINITION
        /// - Software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, 
        /// and notifies them automatically of any state changes, ussually calling one of their methods.
        /// USAGE
        /// -When you need many other objects to recieve an update when another objecy changes
        /// -Publisher Subject sends many data to Observers
        /// -Observers Subscribers take the data they want and use it.
        /// ADVANTAGES
        /// -Loose coupling
        /// -Publisher doen't need to know anything about the Subscribers
        /// DISADVANTAGES
        /// -Publisher may send updates that don't matter to the subscriber
        /// </summary>
        private static void executeObserverDesignPattern()
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("OBSERVER PATTERN \n");
            Console.ForegroundColor = ConsoleColor.White;

            IPublisher publisher = new Subject()
            {
                imbStock = 8000,
                mcsoftStock = 6500,
                nasStock = 5760
            };

            ISubscriber dikeledi = new Observer(publisher);
            ISubscriber papa = new Observer(publisher);

            Console.WriteLine("\n Published to {0} observers \n", publisher.publish());

            ((Subject)publisher).imbStock = 1000.02;

            ISubscriber madillo = new Observer(publisher);
            ISubscriber nozy = new Observer(publisher);
            publisher.unregisterObserver(dikeledi);

            Console.WriteLine("\n Published to {0} observers \n", publisher.publish());

            Console.WriteLine("------------------- \n");
        }
Exemplo n.º 3
0
 public void Subscribe(Observer observer)
 {
     observers.Add(observer);
 }
Exemplo n.º 4
0
 public void Unsubscribe(Observer observer)
 {
     observers.Remove(observer);
 }
Exemplo n.º 5
0
 public void Attach(Observer observer)
 {
     _observers.Add(observer);
 }
Exemplo n.º 6
0
 public void Deattach(Observer observer)
 {
     _observers.Remove(observer);
 }
Exemplo n.º 7
0
 public void RemoveObserver(Observer observer)
 {
     _observers.Remove(observer);
 }
Exemplo n.º 8
0
 public void RemoverObserver(Observer o)
 {
     observers.Remove(o);
 }
Exemplo n.º 9
0
 public void RegisterObserver(Observer o)
 {
     observers.Add(o);
 }
Exemplo n.º 10
0
 public void RemoverObserver(Observer o)
 {
     observers.Remove(o);
 }
Exemplo n.º 11
0
 public void RegisterObserver(Observer o)
 {
     observers.Add(o);
 }