zoukankan      html  css  js  c++  java
  • 设计模式学习笔记--享元模式

     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
  • 相关阅读:
    linux下搭建lamp环境以及安装swoole扩展
    TP5 中引入第三方类库
    thinkphp5 查询的数据是对象时,获取原始数据方法
    thinkphp5 列表页数据分页查询3-带搜索条件
    thinkphp5 列表页数据分页查询2-带搜索条件
    thinkphp5 列表页数据分页查询-带搜索条件
    thinkphp5 怎么获取当前的模块,控制器和方法名
    限定页面执行时间,请求超时抛出异常或提示
    centos安装netcat
    redis在PHP中的基本使用案例
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5551719.html
Copyright © 2011-2022 走看看