类图 using System; using System.Collections.Generic; using System.Text; using System.IO; namespace FlyWeightPattern { public class New { private long _ID; private string _Caption; private string _Author; private string _CreateTime; private string _Content; public New(long id,string caption,string author,string createtime,string content) { _ID=id; _Caption = caption; _Author = author; _CreateTime = createtime; _Content = content; FileStream fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "recode.txt", FileMode.Append); StreamWriter fileWriter = new StreamWriter(fileStream,Encoding.UTF8); fileWriter.WriteLine("生成了新闻" + id); fileWriter.Close(); fileStream.Close(); } public long ID{ get{ return _ID; } } public string Caption { get { return _Caption; } } public string Author { get { return _Author; } } public string CreateTime { get { return _CreateTime; } } public string Content { get { return _Content; } } public override string ToString() { System.Text.StringBuilder sb = new StringBuilder(); sb.AppendFormat("ID:{0}", _ID); sb.AppendFormat("Caption:{0}", _Caption); sb.AppendFormat("Author:{0}", _Author); sb.AppendFormat("CreateTime:{0}", _CreateTime); sb.AppendFormat("Content:{0}", _Content); sb.Append("/n"); return sb.ToString(); } } } 待生成的产品 using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace FlyWeightPattern { public class NewsPool { private Hashtable _NewCollection; public NewsPool() { _NewCollection = new Hashtable(); } public bool isExists(long id) { return _NewCollection.ContainsKey(id); } public void addNew(New news) { _NewCollection.Add(news.ID, news); } public void printAllNews() { string result = string.Empty; foreach (long id in _NewCollection.Keys) { New news = _NewCollection[id] as New; result += news.ToString(); } Console.WriteLine(result); } public New getNew(long id) { return _NewCollection[id] as New; } } } 保存已生成产品的产品池 using System; using System.Collections.Generic; using System.Text; namespace FlyWeightPattern { public class CreateNewsFactory { private NewsPool _NewsPool; public CreateNewsFactory(NewsPool newspool) { _NewsPool = newspool; } public New createNews(long id, string caption, string author, string createtime, string content) { New tmpNew = null; if (!_NewsPool.isExists(id)) { tmpNew = new New(id, caption, author, createtime, content); _NewsPool.addNew(tmpNew); } else tmpNew = _NewsPool.getNew(id); return tmpNew; } } } 产品生成工厂 using System; using System.Collections.Generic; using System.Text; namespace FlyWeightPattern { class Program { static void Main(string[] args) { NewsPool newsPool = new NewsPool(); CreateNewsFactory factory = new CreateNewsFactory(newsPool); factory.createNews(1, "caption1", "author1", "createtime1", "content1"); factory.createNews(2, "caption2", "author2", "createtime2", "content2"); factory.createNews(3, "caption3", "author3", "createtime3", "content3"); factory.createNews(4, "caption4", "author4", "createtime4", "content4"); factory.createNews(5, "caption5", "author5", "createtime5", "content5"); factory.createNews(1, "caption1", "author1", "createtime1", "content1"); factory.createNews(2, "caption2", "author2", "createtime2", "content2"); factory.createNews(3, "caption3", "author3", "createtime3", "content3"); factory.createNews(4, "caption4", "author4", "createtime4", "content4"); factory.createNews(5, "caption5", "author5", "createtime5", "content5"); factory.createNews(6, "caption6", "author6", "createtime6", "content6"); newsPool.printAllNews(); Console.ReadKey(); } } } 调用代码