码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式理解(十)结构型——享元(Flyweight)

时间:2016-03-02 19:53:35      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

最后一个结构型,享元。没有太多的项目经验,对这种模式只有一种概念上的理解,就是为了节约内存等资源,把可重用的东西只申请一次,然后处处调用,同时用Hash进行管理。

    直接上图:

 技术分享

代码:

/*********  大话设计模式上的代码 ************/

abstract class Flyweight{
    public abstract void Operation(int extrinsicstate); 
}

class ConcreteFlyweight : Flyweight{
    public override void Operation(int extrinsicstate){
        Console.WriteLine("具体Flyweight : " + extrinsicstate);
    }
}

class UnshareConcreteFlyweight : Flyweight{
    public override void Operation(int extrinsicstate){
        Console.WriteLine("不共享的具体Flyweight : " + extrinsicstate);
    }
}

class FlyweightFactory{
    private Hashtable flyweights = new Hashtable();
    public FlyweightFactory(){
        flyweights.add("X",new ConcreteFlyweight());
        flyweights.add("Y",new ConcreteFlyweight());
    }
    public Flyweight GetFlyweight(string key){
        return ((Flyweight)flyweights[key]);
    }
}

 

设计模式理解(十)结构型——享元(Flyweight)

标签:

原文地址:http://www.cnblogs.com/myjhaha/p/5236010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!