Create pattern--multi-case mode

Source: Internet
Author: User

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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.