In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to understand the meta-pattern of Java design pattern". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. introduction
We all know the singleton pattern, which uses a global variable to avoid the consumption caused by repeatedly creating objects. What should we do if there are a large number of similar objects in the system? With reference to singleton mode, shareable objects can be cached through object pooling to avoid creating multiple objects, reduce memory use as much as possible, improve performance, and prevent memory overflow.
In the software development process, if we need to reuse an object, if we repeatedly use new to create the object, then we need to apply for memory space many times in memory, which may lead to more and more memory use, which is a very serious problem, but the sharing meta-pattern can solve this problem. Let's take a look at how the sharing meta-pattern solves this problem.
Second, what is the sharing meta-model
Definition: sharing meta-objects, using sharing technology to effectively support the reuse of a large number of fine-grained objects. If there are multiple identical objects in a system, you only need to share a copy of the object and you don't have to create a new object for each use.
Sharing meta-pattern is one of the few design patterns that only improve system performance, and its main function is to reuse large objects (heavyweight objects) to save memory space and object creation time.
Object-oriented can easily solve some scalability problems, but in this process, the system must produce some classes or objects. If there are too many objects in the system, the performance of the system will be degraded. The simplest and most direct way to solve such a problem is to reduce the number of objects in the system. The shared meta-pattern provides a solution that uses shared technology to reuse the same or similar objects. That is to say, code sharing of the same or similar objects is implemented.
The so-called sharing meta-pattern is to run the sharing technology to effectively support the reuse of a large number of fine-grained objects. The system uses a small number of objects, and these are relatively similar, the state change is small, can achieve multiple reuse of objects.
The shared pattern supports the reuse of a large number of fine-grained objects, so the shared meta-pattern requires that the objects that can be shared must be fine-grained objects.
First of all, understand two concepts: internal state and external state.
Internal state: a shared part within a shared meta-object that does not change with the external environment.
External state: changes with the environment, the state that can not be shared is the external state.
Because the sharing meta-mode distinguishes the internal state from the external state, we can set different external states so that the same object can have some different characteristics, while the internal state is set to the same part.
In our programming process, we may need a large number of fine-grained objects to represent objects, if these objects are all the same except for a few parameters, at this time we can use the shared meta pattern to greatly reduce the number of objects in the application.
How to make use of the meta-sharing model? Here we just need to move a small number of different parts of them as parameters to the outside of the class instance and pass them over when the method is called. This illustrates that the internal state is stored inside the shared meta-object, while the external state should be considered by the client.
Third, the structure of sharing meta-model.
1.Flyweight: share meta-interface, the superclass or interface of all specific metaclass, through which Flyweight can accept and act on external states. Through this interface, you can pass in external states, which may be used in the method processing of shared meta-objects.
2.ConcreteFlyweight: the specific sharing element implementation object, which specifies the internal state, must be shared, and needs to encapsulate the internal state of the Flyweight.
3.UnshareConcreteFlyweight: non-shared meta-implementation objects, not all Flyweight implementation objects need to be shared. A non-shared shared meta-implementation object is usually a composite object of a shared meta-object.
4.FlyweightFactoty: shared meta-factory class, which is mainly used to create and manage shared meta-objects, and to provide interfaces to access shared meta-objects. When a user requests a Flyweight, the FlyweightFactory provides a Flyweight object that has already been created or a new one (if it does not exist).
5.Client: sharing meta-client, the main job is to maintain a reference to Flyweight, calculate or store the external state of the sharing element, of course, here you can access shared and unshared Flyweight objects.
The core of the shared meta-factory class is the shared meta-factory class. The function of the shared meta-factory class is to provide a shared meta-object pool for storing shared meta-objects. When users need an object, they first obtain it from the shared meta-pool. If it does not exist in the shared meta-pool, a new shared meta-object is created and returned to the user, and the newly added object is saved in the shared meta-pool.
Fourth, the similarities and differences between the sharing meta-model and the singleton model.
Sharing meta-mode can create objects again or take cached objects
Singleton mode strictly controls that there is only one instance object in a single process.
The sharing meta pattern can be implemented on its own for external singletons, or it can create more objects as needed.
The singleton pattern is that self-control needs to add logic that does not belong to the object itself.
Both of them can save time for object creation ThreadPool thread pool and database connection pool both use shared meta mode
Fifth, the advantages and disadvantages of sharing meta-model.
Advantages:
The number of objects in memory can be greatly reduced, so that only one copy of the same or similar objects is saved in memory, which can save system resources and improve system performance.
The external state of the shared meta-pattern is relatively independent and does not affect its internal state, so that the shared meta-objects can be shared in different environments.
Disadvantages:
The sharing meta-mode makes the system complex, and it is necessary to separate the internal state from the external state, which complicates the logic of the program.
In order for the object to be shared, the sharing meta-mode needs to externalize part of the state of the shared meta-object, and reading the external state will make the running time longer.
Sixth, enjoy the use of meta-mode.
A system has a large number of identical or similar objects, resulting in a large amount of memory consumption.
Most of the states of an object can be externalized, and these external states can be passed into the object.
When using the shared meta-mode, it is necessary to maintain a shared meta-pool to store the shared meta-objects, which requires a certain amount of system resources, so it should be worth using the shared meta-objects only when they need to be reused many times.
7. Public abstract class abStudent {public string Name; public string schName; public string Sex; public abStudent () {schName = "Jilin University"; Sex = "male" } public override string ToString () {return string.Format ("my name is {0}, gender {1}, school {2}", Name, Sex, schName);}} public class Student:abStudent {public Student (string name) {Name = name;}} public class School {private Dictionary StudentList Public School () {StudentList = new Dictionary (); StudentList.Add (1, new Student ("Zhang San"); StudentList.Add (2, new Student ("Li Si"));} public Student GetStudent (int num) {return StudentList [num] as Student }} class Program {static void Main (string [] args) {School school = new School (); Student student = school.GetStudent (1); Console.WriteLine (student.ToString ()); student = school.GetStudent (2); Console.WriteLine (student.ToString ()); Console.ReadKey () This is the end of "how to understand the meta-pattern of Java design patterns". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.