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 use meta-pattern in web design pattern

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the meta-pattern in the web design pattern". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the meta-pattern in the web design pattern.

Sharing meta-mode

First of all, let's take a look at what the shared meta-pattern is. Wikipedia explains that the shared meta-pattern (English: Flyweight Pattern) is a software design pattern. It uses shared objects to minimize memory usage and share information with as many similar objects as possible; it is suitable for use when a large number of objects are simply repeated, resulting in unacceptable use of large amounts of memory. Usually part of the state in an object can be shared. A common practice is to place them in external data structures and pass them on to shared elements when needed.

Keywords: reduce memory, share objects, external data

To put it simply, it means that there are both the same content and different content in a batch of objects, the same content is shared, and different content is transmitted dynamically to minimize the generation of objects. One thing to understand here is that the underlying objects actually correspond to the same object, and the physical address is one.

Enjoy the meta-mode usage scene

There are still many scenarios for sharing meta-mode. The most common scenario is the String string class in Java JDK. Because there is a constant pool in JVM, the implementation of constant pool is a shared meta-mode to avoid the existence of multiple identical objects. In addition, thread pools and many places where buffer pools are used use the shared meta mode, such as the default cache of integers between-128and 127in the Integer class, and so on.

Before we demonstrate the shared meta pattern, we need to understand the names of several roles involved in the shared meta pattern

Abstract sharing metaclass: usually an interface that mainly provides an interface for modifying internal data

Specific sharing metaclass: the implementation class of sharing meta, usually stored in memory and easy to use.

Enjoy the meta-factory class: create a specific meta-class

Let's demonstrate the shared meta-pattern through a simple example. Let's create an interface class, then write an implementation class, and then create the shared meta-class through a factory. The related code is as follows:

UML diagram

Code demonstration

Let's first create an interface class IFlyweight.java, which contains a method to set the width, as follows

Then write an implementation class FlyweightImpl.java to implement the interface and pass a Color object in the constructor

In the third step, we write a factory class FlyweightFactory.java, which has a local cache of Color objects and saves only one object for the same color.

Color assist class Color.java

Finally, write a test application class FlyweightDemo.java

In the above test class, we defined three objects of two colors, and because the objects of the same color have been cached, our output is as follows:

Output result

"from the running results above, we can see that flyweight0 and flyweight1 are actually the same object. Although their width and height property values are different, their underlying layer corresponds to the same object itself. This is the core content of the shared meta pattern, which reduces the generation of objects by sharing variables, thereby reducing memory use.

Thank you for your reading, the above is the content of "how to use web design patterns to enjoy meta-patterns". After the study of this article, I believe you have a deeper understanding of how to use web design patterns to enjoy meta-patterns. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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