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

📄 flyweightstructural.cs

📁 使用C#程序将23个常用设计模式进行列表显示
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Collections;

namespace DesignPattern.FlyweightStructural
{
    class FlyweightStructural : AbstractPattern
    {
        public static void Run(TextBox tbInfo)
        {
            s_tbInfo = tbInfo;
            s_tbInfo.Text = "";

            // 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 fu = new UnsharedConcreteFlyweight(); 
            fu.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) 
        { 
            DesignPattern.FormMain.OutputInfo("ConcreteFlyweight: " + extrinsicstate); 
        }
    }
    
    // "UnsharedConcreteFlyweight" 
    class UnsharedConcreteFlyweight : Flyweight 
    {
        public override void Operation(int extrinsicstate) 
        {
            DesignPattern.FormMain.OutputInfo("UnsharedConcreteFlyweight: " + extrinsicstate); 
        }
    }
}

⌨️ 快捷键说明

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