DOTA版设计模式——享元

Dota中地图有那么多的树,为什么没有拖慢游戏速度那,在此就要讲讲享元模式了。
享元模式将类的粒度进一步细化,FlyWeight的意思是最轻量级选手,也称蝇量模式,对于大量数量的对象来说,类将内蕴状态和外蕴状态分开,内蕴只含最少的数据,剩下的交给外蕴。保证多对象内存消耗不会过大。
UML图:

测试代码:
DotaPatternLibrary.Flyweight.Trees trees = new DotaPatternLibrary.Flyweight.Trees();
            trees.Display();
            GetMemery();
            DotaPatternLibrary.Flyweight.NoFlyweightTrees noFlyweightTrees = new DotaPatternLibrary.Flyweight.NoFlyweightTrees();
            noFlyweightTrees.Display();
            GetMemery();
       public void GetMemery()
        {
            PerformanceCounter myMemory = new PerformanceCounter();
            myMemory.CategoryName = "Memory";
            myMemory.CounterName = "Available KBytes";

            string txtResult = "-->当前可用内存:" + myMemory.NextValue().ToString() + "KB";
            Graphics graphics = LandpyForm.Form.GetGraphics();
            graphics.DrawString(DateTime.Now.ToLongTimeString() + txtResult, new Font("宋体", 10f), new SolidBrush(Color.Red), 0, 0);
        }
完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;

using DotaCommon;

namespace DotaPatternLibrary.Flyweight
{
    
internal class Tree
    {
        
private int _x;
        
private int _y;
        
private int _size;
        
private Color _treeColor;

        
public int X
        {
            
get { return _x; }
            
set { _x = value; }
        }

        
public int Y
        {
            
get { return _y; }
            
set { _y = value; }
        }

        
public int Size
        {
            
get { return _size; }
            
set { _size = value; }
        }

        
public Color TreeColor
        {
            
get { return _treeColor; }
            
set { _treeColor = value; }
        }

        
public Tree(int x, int y, int size, Color treeColor)
        {
            
this._x = x;
            
this._y = y;
            
this._size = size;
            
this._treeColor = treeColor;
        }

        
public void Display()
        {
            Graphics graphic 
= LandpyForm.Form.GetGraphics();
            graphic.DrawEllipse(
new Pen(new SolidBrush(this._treeColor)), _x, _y, _size, _size);
        }
    }

    
public class NoFlyweightTrees
    {
        
private Hashtable trees = new Hashtable();

        
public NoFlyweightTrees()
        {
            
for (int i = 0; i < 20000; i++)
            {
                Random random 
= new Random();
                
int x = random.Next(0, LandpyForm.Form.PicWidth);
                
int y = random.Next(0, LandpyForm.Form.PicHeight);
                
int size = random.Next(36);
                trees.Add(
"CasuarinaTree" + i, new Tree(x, y, size, Color.Brown));
                x 
= random.Next(0, LandpyForm.Form.PicWidth);
                y 
= random.Next(0, LandpyForm.Form.PicHeight);
                size 
= random.Next(36);
                trees.Add(
"YoungTree" + i, new Tree(x, y, size, Color.Green));
            }
        }
        
public void Display()
        {
            
for (int i = 0; i < 20000; i++)
            {
                Tree tree 
= trees["CasuarinaTree" + i] as Tree;
                tree.Display();
                tree 
= trees["YoungTree" + i] as Tree;
                tree.Display();
            }
            Graphics graphic 
= LandpyForm.Form.GetGraphics();
            graphic.DrawString(
"OK"new Font("宋体", 12f), new SolidBrush(Color.Black), 00);
        }
    }

    
public class Trees
    {
        
private Hashtable trees = new Hashtable();

        
public Trees()
        {
            
for (int i = 0; i < 20000; i++)
            {
                trees.Add(
"CasuarinaTree" + i, new CasuarinaTree());
                trees.Add(
"YoungTree" + i, new YoungTree());
            }
        }
        
public void Display()
        {
            
for (int i = 0; i < 20000; i++)
            {
                Random random 
= new Random();
                
int x = random.Next(0, LandpyForm.Form.PicWidth);
                
int y = random.Next(0, LandpyForm.Form.PicHeight);
                
int size = random.Next(36);
                IFlyweightTree tree 
= trees["CasuarinaTree" + i] as IFlyweightTree;
                tree.Display(x, y, size);
                x 
= random.Next(0, LandpyForm.Form.PicWidth);
                y 
= random.Next(0, LandpyForm.Form.PicHeight);
                size 
= random.Next(36);
                tree 
= trees["YoungTree" + i] as IFlyweightTree;
                tree.Display(x, y, size);
            }
            Graphics graphic 
= LandpyForm.Form.GetGraphics();
            graphic.DrawString(
"OK"new Font("宋体", 12f), new SolidBrush(Color.Black), 00);
        }
    }

    
internal interface IFlyweightTree
    {
        
void Display(int _x, int _y, int _size);
    }

    
internal class CasuarinaTree : IFlyweightTree
    {
        
public void Display(int _x, int _y, int _size)
        {
            Graphics graphic 
= LandpyForm.Form.GetGraphics();
            graphic.DrawEllipse(
new Pen(new SolidBrush(Color.Brown)), _x, _y, _size, _size);
        }
    }

    
internal class YoungTree : IFlyweightTree
    {
        
#region IFlyweightTree 成员

        
public void Display(int _x, int _y, int _size)
        {
            Graphics graphic 
= LandpyForm.Form.GetGraphics();
            graphic.DrawEllipse(
new Pen(new SolidBrush(Color.Green)), _x, _y, _size, _size);
        }

        
#endregion
    }
}
原文地址:https://www.cnblogs.com/pangxiaoliang/p/1531155.html