Ejemplo n.º 1
0
        public static void Run()
        {
            Console.WriteLine($"{Environment.NewLine}*** PUBLISH SUBSCRIBE PATTERN ***{Environment.NewLine}");

            // Make a bike shop
            IBikeShop shop = new BikeShop("Mountain Bike Guys");

            BikeCustomer customerOne   = new BikeCustomer(shop, "Tom");
            BikeCustomer customerTwo   = new BikeCustomer(shop, "Dick");
            BikeCustomer customerThree = new BikeCustomer(shop, "Harry");

            shop.NewBikeArrived("Road Runner");
            shop.NewBikeArrived("GT");

            customerOne.UnsubscribeFromNewBikeNotifications();
            customerTwo.UnsubscribeFromNewBikeNotifications();

            shop.NewBikeArrived("Rocket");

            // We don't need to ubsubscribe the last customer (or any of them really)
            // because they have the same scope as the object they subscribe to.
            // Memory leaks only occur when objects the sunscriber object is disposed
            // but's it's subscribtion in the subscribed object last on.
            // This is a design problem and should be solved at design time.
        }
Ejemplo n.º 2
0
        public void BikeShop_NewBikeEventRaised_SubscriberIsCalled()
        {
            // Arrange
            IBikeShop    shop        = new BikeShop("Shop");
            BikeCustomer customerOne = new BikeCustomer(shop, "Customer");

            using (StringWriter writer = new StringWriter())
            {
                // Capture the console output
                Console.SetOut(writer);

                // Act
                shop.NewBikeArrived("Rocket");

                // Assert
                Assert.IsTrue(writer.ToString().Contains("Rocket"));
            }
        }