Patterns in SOME –Flyweight

Code in C#:
 
namespace Flyweight_DesignPattern
{
     using System;
     using System.Collections;
 
     class FlyweightFactory
     {
         private ArrayList pool = new ArrayList();
 
         // the flyweightfactory can crete all entries in the pool at startup
         // (if the pool is small, and it is likely all will be used), or as
         // needed, if the pool si large and it is likely some will never be used
         public FlyweightFactory()
         {
              pool.Add(new ConcreteEvenFlyweight());        
              pool.Add(new ConcreteUnevenFlyweight());          
         }
 
         public Flyweight GetFlyweight(int key)
         {
              // here we would determine if the flyweight identified by key
              // exists, and if so return it. If not, we would create it.
              // As in this demo we have implementation all the possible
              // flyweights we wish to use, we retrun the suitable one.
              int i = key % 2;
              return((Flyweight)pool[i]);
         }
     }
 
     abstract class Flyweight
     {
         abstract public void DoOperation(int extrinsicState);       
     }
 
     class UnsharedConcreteFlyweight : Flyweight
     {
         override public void DoOperation(int extrinsicState)
         {
             
         }
     }
 
     class ConcreteEvenFlyweight : Flyweight
     {
         override public void DoOperation(int extrinsicState)
         {
              Console.WriteLine("In ConcreteEvenFlyweight.DoOperation: {0}", extrinsicState);                       
         }
     }
 
     class ConcreteUnevenFlyweight : Flyweight
     {
         override public void DoOperation(int extrinsicState)
         {
              Console.WriteLine("In ConcreteUnevenFlyweight.DoOperation: {0}", extrinsicState);           
         }   
     }
 
     ///<summary>
     ///    Summary description for Client.
     ///</summary>
     public class Client
     {
         public static int Main(string[] args)
         {
              int[] data = {1,2,3,4,5,6,7,8};
             
              FlyweightFactory f = new FlyweightFactory();
             
              int extrinsicState = 3;
              foreach (int i in data)
              {
                   Flyweight flyweight = f.GetFlyweight(i);
                   flyweight.DoOperation(extrinsicState);
              }
             
              return 0;
         }
     }
}
 
 
Code in SOME:
  
CFlyweightFactory -> AFlyweight[][_pool]
       ()
       AFlyweight GetFlyweight(int)
 
AFlyweight
       a_DoOperation(int)
 
CUnsharedConcreteFlyweight : AFlyweight
       o_DoOperation(int)
 
CConcreteEvenFlyweight : AFlyweight
       o_DoOperation(int)
 
CConcreteUnevenFlyweight : AFlyweight
       o_DoOperation(int)
 
CClient
       main
 
 
CClient.main
{
       <% int[] data = {1,2,3,4,5,6,7,8}; %>
      
       CFlyweightFactory f.()
       {
              CConcreteEvenFlyweight f1.();
              CConcreteUnevenFlyweight f2.();
             
              _pool.Add(f1);
              _pool.Add(f2);
       };
                    
       <% int extrinsicState = 3; %>
       <% foreach (int key in data) %>
       <% { %>
                            int key;
                            AFlyweight flyweight = f.GetFlyweight(key)
                            {
                                   <% int i = key % 2; %>
                                   <% return((AFlyweight)_pool[i]); %>               
                            };
                            flyweight.DoOperation(extrinsicState);
       <% }%>
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值