In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "instance usage of MonoState single-state mode". Many people will encounter this dilemma in the operation of actual cases, 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!
MonoState single state mode
Both MonoState mode and Singleton are patterns that guarantee singleness. But there is a clear difference in intent between it and Singleton. The default constructor of Singleton is private, which restricts the client to directly new the object and uses static to ensure that the object of the class is single, while the MonoState mode declares the constructor as public and all fields in the class as static. MonoState does not limit the number of objects created, but it has only one state.
Public class SqlRequest {private String sqlRequest; public SqlRequest (String sqlRequest) {this.sqlRequest = sqlRequest;} public String getSqlRequest () {return sqlRequest;} public void setSqlRequest (String sqlRequest) {this.sqlRequest = sqlRequest;}} public class DataBase {private String name; public DataBase (String name) {this.name = name } public void read (SqlRequest sqlRequest) {System.out.println (name + "received parameters" + sqlRequest.getSqlRequest ()); System.out.println (name + "and read data 123");} public void write (SqlRequest sqlRequest) {System.out.println (name + "received parameters" + sqlRequest.getSqlRequest ()) System.out.println (name + "and write data to the database 456");}} public class DataBaseBalancer {public DataBaseBalancer () {} private static int lastServedId; private static List dataBases = new ArrayList (); static {dataBases.add (new DataBase ("data Source 1")); dataBases.add (new DataBase ("data Source 2")) } public synchronized void serverRequest (SqlRequest request) {if (lastServedId > = dataBases.size ()) {lastServedId = 0;} DataBase dataBase = dataBases.get (lastServedId++); dataBase.write (request); dataBase.read (request);}} public static void main (String [] args) {DataBaseBalancer dataBaseBalancer1 = new DataBaseBalancer (); DataBaseBalancer dataBaseBalancer2 = new DataBaseBalancer () DataBaseBalancer1.serverRequest (new SqlRequest ("request 1"); dataBaseBalancer2.serverRequest (new SqlRequest ("request 2");}
From the above example, DataBaseBalancer internal variables are decorated with static, so no matter how many instances of DataBaseBalancer, the operation data is the same as static.
This is the end of "instance usage of MonoState single-state mode". 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.