Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the meta-sharing mode of Flyweight

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, Xiaobian will share with you the relevant knowledge points on how to realize the Flyweight sharing mode. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you will gain something after reading this article. Let's find out together.

Flyweight (enjoy element mode)

Flyweight is a structural pattern, a shared object design pattern.

Intention: Use sharing techniques to efficiently support a large number of fine-grained objects.

examples given

If you don't understand the intention above, it doesn't matter. Design patterns need to be used in daily work. Combining examples can deepen your understanding. Below I have prepared three examples to let you experience what scenarios will use this design pattern.

Alphabetic objects for rich text editors

Rich text editor in English environment, where the text consists of a large number of letters, in order to facilitate uniform formatting, calculation and other processing, you need to store each letter as an object, but the cost of such storage is too high.

Given that there are 26 English letters in total, there are a lot of reused letters in the document, and each letter has the same and read-only information except for position information. Is there a way to reduce the number of huge letter objects in rich text scenes?

network disk storage

When we upload a movie, sometimes dozens of GB of content is uploaded in less than a second, this is the online disk prompt you,"has adopted the speed technology second transmission," you will not wonder, why such a powerful technology can not take effect every time?

In addition, when the network disk is stored, the same movie may be stored in different folders of different users, and the movie file is particularly large, similar to rich text, the movie file is only stored in different locations, and the rest of the content is particularly large and read-only, what can be optimized for storage?

massively multiplayer games

When playing multiplayer games, in order to prevent plug-ins, the creation and calculation of general objects are completed in the server, so how to ensure that after one player picks up items, the items seen by another player will disappear?

In fact, the truth is self-evident, although between different clients, game objects are independent of each other, but in a game, all players 'objects are shared in the server.

intended to be interpreted

"Sharing" is the essence of the sharing pattern. Sharing a large number of objects with many internal states and few external states is how the sharing pattern is used.

Intention: Use sharing techniques to efficiently support a large number of fine-grained objects.

Sharing technology can be understood as caching. When an object is created and the same object is accessed again, no new object is created. Only when an object that has not been cached is a new object created and cached immediately.

This can be done efficiently to support a large number of fine-grained objects, in rich text examples, countless letters are a large number of fine-grained objects, in network storage, movie files are a large number of fine-grained objects, in large multiplayer games, there are a large number of fine-grained objects in each game.

These fine-grained objects all share the same characteristics:

It's very large, and it's easy to understand.

Has a large amount of internal state and does not change from client to client.

Rich text letters do not change because they are displayed in different sentences, only the state changes; movie files do not change because they are placed in different user folders, only the users who belong to them and which folders they are placed in change; in multiplayer games, the same weapon object does not have more ammunition because there are multiple computers running independently, only the clients who are accessed change.

There are few or no external states. As explained above, the position of letters, the position of movies, and the client side of game objects are all external states. Compared to their internal states, these external states are very small in size and easy to store separately.

In this case, we can save a lot of space by sharing the internal state of the object and storing the external state separately.

Especially for the scene of network disk, promised to give the user 2 TB of storage space, this user saw that other people shared 100 movies, click "download to my network disk", at this time although occupied their own 1 TB of network disk space, but in fact the network disk operator did not increase 1 TB of storage space, may actually increase 1kb of storage space, recorded the storage location, this is the place of network disk chicken thief, and does not occupy space content, However, it occupies the storage space purchased by users in real money.

Of course, this is the value of the sharing mode. For the network disk company, the value is huge, but for the user, there is no value. So the value of the meta-sharing pattern is global, such as reducing the number of huge letter objects for the entire rich text editor, but not optimizing for each letter object.

structure diagram

For clients, the following figure describes how to share Flyweight:

Flyweight: Shared interface through which you can manipulate the external state of objects.

ConcreteFlyweight: An object that implements the Flyweight interface and is shareable.

UnsharedConcreteFlyweight: Objects that are not shared, because not all objects can actually be shared in the ConcreteFlyweight schema.

Flyweight Factory: Creates and manages Flyweight objects, returning Flyweight objects through them, if created, returning the one created before, if not creating a new one.

Client: Client using Flyweight.

As you can clearly see in the second figure, two different clients hold the same aConcreteFlyweight reference.

code examples

The following example is written using typescript.

class FlyweightFactory {

public getFlyWeight(key) {

if (this.flyweight[key]) {

return this.flyweight[key]

}

const flyweight = new Flyweight()

this.flyweight[key] = flyweight

return flyweight

}

}

The getFlyWeight method provided by FlyweightFactory is actually caching flyweight instances by key, and only one flyweight instance is stored under the same key.

drawbacks

If there are not many fine-grained objects, there is no need to use the metamodel pattern.

In addition, even if there are many fine-grained objects, if the internal state of the object is not much, and it is mainly external state, then the sharing mode will not play any role, because the sharing mode can only save internal state by sharing objects, but cannot save external state.

In addition, if the number of shared objects mapped to the meta-schema is not orders of magnitude less than the original object, it makes little sense to use it. For example, the rich text editor example, for English, a total of 26 letters, then 10000 words of the article optimization ratio is 10000:26, but for Chinese articles, the text itself is a lot of examples, maybe 10000 words of the article, after the Chinese characters are removed, there are still 3000, then the optimization ratio is 10000:3000, at this time the meaning of the meta-sharing mode is not so hit.

The above is all the contents of this article "How to realize Flyweight enjoy yuan mode". Thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report