当前位置:网站首页>2 enjoy yuan mode
2 enjoy yuan mode
2022-07-28 20:56:00 【W.C.Zeng】
The flyweight pattern
The flyweight pattern : Multiple instances with similar functions , Share a public instance .
We need to render a forest , There are a thousand trees in the forest , Every tree has the same part : Grid of trees 、 The texture of trees and leaves
Each tree has different parts : Location 、 rotate 、 Zoom, etc
Encapsulate the same part into a class , Instantiate only once ; Different parts are encapsulated into a class , Instantiate a thousand times .
stay Direct3D and openGL The graphic API in , Provide separately
The first is the public data block that will be rendered many times —— Instances of classes of shared trees
The second is the instance list and its parameters —— An example of a thousand trees ( Contains different locations 、 rotate 、 The zoom )
This renders a forest
The use trace of Xiangyuan mode is not obvious , If you do not additionally encapsulate a class shared between multiple instances , This seems to be resource sharing .
stay 1 Command mode in , Jump commands are purely behavioral, stateless records , under these circumstances , There are multiple instances of jump command classes , It's a waste of memory , Because all instances are functionally equivalent .
Flyweight ( Enjoying yuan ) Pattern solved “ Instantiating multiple instances with the same function wastes memory ” This problem .
The solution is to let multiple similar instances share an instance of a class
How to use the sharing mode ?
Illustrate with examples , Create a 2D World class , Generate an instance of the world class later
public class World
{
public List<Terrain> terrainList = new List<Terrain>();
public TerrainProp grassProp = new TerrainProp(1, false, Texture.GRASS_TEXTURE);
public TerrainProp hillProp = new TerrainProp(3, false, Texture.HILL_TEXTURE);
public TerrainProp riverProp = new TerrainProp(2, true, Texture.RIVER_TEXTURE);
}
There are terrain classes in the world class , The data in the terrain category is divided into two parts :
Part is the position of the terrain instance in the world , Each terrain instance should be in a different position in the world , This part is different for each instance 、 Unshareable ;
public class Terrain
{
#region Unshareable parts
// The position of the lower left vertex of the terrain block in the world
public int x;
public int y;
public int width = 10;
public int height = 10;
#endregion
#region The shared part
public TerrainProp prop;
#endregion
public Terrain(int x, int y, TerrainProp prop)
{
this.x = x;
this.y = y;
this.prop = prop;
}
}
The example in the book is to store the terrain in the form of an array into the member attribute of the world class , Therefore, the index of the array directly represents the location of the terrain , There are only shareable parts left between different terrains
The other part is the characteristics of terrain instances , for example “ Moving speed on the terrain ”、“ Whether this terrain is navigable ”、“ The map of this terrain ” Such a characteristic , This part can be used in the same type of terrain instances Shareable .
public class TerrainProp
{
public int moveSpeed;
public bool isWater;
public Texture texture;
public TerrainProp(int moveSpeed, bool isWater, Texture texture)
{
this.moveSpeed = moveSpeed;
this.isWater = isWater;
this.texture = texture;
}
}
public class Texture
{
public static Texture GRASS_TEXTURE;
public static Texture HILL_TEXTURE;
public static Texture RIVER_TEXTURE;
}
Last , When generating multiple terrain instances of the world , The same type of terrain shares the same terrain attribute TerrainProp
public void GenerateWorld()
{
terrainList.Clear();
// Generate Two lawns 、 Two hills 、 The two rivers are arranged horizontally
for (int i = 0; i < 6; i++)
{
TerrainProp tmp;
if (i < 2)
{
tmp = grassProp;
}
else if (i < 4)
{
tmp = hillProp;
}
else
{
tmp = riverProp;
}
var terrain = new Terrain(i * 10, 0, tmp);
terrainList.Add(terrain);
}
}
边栏推荐
- 如何平衡SQL中的安全与性能?
- Introduction to redis II: RedHat 6.5 installation and use
- 微信小程序的分包加载
- Two written interview questions about regular
- Random talk on GIS data (VI) - projection coordinate system
- Alibaba cloud MSE supports go language traffic protection
- 查询oracle视图创建语句及如何向视图中插入数据[通俗易懂]
- 阿里云 MSE 支持 Go 语言流量防护
- Mongoose condition queries part of the data of the specified attribute
- Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~
猜你喜欢

不懂就问,快速成为容器服务进阶玩家!

想画一张版权属于你的图吗?AI作画,你也可以

Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~

Want to draw a picture that belongs to you? AI painting, you can also
![【ADB常用命令及其用法大全(来自[醒不了的星期八]的全面总结)】](/img/63/91b53b0ba718537383a97df59fe573.png)
【ADB常用命令及其用法大全(来自[醒不了的星期八]的全面总结)】

融合数据库生态:利用 EventBridge 构建 CDC 应用

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

Integrating database Ecology: using eventbridge to build CDC applications

Laser slam:logo-loam --- code compilation, installation and gazebo test

Teach you unity scene switching progress bar production hand in hand
随机推荐
Unity typewriter teaches you three ways
What is data center? What value does the data center bring_ Light spot technology
Interesting pictures and words
About the title of linking to other pages
《软件设计师考试》易混淆知识点
[server data recovery] HP StorageWorks series storage RAID5 two disk failure offline data recovery case
Alibaba cloud MSE supports go language traffic protection
Fragment中使用ViewPager滑动浏览页面
Prize essay solicitation | 2022 cloud native programming challenge draft activity opens
C # basic 5-asynchronous
Unity foundation 4 common plug-ins
Integrating database Ecology: using eventbridge to build CDC applications
System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution
Observer mode, object pool
Unity foundation 5-optimization strategy
Laser slam:logo-loam --- code compilation, installation and gazebo test
不懂就问,快速成为容器服务进阶玩家!
怎样搭建企业内部维基百科
Introduction to redis II: RedHat 6.5 installation and use