program.cs
来自「GOF23种设计模式详细例子!附有详细的代码噢!」· CS 代码 · 共 58 行
CS
58 行
using System;
using System.Collections;
using System.Text;
using System.IO;
namespace FlyweightExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GC.GetTotalMemory(false));
Random rnd = new Random();
ArrayList al = new ArrayList();
for (int i = 0; i < 10000; i++)
{
string modelName = rnd.Next(2).ToString();
Model model = ModelFactory.GetInstance().GetModel(modelName);
//Model model = new Model(modelName);
al.Add(model);
}
Console.WriteLine(GC.GetTotalMemory(false));
Console.ReadLine();
}
}
class Model
{
private byte[] data;
public Model(string modelName)
{
data = File.ReadAllBytes("c:\\" + modelName + ".txt");
}
}
class ModelFactory
{
private Hashtable modelList = new Hashtable();
private static ModelFactory instance;
public static ModelFactory GetInstance()
{
if (instance == null)
instance = new ModelFactory();
return instance;
}
public Model GetModel(string modelName)
{
Model model = modelList[modelName] as Model;
if (model == null)
modelList.Add(modelName, new Model(modelName));
return model;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?