简体   繁体   中英

AbstractFactory Pattern not disposing of all objects in c#

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    
    namespace DoFactory.GangOfFour.Abstract.Structural
    {
    
        /// <summary>
        /// The 'AbstractFactory' abstract class
        /// </summary>
    
        abstract class AbstractFactory : IDisposable
        {
            private bool disposedValue = false; // To detect redundant calls
            public AbstractFactory() =>
            Console.WriteLine("simulation to allocate AbstractFactory");
            public abstract AbstractProductA CreateProductA();
            public abstract AbstractProductB CreateProductB();
            
            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for AbstractFactory");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~AbstractFactory()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    
    
        /// <summary>
        /// The 'ConcreteFactory1' class
        /// </summary>
    
        class ConcreteFactory1 : AbstractFactory, IDisposable
        {
            public ConcreteFactory1() =>
            Console.WriteLine("simulation to allocate ConcreteFactory1");
    
            private bool disposedValue = false; // To detect redundant calls
    
            public override AbstractProductA CreateProductA()
            {
                return new ProductA1();
            }
            public override AbstractProductB CreateProductB()
            {
                return new ProductB1();
            }
            
            protected override void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for ConcreteFactory1");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~ConcreteFactory1()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public new void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    
        /// <summary>
        /// The 'ConcreteFactory2' class
        /// </summary>
    
        class ConcreteFactory2 : AbstractFactory, IDisposable
        {
            public ConcreteFactory2() =>
            Console.WriteLine("simulation to allocate ConcreteFactory2");
    
            private bool disposedValue = false; // To detect redundant calls
    
            public override AbstractProductA CreateProductA()
            {
                return new ProductA2();
            }
            public override AbstractProductB CreateProductB()
            {
                return new ProductB2();
            }
    
            protected override void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for ConcreteFactory2");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~ ConcreteFactory2()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public new void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    
        /// <summary>
        /// The 'AbstractProductA' abstract class
        /// </summary>
    
        abstract class AbstractProductA : IDisposable
        {
            private bool disposedValue = false; // To detect redundant calls
            public AbstractProductA() =>
            Console.WriteLine("simulation to allocate AbstractProductA");
            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for AbstractProductA");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~AbstractProductA()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    
        /// <summary>
        /// The 'AbstractProductB' abstract class
        /// </summary>
    
        abstract class AbstractProductB : IDisposable
        {
            private bool disposedValue = false; // To detect redundant calls
    
            public abstract void Interact(AbstractProductA a);
    
            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for AbstractProductB");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~AbstractProductB()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    
    
        /// <summary>
        /// The 'ProductA1' class
        /// </summary>
    
        class ProductA1 : AbstractProductA, IDisposable
        {
            private bool disposedValue = false; // To detect redundant calls
            public ProductA1() =>
            Console.WriteLine("simulation to allocate ProductA1");
            
            protected override void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for ProductA1");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~ProductA1()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public new void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    
        /// <summary>
        /// The 'ProductB1' class
        /// </summary>
    
        class ProductB1 : AbstractProductB, IDisposable
        {
            private bool disposedValue = false; // To detect redundant calls
            public ProductB1() =>
            Console.WriteLine("simulation to allocate ProductB1");
    
            
            public override void Interact(AbstractProductA a)
            {
                Console.WriteLine(this.GetType().Name +
                  " interacts with " + a.GetType().Name);
            }
    
            protected override void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for ProductB1");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~ProductB1()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public new void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    
        /// <summary>
        /// The 'ProductA2' class
        /// </summary>
    
        class ProductA2 : AbstractProductA, IDisposable
        {
            private bool disposedValue = false; // To detect redundant calls
            public ProductA2() =>
            Console.WriteLine("simulation to allocate ProductA2");
    
            
            protected override void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for ProductA2");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~ProductA2()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public new void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    
        /// <summary>
        /// The 'ProductB2' class
        /// </summary>
    
        class ProductB2 : AbstractProductB, IDisposable
        {
            private bool disposedValue = false; // To detect redundant calls
            public ProductB2() =>
            Console.WriteLine("simulation to allocate ProductB2");
    
            
            public override void Interact(AbstractProductA a)
            {
                Console.WriteLine(this.GetType().Name +
                  " interacts with " + a.GetType().Name);
            }
    
            protected override void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for ProductB2");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~ProductB2()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public new void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    
        /// <summary>
        /// The 'Client' class. Interaction environment for the products.
        /// </summary>
    
        class Client : IDisposable
        {
            
            private AbstractProductA _abstractProductA;
            private AbstractProductB _abstractProductB;
            private bool disposedValue = false; // To detect redundant calls
    
            // Constructor
    
            public Client(AbstractFactory factory)
            {
                Console.WriteLine("simulation to allocate Client");
                _abstractProductB = factory.CreateProductB();
                _abstractProductA = factory.CreateProductA();
            }
    
            public void Run()
            {
                _abstractProductB.Interact(_abstractProductA);
            }
    
            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects)
                    }
                    Console.WriteLine("simulation to release native memory for Client");
                    // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                    // TODO: set large fields to null
                    disposedValue = true;
                }
            }
    
            // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
            ~Client()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: false);
            }
    
            public void Dispose()
            {
                // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    }

using DoFactory.GangOfFour.Abstract.Structural;
using System;

namespace DesignPatterns
{
    class Program
    {
        static void Main(string[] args)
        {
            // Abstract factory #1
            using (AbstractFactory factory1 = new ConcreteFactory1())
            {
                using (Client client1 = new Client(factory1))
                {
                    client1.Run();
                }
            }
            // Abstract factory #1
            using (AbstractFactory factory2 = new ConcreteFactory2())
            {
                using (Client client2 = new Client(factory2))
                {
                    client2.Run();
                }
            }

            Console.ReadKey();
        }
    }
}


The Following is the output:-

    simulation to allocate AbstractFactory
    simulation to allocate ConcreteFactory1
    simulation to allocate Client
    simulation to allocate ProductB1
    simulation to allocate AbstractProductA
    simulation to allocate ProductA1
    ProductB1 interacts with ProductA1
    simulation to release native memory for Client
    simulation to release native memory for ConcreteFactory1
    simulation to allocate AbstractFactory
    simulation to allocate ConcreteFactory2
    simulation to allocate Client
    simulation to allocate ProductB2
    simulation to allocate AbstractProductA
    simulation to allocate ProductA2
    ProductB2 interacts with ProductA2
    simulation to release native memory for Client
    simulation to release native memory for ConcreteFactory2

As you are overriding the Dispose(bool disposing) method of the AbstractFactory in your derived classes that code will no longer run.

What you could do is move that logic to a non-virtual/private method called DisposeBase or something, and declare Dispose(bool disposing) as abstract rather than just virtual, which will require derivers to implement it.

Then call both Disposal methods in the AbstractFactory implementation of IDisposable .

This will also mean you will no-longer need to implement IDisposable on the derived classes.

using System;
using System.Collections.Generic;
using System.Text;


namespace DoFactory.GangOfFour.Abstract.Structural
{
    

    abstract class AbstractFactory : IDisposable
    {
        private bool disposedValue = false; // To detect redundant calls
        public AbstractFactory() =>
        Console.WriteLine("simulation to allocate AbstractFactory");
        public abstract AbstractProductA CreateProductA();
        public abstract AbstractProductB CreateProductB();
        
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                }
                Console.WriteLine("simulation to release native memory for AbstractFactory");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~AbstractFactory()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }


    /// <summary>
    /// The 'ConcreteFactory1' class
    /// </summary>

    class ConcreteFactory1 : AbstractFactory, IDisposable
    {
        public ConcreteFactory1() =>
        Console.WriteLine("simulation to allocate ConcreteFactory1");

        private bool disposedValue = false; // To detect redundant calls

        public override AbstractProductA CreateProductA()
        {
            return new ProductA1();
        }
        public override AbstractProductB CreateProductB()
        {
            return new ProductB1();
        }
        
        protected override void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                    base.Dispose(true);                    
                }
                Console.WriteLine("simulation to release native memory for ConcreteFactory1");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~ConcreteFactory1()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public new void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }

    /// <summary>
    /// The 'ConcreteFactory2' class
    /// </summary>

    class ConcreteFactory2 : AbstractFactory, IDisposable
    {
        public ConcreteFactory2() =>
        Console.WriteLine("simulation to allocate ConcreteFactory2");

        private bool disposedValue = false; // To detect redundant calls

        public override AbstractProductA CreateProductA()
        {
            return new ProductA2();
        }
        public override AbstractProductB CreateProductB()
        {
            return new ProductB2();
        }

        protected override void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                    base.Dispose(true);
                }
                Console.WriteLine("simulation to release native memory for ConcreteFactory2");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~ ConcreteFactory2()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public new void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }

    /// <summary>
    /// The 'AbstractProductA' abstract class
    /// </summary>

    abstract class AbstractProductA : IDisposable
    {
        private bool disposedValue = false; // To detect redundant calls
        public AbstractProductA() =>
        Console.WriteLine("simulation to allocate AbstractProductA");
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                }
                Console.WriteLine("simulation to release native memory for AbstractProductA");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~AbstractProductA()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }

    /// <summary>
    /// The 'AbstractProductB' abstract class
    /// </summary>

    abstract class AbstractProductB : IDisposable
    {
        private bool disposedValue = false; // To detect redundant calls

        public abstract void Interact(AbstractProductA a);

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                }
                Console.WriteLine("simulation to release native memory for AbstractProductB");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~AbstractProductB()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }


    /// <summary>
    /// The 'ProductA1' class
    /// </summary>

    class ProductA1 : AbstractProductA, IDisposable
    {
        private bool disposedValue = false; // To detect redundant calls
        public ProductA1() =>
        Console.WriteLine("simulation to allocate ProductA1");
        
        protected override void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                    base.Dispose(true);                    
                }
                Console.WriteLine("simulation to release native memory for ProductA1");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~ProductA1()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public new void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }

    /// <summary>
    /// The 'ProductB1' class
    /// </summary>

    class ProductB1 : AbstractProductB, IDisposable
    {
        private bool disposedValue = false; // To detect redundant calls
        public ProductB1() =>
        Console.WriteLine("simulation to allocate ProductB1");

        
        public override void Interact(AbstractProductA a)
        {
            Console.WriteLine(this.GetType().Name +
              " interacts with " + a.GetType().Name);
        }

        protected override void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                    base.Dispose(true);                    
                }
                Console.WriteLine("simulation to release native memory for ProductB1");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~ProductB1()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public new void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }

    /// <summary>
    /// The 'ProductA2' class
    /// </summary>

    class ProductA2 : AbstractProductA, IDisposable
    {
        private bool disposedValue = false; // To detect redundant calls
        public ProductA2() =>
        Console.WriteLine("simulation to allocate ProductA2");

        
        protected override void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                    base.Dispose(true);
                    
                }
                Console.WriteLine("simulation to release native memory for ProductA2");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~ProductA2()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public new void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }

    /// <summary>
    /// The 'ProductB2' class
    /// </summary>

    class ProductB2 : AbstractProductB, IDisposable
    {
        private bool disposedValue = false; // To detect redundant calls
        public ProductB2() =>
        Console.WriteLine("simulation to allocate ProductB2");

        
        public override void Interact(AbstractProductA a)
        {
            Console.WriteLine(this.GetType().Name +
              " interacts with " + a.GetType().Name);
        }

        protected override void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                    base.Dispose(true);
                    
                }
                Console.WriteLine("simulation to release native memory for ProductB2");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~ProductB2()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public new void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }

    /// <summary>
    /// The 'Client' class. Interaction environment for the products.
    /// </summary>

    class Client : IDisposable
    {
        
        private AbstractProductA _abstractProductA;
        private AbstractProductB _abstractProductB;
        private bool disposedValue = false; // To detect redundant calls

        // Constructor

        public Client(AbstractFactory factory)
        {
            Console.WriteLine("simulation to allocate Client");
            _abstractProductB = factory.CreateProductB();
            _abstractProductA = factory.CreateProductA();
        }

        public void Run()
        {
            _abstractProductB.Interact(_abstractProductA);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                    _abstractProductA.Dispose();
                    _abstractProductB.Dispose();
                }
                Console.WriteLine("simulation to release native memory for Client");
                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~Client()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }
}

The above Code Works. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM