当前位置:编程学习 > C/C++ >>

大话设计模式之享元模式

  面向对象的思想很好地解决了抽象性的问题,一般也不会出现性能上的问题。但是在某些情况下,对象的数量可能会太多,从而导致了运行时的代价。那么我们如何去避免大量细粒度的对象,同时又不影响客户程序使用面向对象的方式进行操作?
 
享元模式:运用共享技术有效的支持大量细粒度的对象
 
结构图:
 
解释:
 
FlyweightFactory:一个享元工厂,用来创建并管理Flyweight对象,它主要是用来确保合理的共享Flyweight,当用户请求一个Flyweight时,FlyweightFactory对象提供一个已创建的实例或者创建一个(如果不存在的话)
 
Flyweight:所有具体享元类的超类或接口,通过这个接口,Flyweight尅接受并作用于外部状态
 
ConcreteFlyweight:继承Flyweight超累或实现Flyweight接口,并为内部状态增加储存空间
 
UnsharedConcreteFlyweight:指那些不需要共享的Flyweight子类,因为Flyweight接口共享成为可能,但它并不强制共享
 
代码实现:
 
[html]  
<span style="font-size:18px;">using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Collections;  
  
namespace 享元模式  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            //代码外部状态  
            int extrinsicstate = 22;  
  
            FlyweightFactory f = new FlyweightFactory();  
  
            Flyweight fx = f.GetFlyweight("X");  
            fx.Operation(--extrinsicstate);  
  
            Flyweight fy = f.GetFlyweight("Y");  
            fy.Operation(--extrinsicstate);  
  
            Flyweight fz = f.GetFlyweight("Z");  
            fz.Operation(--extrinsicstate);  
  
            Flyweight uf = new UnsharedConcreteFlyweight();  
  
            uf.Operation(--extrinsicstate);  
  
            Console.Read();  
  
  
        }  
    }  
  
//Flyweight 类,它是所有具体享元类的超类或接口  
    abstract class Flyweight  
    {  
        public abstract void Operation(int extrinsicstate);  
    }  
  
    //ConcreteFlyweight类,继承Flyweight超类或实现Flyweight接口,并为内部状态增加存储空间  
    class ConcreteFlyweight : Flyweight  
    {  www.zzzyk.com
        public override void Operation(int extrinstate)  
        {  
            Console.WriteLine("具体Flyweight:" + extrinstate);  
  
        }  
  
    }  
  
    //UnsharedConcreteFlyweight类,代表那些不需要共享的Flyweight子类  
    class UnsharedConcreteFlyweight : Flyweight  
    {  
        public override void Operation(int extrinsicstate)  
        {  
            Console.WriteLine("不共享的具体Flyweight:" + extrinsicstate);  
        }  
    }  
  
    //FlyweightFactory类,是一个享元工厂,用来创建并管理Flyweight对象  
    class FlyweightFactory  
    {  
  
        //使用Hashtable需要调用using System.Collections;  
        private Hashtable flyweights = new Hashtable();  
  
        public FlyweightFactory()  
        {  
            flyweights.Add("X", new ConcreteFlyweight());  
            flyweights.Add("Y", new ConcreteFlyweight());  
            flyweights.Add("Z", new ConcreteFlyweight());  
  
        }  
  
        public Flyweight GetFlyweight(string key)  
        {  
            return ((Flyweight)flyweights[key]);  
        }  
  
    }  
  
  
  
}  
</span>  
 
例子:网站共享
 
结构图:
 
代码实现:
[html]  
<span style="font-size:18px;">using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Collections;  
  
namespace 网站共享代码  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            WebSiteFactory f=new WebSiteFactory ();  
  
            WebSite fx=f .GetWebSiteCategory ("产品展示");  
            fx .Use (new User ("王会来"));  
  
            WebSite fy=f.GetWebSiteCategory ("产品展示");  
            fy .Use (new User ("陈金荣"));  
  
            WebSite fz=f.GetWebSiteCategory ("产品展示");  
            fz.Use (new User ("韩学敏"));  
  
            WebSite fl=f.GetWebSiteCategory ("博客");  
            fl .Use (new User ("李双喆"));  
  
            WebSite fm=f .GetWebSiteC
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,