2012年3月21日星期三

Cheap Vibram Five FingersStarting today

Starting today,to take the time to learn about some of the most common design patterns ;first learn about the singleton pattern ( Singleton ) : a single case model role is to ensure that applications throughout the life cycle, any moment ,a single case of an instance of a class is only a single case ;see below patterns of UML class diagram :look at this chart is relatively simple ,the Singleton class a private singleton object ,Beats By Dr Dre,and private construction method, and then exposed to a public getInstance ( ) method .
This method to return a singleton object .The following look directly simulation code : the first class Singleton .Class Singleton { / / use static private global variables preserved only instance of private static Singleton Singleton ;private Singleton ( ) { } / * * * here that will only be instantiated once ,is first instantiated ,then proceeds to call do not instantiate * @ return singleton * / public static Singleton getInstance ( ) {if ( null = = singleton ) {singleton = new Singleton ( ) ;} return Singleton ;} } from the code analysis, although we want to visit in two ( following the client code is to create two examples ) getInstance ( ) method ,but we return is the only instance of singleton .
The first time we went to getInstance ( ) will create an instance of singleton ,then always exists in its own class ,Lady Gaga Heartbeats,again when we call getInstance ( ) method of back judge whether existing singleton example ,when there is returned directly have previously created instance of singleton ,Monclers,so as to ensure the examples of the uniqueness of the .
Second types of Singleton :Singleton {private static .Class Singleton singleton = new Singleton ( ) ;private Singleton ( ) { } public static Singleton getInstance ( ) {return Singleton ;} } here is the client code :/ * * * implementation example model of a single test class * @ author jiangqq * * / public class SingletonTest {public static void main ( String args ) {Singleton singleton1 = Singleton.
getInstance ( ) ;Singleton singleton2 = Singleton.getInstance true ( ) ;/ / returns is the two objects have the same if ( singleton1 = = singleton2 ) {System.out.println ( " ;singleton1 and singleton2 are one and the same instance " ;} else {System.
out.println ( ) ;" ;singleton1 and singleton2 are not the same instance " ) ;} } } ;running results screenshot :the more good ,Moncler Men Vest,I compare like with the first ,but the first
one malpractice ,Cheap Vibram Five Fingers,in multiple threads ,may realize examples not only ;this time we can lock ( ) method lock congestion summarize some characteristics of single instance mode :1: any
time during the class program cycle ,there exists a unique only Examples ;2 :single cases in the class constructor is private, if I want to obtain single case examples, must go through
Related articles:

没有评论:

发表评论