设计模式学习笔记--享元模式

 1 using System;
 2 
 3 namespace FlyWeight
 4 {
 5     ///  
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/2 7:21:28 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// FlyWeight说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     ///  
11     public abstract class FlyWeight
12     {
13         public abstract void Operation(int extrinsticstate);
14     }
15 }
View Code
 1 using System;
 2 
 3 namespace FlyWeight
 4 {
 5     ///  
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/2 7:23:28 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// ConcreteFlyWeight说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     ///  
11     public class ConcreteFlyWeight : FlyWeight
12     {
13         public override void Operation(int extrinsticstate)
14         {
15             Console.WriteLine("具体FlyWeight:" + extrinsticstate);
16         }
17     }
18 }
View Code
 1 using System;
 2 
 3 namespace FlyWeight
 4 {
 5     ///  
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/2 7:25:00 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// UnsharedConcreteFlyWeight说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     ///  
11     public class UnsharedConcreteFlyWeight:FlyWeight
12     {
13         public override void Operation(int extrinsticstate)
14         {
15             Console.WriteLine("不共享的具体FlyWeight:" + extrinsticstate);
16         }
17     }
18 }
View Code
 1 using System;
 2 using System.Collections;
 3 
 4 namespace FlyWeight
 5 {
 6     ///  
 7     /// 作者:bzyzhang
 8     /// 时间:2016/6/2 7:25:54 
 9     /// 博客地址:http://www.cnblogs.com/bzyzhang/
10     /// FlyWeightFactory说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
11     ///  
12     public class FlyWeightFactory
13     {
14         private Hashtable flyWeights = new Hashtable();
15 
16         public FlyWeightFactory()
17         {
18             flyWeights.Add("X",new ConcreteFlyWeight());
19             flyWeights.Add("Y", new ConcreteFlyWeight());
20             flyWeights.Add("Z", new ConcreteFlyWeight());
21         }
22 
23         public FlyWeight GetFlyWeight(string key)
24         {
25             return (FlyWeight)flyWeights[key];
26         }
27     }
28 }
View Code
 1 namespace FlyWeight
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             int extrinsicstate = 22;
 8 
 9             FlyWeightFactory flyWeightFactory = new FlyWeightFactory();
10 
11             FlyWeight flyWeightX = flyWeightFactory.GetFlyWeight("X");
12             flyWeightX.Operation(--extrinsicstate);
13 
14             FlyWeight flyWeightY = flyWeightFactory.GetFlyWeight("Y");
15             flyWeightY.Operation(--extrinsicstate);
16 
17             FlyWeight flyWeightZ = flyWeightFactory.GetFlyWeight("Z");
18             flyWeightZ.Operation(--extrinsicstate);
19 
20             FlyWeight uf = new UnsharedConcreteFlyWeight();
21             uf.Operation(--extrinsicstate);
22         }
23     }
24 }
View Code

 

你可能感兴趣的:(设计模式学习笔记--享元模式)