1. Primary key generator develops the readers of the database driver information system know that in a relational database, all the data is stored in the table, and each table has a primary key. For most user input data, the primary key needs to be generated by the system in a serial number, rather than being given by the operator. Some relational database engines provide some sequence key generation mechanism. If SQL Server allows you to have an automatic number column in each table. Oracle provides a sequence object that provides a sequence key value. But some database engines do not have a corresponding mechanism, such as Sybase. At this time, we need to generate the primary key sequence number yourself. The usual approach is to use a table to store all primary key maximum. This table contains two columns, a column stores key name, another listing key value, and the client uses the SQL statement to manage the key value. Since the system is always required, the sequence key is always required, so the entire system requires a sequence key management object, which exists during operation. Considering that a sequence key manager can be managed by multiple sequence keys belonging to different modules, this sequence key manager needs to access the entire system.
2. Single-case mode single case mode has three main points: First, a class can only have an instance; the other is that it must create this instance itself; 2.1 Hungry Chinese singles-style Hungry-style single example is the simplest single-case class in Java, and the source code is as follows:
Public class eagersingleton {private eagersingleton ()} // static factory method public static electionleton getInstance () {return m_instance;} private static final eagersingleton m_instance = new EagerSingleton ();
When this class is loaded, the static variable instance will be initialized, and the private constructor of the class is called. At this time, the only instance of the single case is created.
The most important feature of the single case class in the Java language is that the class constructor is private, thereby avoiding the export use of constructor directly created any alternative instances. Since the constructor is private, this class cannot be inherited.
2.2 lazy single case
The same is the same as the hungry single example, the structure of the class is private, and the different is the lazy single case will instantiate themselves when they are first referenced. If the loader is static, then the lazy single case class is loaded when it is loaded. The source code is as follows:
public class LazySingleton {private LazySingleton () {} synchronized public static LazySingleton getInstance () {if (m_instance == null) {m_instance = new LazySingleton ();} return m_instance;} private static LazySingleton m_instance = null;}
It can be seen that the static factory method uses synchronization to handle multithreaded environments in the lazy single case implementation given above. Since the constructor is private, this class cannot be inherited.
Hungry Chinese single cases will initialize themselves when they are loaded, and from the perspective of resource utilization efficiency, it is slightly less than the lazy singles. However, when the lazy single case, when instantism, it must handle access restrictions on multiple threads at the same time.
2.3 Single-case states A single case class is a state, a stateful single case object is generally a variable single case object. Some single-case objects are often used as a repository (repositary). For example, a single case object can hold an int type property that provides a unique sequence number for the system.
On the other hand, a single case can also be not status, only for the object of providing tool-based functions. A single-size class that is not status is also a constant single case class.
3. Many cases
Single example mode is easy to promote to any and limited number of instances, this is called multiple models. Multi-case mode except for a class that can have multiple instances, the characteristics are similar to single case mode.
The number of instances of multiple models does not need to have the upper limit. Since there is no upper limit, the number of instances is not limited, so although this multi-class class is a single example mode, this multi-class class is not It must be able to return to a single case.
Since I don't know how many instances to create in advance, I will inevitably use all instances of aggregation management.
4. Application of single case mode
Let's go back to the primary key generator. Readers should have realized that this system design should be used in single case mode.
This design consists of a single case keygenerator and a KeyInfo object that stores information of a key. The source code is as follows:
import java.util.HashMap; public class KeyGenerator {private static KeyGenerator keygen = new KeyGenerator (); private static final int POOL_SIZE = 20; private HashMap keyList = new HashMap (10); private KeyGenerator () {} public static KeyGenerator getInstance ( ) {return keygen;} public int getNextKey (String keyName) {KeyInfo keyinfo; if (keyList.containsKey (keyName)) {keyinfo = (KeyInfo) keyList.get (keyName); System.out.println ( "key found") } Else {keyinfo = new keyinfo (pool_size, keyname); keylist.put (keyname, keyInfo); System.out.Println ("new key created");} return keyinfo.getNextKey ();}} if (Keylist. Containskey (keyName) {keyInfo = (keyInfo) Keylist.get (keyname); System.out.Println ("Key Found");} else {keyinfo = new keyInf o (POOL_SIZE, keyName); keyList.put (keyName, keyinfo); System.out.println ( "new key created");} return keyinfo.getNextKey ();}} public int getNextKey (String keyName) {public static KeyGenerator GetInStance () {Return Keygen;} private keygenerator () {private hashmap keylist = new hashmap (10); private static final int pool_size = 20; public class keygenerator {can see that KeyGenerator is a single case, it provides private construct Functions and a static factory approach provide their own unique instances to the outside world.
One system often does not stop one primary key, we use a gather keyList to store the KeyInfo object of different sequence key information.
Below is the Keyinfo class:
public class KeyInfo {private int keyMax; private int keyMin; private int nextKey; private int poolSize; private String keyName; public KeyInfo (int poolSize, String keyName) {this.poolSize = poolSize; this.keyName = keyName; retrieveFromDB (); } public int getKeyMax () {return keyMax;} public int getKeyMin () {return keyMin;} public synchronized int getNextKey () {if (nextKey> keyMax) {retrieveFromDB ();} return nextKey ;} private void retrieveFromDB () { String SQL1 = "Update KeyTable Set KeyValue = KeyValue " PoolSize "Where keyname = '" keyname "'" String SQL2 = "SELECT KeyValue from Keytable WHYNAME = '" Keyname "'"; // Execute the above operation in a transaction and submit / / assume that the returned value is 1000 int keyFromDB = 1000; keyMax = keyfromDB; keymin = KeyFromDB - PoolSize 1; NextKey = keymin;}}} keymax = keyfromdb;
Public synchronized int getNextKey () {public int getKeymax () {public keyInfo (int poolize, String Keyname) {The purpose of using KeyInfo is not to check each time the key value query. After all, a key is just some sequence numbers. In addition to each of the requests, it is not as good as one of the one-time pre-registration multiple key values, and then provides these predetermined key values many times. This is the cache mechanism of the key value. When KeyGenerator updates the data in the database, it increases the key value, but not add 1 but more, and the value added in this example is 20. In order to store all the information related to the key, use KeyInfo.
In addition to the information related to the keys, this KeyInfo provides a retrieveFromDB () method to query the database. The 20 key values obtained each query will then provide to the requester, until 20 key values are all used, and then 20 key values are scheduled to the database. KeyGenerator maintains a reference to the KeyInfo object. The client calls the getNextKey () method to get the key value of the next key.
Below is a source code for a schematic client Client class:
Public class client {private static keygenerator keygen; public static void main (string [] args) {keygen = keygenerator.getInstance (); for (int i = 0; i <25; i ) {system.out.println ("key (" (i 1) ") = " Keygen.getNextKey (" Po_Number "));}}} for (int i = 0; i <25; i ) {public static void main (String [] args ) {
5. Multi-case model application
As mentioned earlier, in order to handle the multi-series key value, in addition to the single state encapsulated by the single case mode to the aggregation state, the following is the source code of the KeyGenerator. It can be seen that this is a multi-class class, and each KeyGenerator object holds a specific KeyInfo object as an intrinsic state. The client can use this class's static factory method to get the required instance, and this factory method will first check the keygens accumulated by registration. If the required key name is in the aggregation, then the instance corresponding to this button directly to the client, if there is no existence, create a new instance.
import java.util.HashMap; public class KeyGenerator {private static HashMap kengens = new HashMap (10); private static final int POOL_SIZE = 20; private KeyInfo keyinfo; private KeyGenerator () {} private KeyGenerator (String keyName) {keyinfo = new KeyInfo (pOOL_SIZE, keyName);} public static synchronized KeyGenerator getInstance (String keyName) {KeyGenerator keygen; if (kengens.containsKey (keyName)) {keygen = (KeyGenerator) kengens.get (keyName);} else {keygen = new KeyGenerator (});} public int getNextKey () {return keyinfo.getNextKey ();}} KeyInfo is the same as the single-class class, the following is the client code:
Public class client {private static keygenerator keygen; public static void main (string [] args) {keygen = keygenerator.getInstance ("po_number"); for (int i = 0; i <25; i ) {system.out.println ("Key (" (" (i 1) ") = " keygen.getnextKey ());}}}