享元模式

代码
 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 
 5 public abstract class FlyWeight
 6 {
 7     public abstract void opetation();
 8 }
 9 
10 public class ConcreteFlyweight:FlyWeight
11 {
12     private string str;
13     public ConcreteFlyweight(string str)
14     {
15         this.str=str;
16     }
17     
18     public override void opetation()
19     {
20         Console.WriteLine("Concrete---Flyweight:"+str);
21     }
22 }
23 
24 public class FlyWeightFactory
25 {
26     private Hashtable flyweights=new Hashtable();
27     public FlyWeightFactory()
28     {
29     
30     }
31     public FlyWeight getFlyWeight(object obj)
32     {
33         FlyWeight flyweight=(FlyWeight)flyweights[obj];
34         if(flyweight==null)
35         {
36             flyweight=new ConcreteFlyweight(obj.ToString());
37             flyweights.Add(obj,flyweight);
38         }
39         return flyweight;
40     }
41     
42     public int getFlyweightSize()
43     {
44         return flyweights.Count;
45     }
46 }
47 
48 public class FlyweightPattern
49 {
50     FlyWeightFactory factory=new FlyWeightFactory();
51     FlyWeight fly1;
52     FlyWeight fly2;
53     FlyWeight fly3;
54     FlyWeight fly4;
55     FlyWeight fly5;
56     FlyWeight fly6;
57     FlyWeight fly7;
58     FlyWeight fly8;
59     
60     public FlyweightPattern()
61     {
62         fly1=factory.getFlyWeight("MikeSoft");
63         fly2=factory.getFlyWeight("MikeChang");
64         fly3=factory.getFlyWeight("MikeSoft");
65         fly4=factory.getFlyWeight("MikeSoft");
66         fly5=factory.getFlyWeight("MikeSoft");
67         fly6=factory.getFlyWeight("MikeSoft");
68         fly7=factory.getFlyWeight("MikeSoft");
69         fly8=factory.getFlyWeight("MikeSoft");
70     }
71     
72     public void showFlyweight()
73     {
74         fly1.opetation();
75         fly2.opetation();
76         fly3.opetation();
77         fly4.opetation();
78         fly5.opetation();
79         fly6.opetation();
80         fly7.opetation();
81         fly8.opetation();
82         
83         int objSize=factory.getFlyweightSize();
84         Console.WriteLine(objSize);
85     }
86 }
87 
88 public class MyClass
89 {
90     public static void Main()
91     {
92         Console.WriteLine("The FlyWeight Pattern!!");
93         FlyweightPattern fp=new FlyweightPattern();
94         fp.showFlyweight();
95         Console.ReadLine();
96     }
97 }


代码
using System;
using System.Collections;
using System.Collections.Generic;

class FlyweightFactory
{
    
private Hashtable flywights=new Hashtable();
    
public FlyweightFactory()
    {
        flywights.Add(
"X",new Contest1());
        flywights.Add(
"Y",new Contest1());
        flywights.Add(
"Z",new Contest1());
    }
    
    
public Flyweight getFlyweight(string key)
    {
        
if(!flywights.Contains(key))
        {
            flywights.Add(key,
new UnsharedContest());
        }
        
return ((Flyweight)flywights[key]);
    }
}

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

public class Contest1:Flyweight
{
    
public override void Operation(int index)
    {
        Console.WriteLine(
"具体Flyweight:"+index);
    }
}

public class UnsharedContest:Flyweight
{
    
public override void Operation(int index)
    {
        Console.WriteLine(
"不共享的具体Flyweight:"+index);
    }
}


public class MyClass
{
    
public static void Main()
    {
        
int index=22;
        FlyweightFactory f
=new FlyweightFactory();
        
        Flyweight myweight
=f.getFlyweight("X");
        myweight.Operation(
--index);
        
        Flyweight myweight1
=f.getFlyweight("M");
        myweight1.Operation(
--index);
        Console.ReadLine();
    }
}

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

public abstract class FlyWeight
{
    public abstract void opetation();
}

public class ConcreteFlyweight:FlyWeight
{
    private string str;
    public ConcreteFlyweight(string str)
    {
        this.str=str;
    }
    
    public override void opetation()
    {
        Console.WriteLine("Concrete---Flyweight:"+str);
    }
}

public class FlyWeightFactory
{
    private Hashtable flyweights=new Hashtable();
    public FlyWeightFactory()
    {
    
    }
    public FlyWeight getFlyWeight(object obj)
    {
        FlyWeight flyweight=(FlyWeight)flyweights[obj];
        if(flyweight==null)
        {
            flyweight=new ConcreteFlyweight(obj.ToString());
            flyweights.Add(obj,flyweight);
        }
        return flyweight;
    }
    
    public int getFlyweightSize()
    {
        return flyweights.Count;
    }
}

public class FlyweightPattern
{
    FlyWeightFactory factory=new FlyWeightFactory();
    FlyWeight fly1;
    FlyWeight fly2;
    FlyWeight fly3;
    FlyWeight fly4;
    FlyWeight fly5;
    FlyWeight fly6;
    FlyWeight fly7;
    FlyWeight fly8;
    
    public FlyweightPattern()
    {
        fly1=factory.getFlyWeight("MikeSoft");
        fly2=factory.getFlyWeight("MikeChang");
        fly3=factory.getFlyWeight("MikeSoft");
        fly4=factory.getFlyWeight("MikeSoft");
        fly5=factory.getFlyWeight("MikeSoft");
        fly6=factory.getFlyWeight("MikeSoft");
        fly7=factory.getFlyWeight("MikeSoft");
        fly8=factory.getFlyWeight("MikeSoft");
    }
    
    public void showFlyweight()
    {
        fly1.opetation();
        fly2.opetation();
        fly3.opetation();
        fly4.opetation();
        fly5.opetation();
        fly6.opetation();
        fly7.opetation();
        fly8.opetation();
        
        int objSize=factory.getFlyweightSize();
        Console.WriteLine(objSize);
    }
}

public class MyClass
{
    public static void Main()
    {
        Console.WriteLine("The FlyWeight Pattern!!");
        FlyweightPattern fp=new FlyweightPattern();
        fp.showFlyweight();
        Console.ReadLine();
    }
}

原文地址:https://www.cnblogs.com/mikechang/p/1707003.html