⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 来自DoFactory的设计模式, 由于是本人根据已有代码创建,没有NETOptimized项
💻 CS
字号:
// Flyweight pattern -- Structural example  

using System;
using System.Collections;

namespace DoFactory.GangOfFour.Flyweight.Structural
{
    // MainApp test application 

    class MainApp
    {
        static void Main()
        {
            // Arbitrary extrinsic state 
            int extrinsicstate = 22;

            FlyweightFactory f = new FlyweightFactory();

            // Work with different flyweight instances 
            Flyweight fx = f.GetFlyweight("X");
            fx.Operation(--extrinsicstate);

            Flyweight fy = f.GetFlyweight("Y");
            fy.Operation(--extrinsicstate);

            Flyweight fz = f.GetFlyweight("Z");
            fz.Operation(--extrinsicstate);

            UnsharedConcreteFlyweight uf = new
              UnsharedConcreteFlyweight();

            uf.Operation(--extrinsicstate);

            // Wait for user 
            Console.Read();
        }
    }

    // "FlyweightFactory" 

    class FlyweightFactory
    {
        private Hashtable flyweights = new Hashtable();

        // Constructor 
        public FlyweightFactory()
        {
            flyweights.Add("X", new ConcreteFlyweight());
            flyweights.Add("Y", new ConcreteFlyweight());
            flyweights.Add("Z", new ConcreteFlyweight());
        }

        public Flyweight GetFlyweight(string key)
        {
            return ((Flyweight)flyweights[key]);
        }
    }

    // "Flyweight" 

    abstract class Flyweight
    {
        public abstract void Operation(int extrinsicstate);
    }

    // "ConcreteFlyweight" 

    class ConcreteFlyweight : Flyweight
    {
        public override void Operation(int extrinsicstate)
        {
            Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);
        }
    }

    // "UnsharedConcreteFlyweight" 

    class UnsharedConcreteFlyweight : Flyweight
    {
        public override void Operation(int extrinsicstate)
        {
            Console.WriteLine("UnsharedConcreteFlyweight: " +
              extrinsicstate);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -