Multiple models are usually divided into: there are upper limit multiple cases, no upper limit multi-case mode.
Usually we pass new one object is the unlimited multi-instance mode.
What is called the upper limit of multiple cases, in fact, is the generalization of a single-case model , creating >1 instances (limited).
Code Description:
<span style= "FONT-SIZE:18PX;" >public class Case {//Save an instance of a finite class private static arraylist<case> caselist=new arraylist<case> ();// The number of instances created private static final int max_num=2;/* * Performs static initialization at the class initialization stage fast */static{for (int i=0;i<max_num;i++) {Caselist.add (new Case ());}} Private case () {}//defines static, class All public static case Getcase () {random random=new random (); int Count=random.nextint ( Max_num); return Caselist.get (count);}} </span>
In the above code, an instance of a finite class is created by a static initialization block, and the instance is randomly obtained through the Gecase () method.
If we do not want to specify the first instance by randomly acquiring an instance, we can refer to the following code :
<span style= "FONT-SIZE:18PX;" >public class Case {//Save an instance of a finite class private static arraylist<case> caselist=new arraylist<case> ();// The number of instances created private static final int max_num=2;/* * Performs static initialization at the class initialization stage fast */static{for (int i=0;i<max_num;i++) {Caselist.add (new Case ());}} Private case () {}//defines static, class All public static case Getcase (final int index) {if (Index<max_num&&index >=0) {return caselist.get (index);} return null;}} </span>
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/42207289 Emotional Control _
Create pattern--multi-case mode