当前位置:网站首页>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);
}
}
边栏推荐
- “当你不再是程序员,很多事会脱离掌控”—— 对话全球最大独立开源公司SUSE CTO...
- 7/27 training log (bit operation + suffix array)
- Thinking and summary of R & D Efficiency
- System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
- Redis入门二:redhat 6.5安装使用
- About the title of linking to other pages
- 作业 ce
- Explain the life cycle function in unity in detail
- Unity package exe to read and write excel table files
- Network layer performance test
猜你喜欢

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

第六七八次作业

Prometheus complete process of configuring alertmanager

Easynlp Chinese text and image generation model takes you to become an artist in seconds

Explain the imported 3D model in unity

Integrating database Ecology: using eventbridge to build CDC applications

作业 ce

Random talk on GIS data (VI) - projection coordinate system

Learn about the native application management platform of rainbow cloud

研发效能的思考总结
随机推荐
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
Explain several mobile ways of unity in detail
7/27 training log (bit operation + suffix array)
Redis入门二:redhat 6.5安装使用
网络各层性能测试
Laser slam:logo-loam --- code compilation, installation and gazebo test
Explain mesh Collider in unity
How to make the design of governance structure more flexible when the homogenization token is combined with NFT?
Space shooting Lesson 10: score (painting and writing)
Observer mode, object pool
PL515 SOT23-5 单/双口 USB 充电协议端口控制器 百盛电子代理商
记一次Runtime.getRuntime().exce(“command“)报错
System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
Interesting pictures and words
Baklib|为什么说企业需要重视客户体验?
Job CE
JS win7 transparent desktop switching background start menu JS special effect
Explain the camera in unity and its application
Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~
Oracle库访问很慢,是什么原因?