OO mode-singleton

Source: Internet
Author: User

Discussion 1:

Since there is only one class? Why is it necessary to define it in a mode? Isn't it possible to use conventions between programmers or use great design patterns?

1) Let's talk about the benefits of global variables. when defining a global variable, any function or line of code can easily access all global variables without extra costs, easy and easy. For example, when I charge fees for a data center, I define the Login User Name of the data center as a global variable, because the user name information is used in many aspects during the execution of the entire system. Here, it is easy to call global variables. However, if you replace it with a large software, or you define many global variables in your system, it will be confusing to use and complicate the original simple things.

2) I think it is also a difficult problem to set the variable name. If too many variables are involved, it will lead to a conflict of variable names, and it will undoubtedly add a difficult problem to myself.

3) Of course, the most important thing is that defining global variables undoubtedly increases the coupling between modules and functions. That is to say, you have pinned them together and want to modify them again, but it is not that easy.

Let's look at the class diagram:


The getinstance () method is static, which means it is a class method, so it can be used anywhere in the Code. In fact, this is the same as the global variable, however, a single piece can be delayed.

Discussion 2:

Usage of Singleton mode:

We only use one object, such as threadpool, cache, dialog box, registry object, log object, and the object that acts as the driver for printers, graphics cards, and other devices.

 

Discussion 3: simple but not simple

Simple: Because it only involves one class, it will eventually "harm" everyone. In fact, he is very naughty! However, it is very naughty.

In fact, I am still very fond of chocolate. If I can open a chocolate manufacturing factory, I can use what I learned and satisfy my appetite. However, it is better to eat less sweets!

If we have a boiler working, and the uninformed worker opens a boiler, the raw material of a boiler is enough. Then, we can imagine how much waste it will have ......

In this case, we can use Singleton to help you solve the problem:

Avoid opening a boiler again without your knowledge, that is, designing a single piece of Boiler:

// A single process of making chocolate public class chocolateboiler {private Boolean empty; // defines the private variable private Boolean boiled; // uniqueinstance holds a unique single-piece instance Private Static chocolateboiler uniqueinstance; private chocolateboiler () {// at the start, the boiler is empty = true; boiled = true;} public static chocolateboiler getinstance () {If (uniqueinstance = NULL) {// system. out. println ("creating unique instance of chocolate boiler"); uniqueinstance = new chocolateboiler ();} return uniqueinstance;} public void fill () <span style = "font-family: 文; font-size: 16pt; text-indent: 21pt; ">{</span>
If (isempty () {empty = false; boiled = false; // fill mixture }}

However, after execution, you will find that you are allowed to add raw materials during the heating process. This will overflow! The only reason we think of is that we have optimized chocolateboiler by using multiple threads. Let's create a chocolatecotroler to control it!

Public class chocolatecontroller {public static void main (string ARGs []) {chocolateboiler boiler = chocolateboiler. getinstance (); boiler. fill (); // fill boiler. boil (); <span style = "white-space: pre"> </span> // boiling method boiler. drain (); // how to exclude the boiling filler // will return the existing instance chocolateboiler boiler2 = chocolateboiler. getinstance ();}

If boiler is at work, boiler2 will only listen to the command.

The synchronized method is used for multi-thread processing, that is, the getinstance () method is changed to the synchronized method. The multi-thread disaster can be easily solved!

// Multithreading public static synchronized chocolateboiler getinstance () {If (uniqueinstance = NULL) {uniqueinstance = new chocolateboiler ();} return uniqueinstance ;}

If you want to urgently create an instance without changing the execution efficiency, you can create a single piece in a static initiator,

Public class Singleton detail {// create a single piece in static initializer to ensure thread security Private Static response uniqueinstance = new chocolateboiler (); Private chocolateboiler () {} public static chocolateboiler getinstance () {return uniqueinstance; // use directly }}


Singleton has just learned a little bit about it, and we are very much looking forward to practicing it in the cooperation of data centers! For a long time, there are three problems encountered in the code of the chocolate Boiler:

1) synchronize the getinstance method to ensure the most feasible method.

2) eager instantiation

3) double check shackles ()

The three methods are a layer-by-layer optimization process, but the optimization should also be targeted at the problem!






OO mode-singleton

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.