First look at one of the simplest textbook-style single case mode:
Class Csingleton
{public
:
static csingleton* getinstance ()
{
if (NULL = PS)
{//TAG1
PS = new Csingleton;
}
return PS;
}
Private:
Csingleton () {}
Csingleton operator= (const Csingleton s);
Static csingleton* PS;
csingleton* Csingleton::p s = NULL;
There are 2 main points:
1.private constructors and = opera
Brief introductionA single case pattern is one of the simplest forms of design patterns. The purpose of this pattern is to make an object of a Class A unique instance of the system. To do this, you can start by instantiating it from the client. Therefore, you need to "block" all access to the object that you want to generate, using a mechanism that only allows you to generate unique instances of the object class. Use the factory method to limit the in
How to modify the Fstab file in Linux single-user modeAdded a new hard disk, the partition format in the Fstab set the automatic mount, fstab Setup error, resulting in a Linux reboot error and can not log on to the system, so let Linux enable single-user mode to enter the system, when modifying the/etc/fstab file, prompt file read-only, Cannot be modified.There a
PHP Single example mode to realize shopping cart function-php Instance tutorial complete this small function must write the need to develop good habits PHP shopping cart development requirements function is as follows 1: Shopping cart in Session 2: Single case mode to develop function: Add or delete to increase the red
I, Java reflection technology can get all the methods of the class, member variables, but also access to the private construction method, so that the singleton model used in the private constructor is called will produce multiple instances, write code test.
Package test;
Import Java.lang.reflect.Constructor;
public class Singetontest {private static singetontest singleton = null;
private int s = 0; The constructor method is a proprietary private singetontest () {}//Synchronized Fetch insta
Several ways to implement Java single example pattern
Single case mode It's written in many books:
public class SingleTon1 {
private SingleTon1 () {
}
private static SingleTon1 instance = null;
public static SingleTon1 getinstance () {
if (instance = null) {
instance = new SingleTon1 ();
}
return instance
}
}
Privatization of the construction method:
1. You can use private to declare properties, private declared properties can not be directly accessed by the outside world, you can also use the private declaration method, the declared method can only be accessed within the class.
2. In fact, you can also use private to declare the construction method, the declaration of the construction method, once declared, then the external can not directly instantiate the object operation.
Original link: Zookeeper in the actual combat of the single cluster mode
The previous article introduces the installation and application of zookeeper stand-alone mode, but zookeeper is designed to solve distributed application scenarios, so it is usually run in cluster mode. Today we are planning to deploy three zooke
Tags: term win switch LAN ALT note has a server serverIn CentOS7, there is a single-user mode, similar to the security mode under Windows. If we forget the root password, we can reset it in single-user mode.Note: This operation must be done in front of the server and cannot be operated remotely.1. Restart the machine,
Note: Scenes and examples are from GitHub's design pattern. Portal:Https://github.com/iluwatar/java-design-patterns/tree/master/singletonIntention:Singleton mode even to ensure that a class has only one instance and provides a global access point.Scene:There is only one ivory tower in the world that allows wizards to learn magic, and all wizards come to this ivory tower to practise. Then the ivory tower can be understood here as a
One, single user modeUsage scenario: Forget password, but can't reload the system, this is used in single-user mode.Operation Steps:1, restart the computer, enter the following interface2, press E to enter the next screen3, pull this page down, modify and add the following statementRW init=/sysroot/bin/sh4, modify into as, press ctrl+x continue to run5, enter the following interface:6, input chroot/sysroot/
Single-Case modeSingleton mode is useful when you need to ensure that an object can have only one instance. It delegates control of the created object to a single point, and at any time the application will only have one instance to exist.The singleton pattern must contain: private constructor, static variable, public static method, private Clone method.Here's a
First step
We use our fingers to slide the "home" and "back" keys from left to right, and then we can go to a single hand mode interface on the right, as shown in the following figure.
Second Step
If our opposition has always been from right to left in turn "home" and "multitasking" key, you can enter the Millet 4 mobile phone A single hand
multithreaded situations.The key to implementing the singleton pattern is that the constructors of the singleton classes cannot be accessed arbitrarily. All of our implementations can actually initialize the instance arbitrarily. It can achieve the effect is in accordance with the agreed call method can achieve the effect of a single case. In the above implementation. The first defines a private constant instance, and other classes are not accessible
The intent of the singleton pattern is to ensure that a class has only one instance in the entire JVM and provides him with a global access point.single case mode mechanismHow to prevent other developers from creating new instances of the class.You can create a unique constructor and set it to private.single case and threadMultithreading delay Initializes a single case pattern to try to avoid multiple threa
When to use a single case mode:
When there are multiple instances that can cause procedural logic errors
Second, the benefits:
1, reduce the memory consumption2. The singleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to a unique instance.3, because the class controls the instantiat
to perform synchronized (Singleton.class). thereby improving performance.Ps: There is also the implementation using private static Object obj = new Object (), plus synchronized (obj), there is actually no need to create more than one object. Synchronized (X.class) is used to make sure, that there are exactly one Thread in the block.2, the role of a single caseThere are two main functions of the single case
different, but the shared variable#Phase DclassSingleton_d1 (object): _store= {} def __new__(CLS, *args, * *Kwargs): obj= Super (SINGLETON_B1, CLS).__new__(CLS, *args, * *Kwargs)Printcls, obj, id (cls._store), id (obj._store), id (singleton_b1._store) obj.__dict__=Cls._storereturnobj#If you inherit other classes first, it will cause the singleton mode to failclassF (SINGLETON_D1):def __init__(self, name): Self.name=NAMEF1= F (' Python') F2= F (' S
. In any case, if you serialize an object of a singleton class and then restore multiple objects, you will have multiple instances of the Singleton class.The fix for the first problem is:Java code
private static Class GetClass (String classname)
Throws ClassNotFoundException {
ClassLoader ClassLoader = Thread.CurrentThread (). Getcontextclassloader ();
if (ClassLoader = = null)
ClassLoader = Singleton.class.getClassLoader ();
Return (Classloader.loadclass (classname));
}
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.