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

Example Analysis of Java sharing Meta-pattern

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

Share

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

This article mainly introduces the relevant knowledge of Java meta-pattern example analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Java meta-pattern example analysis article. Let's take a look.

Define

Sharing meta-pattern (FlyWeight Pattern), also known as fly quantity mode, uses sharing technology to effectively support a large number of fine-grained objects. Shared meta-pattern is an important way to implement pool technology.

Principle class diagram

Flyweight: an abstract meta-role, which is an abstract product class, and defines the internal and external states of the object

ConcreteFlyweight: specific meta-roles, specific product classes, abstract roles, and specific business logic

UnsharedConcreteFlyweight: a role that cannot be shared, which may also appear in shared meta mode

FlyweightFactory: shared meta-factory class, which is used to build a container for a pool, and provides public methods in the shared meta-factory (get objects from the pool, get the total number of objects in the pool, get objects according to key,)

Client: client side, using shared meta mode to complete business logic

Case requirement

We took a small outsourcing project to make a product display page for our customer Lao Wang. Lao Wang's friends feel good and want to do such a product display page, but their requirements are a little different.

1) some customers require the web page to be published in the form of news.

2) some customers require the web page to be published in the form of a blog

3) some customers want the web page to be published in the form of Wechat official account.

Solution: sharing meta-mode

Define site user classes

Public class User {private String name; public User (String name) {this.name = name;} public String getName () {return name;} public void setName (String name) {this.name = name;}}

Define website abstract classes-- enjoy meta-roles

Public abstract class Website {public abstract void show (User user);}

Define specific website classes-specific meta-roles

The release form of public class ConcreteWebsite extends Website {/ / website is private String type; public ConcreteWebsite (String type) {this.type = type;} @ Override public void show (User user) {System.out.println ("publish the site in the form of" + type + "); System.out.println (" the user name of the website is "+ user.getName ());}}

Define the website factory class-enjoy the meta-factory class

Public class WebsiteFactory {/ / create HashMap to act as the role of the pool private HashMap pool = new HashMap (); / / get the object public Website getWebsite (String type) {if (! pool.containsKey (type)) {pool.put (type,new ConcreteWebsite (type));} return pool.get (type) in the pool according to the type } / / get the total number of objects in the pool public int getWebsiteCount () {System.out.println ("Total number of objects in the current pool:" + pool.size ()); return pool.size ();}}

Define client test classes

Public class Client {public static void main (String [] args) {/ / create a sharing factory WebsiteFactory websiteFactory = new WebsiteFactory (); Website news = websiteFactory.getWebsite ("News"); news.show (new User ("Lao Li")); Website wechat = websiteFactory.getWebsite ("blog"); wechat.show (new User ("Lao Gao")); websiteFactory.getWebsiteCount () Website wechat1 = websiteFactory.getWebsite ("Wechat"); wechat.show (new User ("Lao Shen")); websiteFactory.getWebsiteCount ();}}

View test results

Analysis.

According to different types of requirements, a factory class is used to generate the corresponding specific sharing meta-objects, and the requirements are realized. The information of the sharing meta-object is divided into two parts, the internal state and the external state. The internal object refers to the information shared by the object, which is stored inside the shared meta-object and does not change with the change of the environment, while the external object changes with the change of the environment and cannot be shared.

This is the end of the article on "sample Analysis of Java sharing Meta pattern". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Java meta-pattern example analysis". If you want to learn more, you are welcome to follow 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