예제 #1
0
        static void Main(string[] args)
        {
            // The client code picks a concrete strategy and passes it to the
            // context. The client should be aware of the differences between
            // strategies in order to make the right choice.
            CompressionContext ctx = new CompressionContext(new ZipCompressionStrategy());

            ctx.CreateArchive("DotNetDesignPattern");

            ctx.SetStrategy(new RarCompressionStrategy());
            ctx.CreateArchive("DotNetDesignPattern");

            Console.Read();

            #region Method - II

            CompressionContext ctxTwo = null;
            //ctxTwo = new CompressionContext(); //Create the default constructor in the CompressionContext class

            if ("ZipCompression".Equals("ZipCompression", StringComparison.InvariantCultureIgnoreCase))
            {
                ctx.SetStrategy(new ZipCompressionStrategy());
            }
            else if ("RarCompression".Equals("RarCompression", StringComparison.InvariantCultureIgnoreCase))
            {
                ctx.SetStrategy(new RarCompressionStrategy());
            }

            #endregion
        }
예제 #2
0
        public void Test_Compression()
        {
            File file = new File {
                Comporession = CompressionType.None, FileName = "testingCompressionFile"
            };

            ZIPCompression     zipCompression = new ZIPCompression();
            CompressionContext context        = new CompressionContext(zipCompression);

            context.CreateArchive(file);

            Assert.That(file.Comporession, Is.EqualTo(CompressionType.Zip));

            RARCompression rarCompression = new RARCompression();

            context.SetStrategy(rarCompression);

            context.CreateArchive(file);

            Assert.That(file.Comporession, Is.EqualTo(CompressionType.Rar));
        }
예제 #3
0
        public void ArchiveByCompressing()
        {
            var compressionStrategy = new Mock <ICompressionStrategy>();

            compressionStrategy.Setup(c => c.Compress()).Returns("Compression Done Successfully");

            ICompressionContext compressionContext = new CompressionContext(compressionStrategy.Object);

            compressionContext.CreateArchive();

            compressionStrategy.Verify(c => c.Compress());
        }