Пример #1
0
        public void TestMethod()
        {
            var image = samplePrototype.Clone();

            Assert.IsNull(samplePrototype.Name);
            Assert.IsNull(image.Name);//副本与样本当时的内容一致

            samplePrototype.Name = "A";
            Trace.WriteLine("samplePrototype.Name: " + samplePrototype.Name);
            image = samplePrototype.Clone();

            Assert.AreEqual("A", image.Name);
            Trace.WriteLine("image.Name: " + image.Name);//副本与样本当时的内容一致

            Assert.IsInstanceOfType(image, typeof(ConcretePrototype));

            image.Name = "B";//独立修改副本的内容
            Trace.WriteLine("image.Name: " + image.Name);

            //理论上string类型Name是引用类型,但是由于该引用类型的特殊性(无论是实际还是语义),
            //Object.MemberwiseClone方法仍旧为其创建了副本。也就是说,在浅拷贝过程,
            //我们应该将字符串看成是值类型。

            Assert.IsTrue(samplePrototype.Name != image.Name);//证明副本与样本是两个独立的个体
            Trace.WriteLine(samplePrototype.Name != image.Name);
        }
        public void Test()
        {
            sample.Name = "A";
            IPrototype image = sample.Clone();

            Assert.AreEqual <string>("A", image.Name);                          // 副本与样本当时的内容一致
            Assert.AreEqual <Type>(typeof(ConcretePrototype), image.GetType()); //具体类型
            image.Name = "B";                                                   // 独立修改副本的内容
            Assert.IsTrue(sample.Name != image.Name);                           // 证明是两个独立的个体
        }
Пример #3
0
        public void Test()
        {
            var mirrorImage = sample.Clone();

            Assert.AreEqual <int>(sample.Signal.GetHashCode(), mirrorImage.Signal.GetHashCode());
            Assert.AreEqual <string>(mirrorImage.Name, sample.Name);

            sample.Name = "A";
            Assert.AreNotEqual <string>("A", mirrorImage.Name);
            mirrorImage = sample.Clone();
            Assert.AreEqual <string>("A", mirrorImage.Name);
            Assert.IsInstanceOfType(mirrorImage, typeof(ConcretePrototype));
        }
        public void Test()
        {
            var image = sample.Clone();

            Assert.IsNull(sample.Name);                 //  副本与样本当时的内容一致
            Assert.IsNull(image.Name);

            sample.Name = "A";                          //  修改样本内容
            image       = sample.Clone();
            Assert.AreEqual <string>("A", image.Name);  // 副本与样本当时的内容一致
            Assert.IsInstanceOfType(image, typeof(ConcretePrototype));
            image.Name = "B";                           // 独立修改副本的内容
            Assert.IsTrue(sample.Name != image.Name);   // 证明副本与样本两个独立的个体
        }
        public void Test()
        {
            var image = sample.Clone();

            Assert.IsNull(sample.Name);
            Assert.IsNull(image.Name);

            sample.Name = "A";
            image       = sample.Clone();
            Assert.AreEqual <string>("A", image.Name);
            Assert.IsInstanceOfType(image, typeof(ConcretePrototype));
            image.Name = "B";
            Assert.IsTrue(sample.Name != image.Name);
        }
        public IComponent <T> Share(T set, IComponent <T> toHere)
        {
            IPrototype <IComponent <T> > prototype = this.Find(set) as IPrototype <IComponent <T> >;
            IComponent <T> copy = prototype.Clone() as IComponent <T>;

            toHere.Add(copy);
            return(toHere);
        }
Пример #7
0
        public IPrototype CreateInstance(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            IPrototype masterInstance = null;

            if (masterInstances.TryGetValue(name, out masterInstance))
            {
                return(masterInstance.Clone());
            }
            return(null);
        }
Пример #8
0
        public IPrototype[] CreateInstances(string name, int numInstances)
        {
            if (numInstances <= 0)
            {
                return(null);
            }

            IPrototype masterInstance = null;

            if (masterInstances.TryGetValue(name, out masterInstance))
            {
                IPrototype[] instances = new IPrototype[numInstances];
                for (int i = 0; i < numInstances; i++)
                {
                    instances[i] = masterInstance.Clone();
                }
                return(instances);
            }
            return(null);
        }
Пример #9
0
        static void Main(string[] args)
        {
            SystemConsole.WriteLine("Hello World!");

            foreach (IFactory factory in Factories)
            {
                IFactoryObject factoryObject = factory.MakeObject();
                factoryObject.PrintSelf();
            }

            IBuiltObject builtObject1 =
                new Builder(_logger)
                .Start()
                .Annoying()
                .Loud()
                .WithColor(ConsoleColor.Green)
                .Build();

            IBuiltObject builtObject2 =
                new Builder(_logger)
                .Start()
                .Quiet()
                .WithColor(ConsoleColor.Yellow)
                .Build();

            builtObject1.PrintSelf();
            builtObject2.PrintSelf();

            foreach (IFactoryMethod factoryMethod in FactoryMethods)
            {
                IFactoryObject factoryObject = factoryMethod.CreateObject();
                factoryObject.PrintSelf();
            }

            IPrototype prototype = Prototype.InitialPrototype.Clone();

            for (int i = 0; i < 10; ++i)
            {
                SystemConsole.WriteLine($"prototype #{prototype.Id} copied");
                prototype = prototype.Clone();
            }

            Singleton singleton = Singleton.Instance;

            SystemConsole.WriteLine($"Singleton created at {singleton.CreationDateTime}");

            DivisionResult <int> divisionResult = _multiplyDivideAdapter.Divide(10, 3);

            SystemConsole.WriteLine($"{nameof(MultiplyDivideAdapter)}: 10 / 3 = {divisionResult.WholePart} with remainder {divisionResult.Remainder}");

            int multiplicationResult = _multiplyDivideAdapter.Multiply(10, 3);

            SystemConsole.WriteLine($"{nameof(MultiplyDivideAdapter)}: 10 x 3 = {multiplicationResult}");

            _logger.Log("Turn on hot");
            _hotWaterPipeBridge.TurnOn();
            _logger.Log("Turn off hot");
            _hotWaterPipeBridge.TurnOff();
            _logger.Log("Sprinkling hot");
            _hotWaterPipeBridge.Sprinkle();

            _logger.Log("Turn on cold");
            _coldWaterPipeBridge.TurnOn();
            _logger.Log("Turn off cold");
            _coldWaterPipeBridge.TurnOff();
            _logger.Log("Sprinkling cold");
            _coldWaterPipeBridge.Sprinkle();

            _TraverseCompositeTree(_composite);

            _logger.Log($"It seems we have a gift! First let's unwrap the {_wrapper.GetType().Name}");
            GiftBox giftBox = (_wrapper as WrappingPaper).Unwrap();

            _logger.Log($"We have the {giftBox.GetType().Name} open, now let's take our gift out.");
            Gift gift = giftBox.Open();

            _logger.Log($"The gift is a {gift.Name}!");

            _logger.Log($"Random divided numbers from facade: {_facade.GetRandomNumbersAndDivideThem()}");
            _logger.Log($"Random multiplied numbers from facade: {_facade.GetRandomNumbersAndMultiplyThem()}");

            SystemConsole.ReadLine();
        }