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

What are the singleton mode and multiple case mode of Java

2025-01-28 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 "what is Java singleton pattern and multi-case model". 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!

A singleton model

1 code

Class Boss {private static Boss instance;// static member variable, used to hold the unique created object instance private Boss () {/ / using the privatization construction method to prevent external creation of objects} public static Boss findBoss () / / check and ensure that there is only one instance {if (instance = = null) {System.out.println ("currently there is no boss, assign one now!") ; instance = new Boss ();} else {System.out.println ("I already have a boss! report directly.") ;} return instance;}} public class Singleton {public static void main (String [] args) {Boss boss1 = null; / / declare object boss1 Boss boss2 = null; / / declare object boss2 boss1 = Boss.findBoss (); / / instantiate object boss2 = Boss.findBoss (); / / instantiate object}}

2 run

There is no boss at present, appoint one immediately! I already have a boss! Let's report directly.

Two multi-case models

1 code

/ / enumerated class enum Sex {male, female}; class sexClass {private String title; / / Save information private static final sexClass MALE = new sexClass ("male"); private static final sexClass FEMALE = new sexClass ("female"); private sexClass (String title) / / Private constructor {this.title = "created object gender is:" + title; System.out.println ("constructor" + title) } public static sexClass getInstance (Sex sex) / / static method {switch (sex) {case male: return MALE; case female: return FEMALE; default: return null;}} @ Override public String toString () {return this.title;}} public class TestMultiton {public static void main (String args []) {System.out.println (sexClass.getInstance (Sex. (male)); / / if this sentence is also annotated, the output is empty, reflecting the reliance on loading (loading the class when it is used) / / System.out.println (sexClass.getInstance (Sex.). Female);}}

2 run

Constructor male constructor female created object gender: male

3 description

Multiple design patterns are actually an extended version of "singleton design patterns". The design ideas and implementation steps are very similar.

This is the end of the content of "what are Java singleton patterns and multiple patterns". Thank you for your 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.

Share To

Development

Wechat

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

12
Report