- com.cleversoft.designpatterns.singleton1.Singleton.java:
- package com.cleversoft.designpatterns.singleton1;
- /** *//**
- * Hungry Singleton
- *
- */
- public class Singleton {
- private static Singleton singleton = new Singleton();
- private Singleton() {
- }
- public static Singleton getInstance() {
- return singleton;
- }
- public String demoMethod() {
- return "This is a hungry singleton demo!";
- }
- }
- //---------------------------------------------------------------------------------------------------------
- com.cleversoft.designpatterns.singleton1.Main.java:
- package com.cleversoft.designpatterns.singleton1;
- public class Main {
- /** *//**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println(Singleton.getInstance().demoMethod());
- }
- }
- //---------------------------------------------------------------------------------------------------------
- 懒汉式单例
- com.cleversoft.designpatterns.singleton2.LazySingleton.java:
- package com.cleversoft.designpatterns.singleton2;
- /** *//**
- * Lazy Singleton
- *
- */
- public class LazySingleton {
- static boolean instance_flag = false;
- private LazySingleton() {
- }
- synchronized public static LazySingleton getInstance() {
- if (!instance_flag) {
- instance_flag = true;
- return new LazySingleton();
- } else
- return null;
- }
- }
- //---------------------------------------------------------------------------------------------------------
- com.cleversoft.designpatterns.singleton2.Main.java:
- package com.cleversoft.designpatterns.singleton2;
- public class Main {
- /** *//**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- LazySingleton ls1, ls2;
- System.out.println("Start to get first Instance");
- ls1 = LazySingleton.getInstance();
- if (ls1 != null){
- System.out.println("This is the first Instance!/n");
- }
- System.out.println("Start to get second Instance");
- ls2 = LazySingleton.getInstance();
- if (ls2 != null){
- System.out.println("This is the second Instance!");
- }
- else{
- System.out.println("You can just get 1 Instance!");
- }
- }
- }
- 引用:
- >强烈建议大家不要再花时间在实现双重检查成例上
- 是这样,不必在这上面钻牛角尖,而且单例模式在实际应用中已经逐步被Ioc容器替代,实用价值不高。
- 有上限多例模式
- com.cleversoft.designpatterns.multiton.Die.java:
- package com.cleversoft.designpatterns.multiton;
- import java.util.Random;
- import java.util.Date;
- public class Die {
- private static Die die1 = new Die();
- private static Die die2 = new Die();
- private Die() {
- }
- public static Die getInstance(int whichOne) {
- if (whichOne == 1) {
- return die1;
- } else {
- return die2;
- }
- }
- public synchronized int dice() {
- Date d = new Date();
- Random r = new Random(d.getTime());
- int value = r.nextInt();
- value = Math.abs(value);
- value = value % 6;
- value += 1;
- System.out.println(value);
- return value;
- }
- }
- //---------------------------------------------------------------------------------------------------------
- com.cleversoft.designpatterns.multiton.Main.java:
- package com.cleversoft.designpatterns.multiton;
- public class Main {
- private static Die die1;
- private static Die die2;
- public static void main(String[] args) {
- die1 = Die.getInstance(1);
- die2 = Die.getInstance(2);
- die1.dice();
- die2.dice();
- }
- }
- 无上限多例模式
- 比较合适的例子就是在应用程序中的“多语言支持”和序列键生成器。