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

Posted bzyzhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式学习笔记--享元模式相关的知识,希望对你有一定的参考价值。

 1 using System;
 2 
 3 namespace FlyWeight
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/2 7:21:28 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// FlyWeight说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
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     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/2 7:23:28 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// ConcreteFlyWeight说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
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     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/2 7:25:00 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// UnsharedConcreteFlyWeight说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
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     /// <summary> 
 7     /// 作者:bzyzhang
 8     /// 时间:2016/6/2 7:25:54 
 9     /// 博客地址:http://www.cnblogs.com/bzyzhang/
10     /// FlyWeightFactory说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
11     /// </summary> 
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

 

以上是关于设计模式学习笔记--享元模式的主要内容,如果未能解决你的问题,请参考以下文章

学习设计模式之享元模式

《Android源代码设计模式解析与实战》读书笔记(二十二)

Java设计模式学习笔记

软件设计模式学习(十五)享元模式

软件设计模式学习(十五)享元模式

设计模式学习08:享元模式