First, describeSingleton mode is a very common design pattern in which a class can have only one object (instance), typically by privatizing the class's constructor to prevent the object from being created outside of the class and providing a unique object to the outside world (this object is created in the Class). There are two common implementations of the singleton pattern in Java, one of which is the Bad Han way, which is to rest the class with static and initialize it when the class loads,
a hungry tiger, the story of the fire ... Look, there's no turning! There was a man wandering in the forest when suddenly met a hungry tiger, the tiger roared and jumped up. He immediately used his life's greatest strength and the fastest speed to escape, but the Tiger Chase, he has been running all the way, and finally by the tiger into the edge of the cliff.Standing on the edge of the cliff, he thought: i
1 single-instance patterns in textbooksWe all know how a simple singleton pattern should be implemented: the constructor is declared as private or protect to prevent being instantiated by an external function, and a private static class pointer is stored inside a unique instance, and the action of the instance is handled by a public class method. The method also returns an instance of the Singleton class.On the code:Class Singleton{protected:singleton () {}private:static singleton* p;public:stat
and CPU utilization. This is the purpose of the single-piece mode. Of course, the single-piece mode is not just for this situation. The applicability of the single-piece mode is described in the book "Design mode: The basis of reusable object-oriented software" as follows:1. When a class can have only one instance and the customer can access it from a well-known access point.2, when this unique instance should be extensible by subclasses, and the customer should be able to use an extended insta
The essential difference between lazy and a hungry man in a single case lies in the following points:1, a hungry man type is thread-safe, in the creation of a class at the same time has created a static object for the system to use, not change later. Lazy type If you create an instance object without adding synchronized, the access to the object is not thread-safe.2, from the realization of their biggest di
Productare you hungry?Choose the reason: With the takeaway can easily have a not to go out and hungry of the cool crooked weekend. the first part of the research, evaluation
Download the software and use it to describe the simplest and most intuitive personal first-time experience.
Main interface:First time to start is a freshman time, feel the page is neat and clean, the use of the hue also m
Steve jobs:commencement Address at Stanford University"Stay hungry, stay foolish."If you are hungry for knowledge, be foolish in modesty.Steve Jobs (Steve Paul jobs), CEO of Apple Computer and Pixar Animation Corporation (Pixar).The following is a speech by Steve Jobs at the Stanford University graduation ceremony on June 12, 2005.Thank. I ' m honored to is with your today for your commencement from one of
Design of the singleton mode:1 /*2 This is the first object to initialize. 3 called: a Hungry man type. 4 5 once the single class is in memory, the object has been created. 6 Class Single7 {8 private static Single S = new single ();9 Private single () {}Ten Public Static Single getinstance () One { A return s; - } - } the */ - - //The object is initialized only when the method is called, also called the object's deferred loading. Become: Lazy
I want to share with you a real story around our programmers when the whole community is watching amid. And I in the "If you can't see you can make it?" "In the article introduced by those blind programmers, the same is stay hungry, stay foolish. But I personally think that the story I want to tell you today is more meaningful to us ordinary people. I sincerely hope that we read this "from the toilet to the programmer" story, we can learn something fr
hungry man-static final field
This approach is very simple, because instances of a singleton are declared static and final variables, initialized when the class is first loaded into memory, so creating the instance itself is thread-safe.
The public class singleton{
//class is loaded with the initialization of the
private static final Singleton instance = new Singleton ();
Private Singleton () {} public
static Singleton getinstan
In some cases, some objects require only one, in other words, each class requires only one instance. For example, one computer is connected to multiple printers, but there is only one print program on this computer, and a singleton mode is required to prevent the printer from being entered into the printer from left to right. 、The role of the singleton pattern is to ensure that, at any one time, instances of the Singleton class only exist for the entire application life cycle. There are three ma
//-------------------------------------------------------------A hungry man mode--start------------------------------------------------ ----------- PackageCom.study.DesignPattern01;/*** Create a single instance of a Hungry man mode *@authorZlhome * Some objects, we only need one, if more, then it can lead to inconsistent data, excessive resources and so on, such as: Configuration file, tool class, thread po
Nowadays many people, such as work, have no time to eat. Or for some reason there is no way to eat in a restaurant. Just appeared outside.
For example, now very hot public comments, hungry Mody, the United States to sell, Word-of-mouth outside, home urgent send, and so on. Some of the takeout industry.
Like I'm looking hungry today? is a Chinese largest professional network catering platform, these take-awa
Hungry, Baidu Takeout, the United States and the group to sell how to share the same printer. With the rapid rise of the delivery platform, a takeaway platform has long been unable to meet the needs of a business, many businesses are at the same time on a number of take-away platform, such as our daily life the most common in the United States to sell, Baidu take-away, hungry Mody, and so on, so, the proble
The singleton mode is a very basic design pattern, which may be asked to write different types of singleton code in the interview, there are three main modes:
1. A Hungry man mode:
A hungry man mode, very hungry very anxious, so the class is loaded when the instance object is created public
class Singleton1 {
private static Singleton1 singleton = new Singleto
1345 Hungry cowstime limit: 1 sspace limit: 128000 KBtitle level: Golden GoldTitle DescriptionDescriptionJohn keeps a number of cows and the cows eat every night. Because of the relatively simple conditions, not all cows can eat food. The way cows eat is this: John has n buckets (1John, of course, did not want the cows hungry, so he wanted to meet the demands of some of the groups according to the cows ' re
() //static, synchronous, exposing access point { if (intance = = null) { Intance = new Lazysingleton (); } return intance;} }Key points: (given on code comments)1) The constructor is defined as private----cannot get the object of the class in another class, it can only get its own object in the class itself2) The member variable is static, not initialized----class is loaded fast, but the unique instance of the access class is slow, and static gua
Concept:Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system.The singleton mode has the following characteristics:1. A singleton class can have only one instance2. The Singleton class must create its own unique instance for itself3. The Singleton class must provide this instance to all other objectsA Hungry man type:/* A hungry ma
Single case Mode
A single case pattern is used very frequently in programming, designed to provide only one object in a program (guaranteed to be constructed only once), such as log objects written to logs, and Windows Task Manager implementation (only one can be opened). Here we mainly introduce the implementation of Java for the single example mode (including a Hungry man and lazy). Implement
Here, using the log class as an example, the log object
1. The A hungry man is created when the JVM is started in a singleton class, and the lazy type is created when the first instance of a singleton class is accessed, that is, the A hungry man is considered preload and the lazy load is deferred;
Sample code:
public class Eagersingleton {private static final Eagersingleton instance = new Eagersingleton (); Private Eagersingleton ( {System.out.println ("initia
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.