JAVA设计模式之【单例模式】_任务管理器

任务管理器案例

1.单例类

package Singleton;

/**
 * Created by Jim on 2016/9/28.
 */
public class TaskManager {
    private static TaskManager tm = null;
    private TaskManager() {
        System.out.println("创建任务管理器");
    }

    public void displayProcesses() {
        System.out.println("显示进程");
    }

    public void displayServices() {
        System.out.println("显示服务");
    }

    public static TaskManager getInstance() {
        if (tm == null) {
            tm = new TaskManager();
        }
        return tm;
    }

}

2.客户端

package Singleton;

public class Client
{
	public static void main(String args[])
	{
         try
         {
			 TaskManager tm = TaskManager.getInstance();
             tm.displayProcesses();
             tm.displayServices();

             TaskManager tm2 = TaskManager.getInstance();
             tm2.displayProcesses();
             tm2.displayServices();
         }
         catch(Exception e)
         {
         	System.out.println(e.getMessage());
         }
	}
}

身份证办理案例

1.单例类

package Singleton;

/**
 * Created by e550 on 2016/9/29.
 */
public class IdentityCardNo {
    private static IdentityCardNo instance=null;
    private String no;

    private IdentityCardNo()
    {
    }

    public static IdentityCardNo getInstance()
    {
        if(instance==null)
        {
            System.out.println("第一次办理身份证,分配新号码!");
            instance=new IdentityCardNo();
            instance.setIdentityCardNo("No400011112222");
        }
        else
        {
            System.out.println("重复办理身份证,获取旧号码!");
        }
        return instance;
    }

    private void setIdentityCardNo(String no)
    {
        this.no=no;
    }

    public String getIdentityCardNo()
    {
        return this.no;
    }
}


2.客户端类

package Singleton;

public class Client
{
	public static void main(String args[])
	{
         try
         {
             IdentityCardNo no1,no2;
             no1=IdentityCardNo.getInstance();
             no2=IdentityCardNo.getInstance();
             System.out.println("身份证号码是否一致:" + (no1==no2));

             String str1,str2;
             str1=no1.getIdentityCardNo();
             str2=no1.getIdentityCardNo();
             System.out.println("第一次号码:" + str1);
             System.out.println("第二次号码:" + str2);
             System.out.println("内容是否相等:" + str1.equalsIgnoreCase(str2));
             System.out.println("是否是相同对象:" + (str1==str2));

         }
         catch(Exception e)
         {
         	System.out.println(e.getMessage());
         }
	}
}

执行结果:

第一次办理身份证,分配新号码!
重复办理身份证,获取旧号码!
身份证号码是否一致:true
第一次号码:No400011112222
第二次号码:No400011112222
内容是否相等:true
是否是相同对象:true

打印池

1.打印池类

package SpoolerSingleton;

public class PrintSpoolerSingleton
{
    private static PrintSpoolerSingleton instance=null; // 打印池

    private PrintSpoolerSingleton()
    {
    }

    public static PrintSpoolerSingleton getInstance() throws PrintSpoolerException
    {
        if(instance==null)
        {
            System.out.println("创建打印池!");
            instance=new PrintSpoolerSingleton();
        }
        else
        {
            throw new PrintSpoolerException("打印池正在工作中!");
        }
        return instance;
    }

    public void manageJobs()
    {
        System.out.println("管理打印任务!");
    }
}

2.异常辅助类

package SpoolerSingleton;

public class PrintSpoolerException extends Exception
{
    public PrintSpoolerException(String message)
    {
        super(message);
    }
}

3.客户端类

package SpoolerSingleton;

public class Client
{
    public static void main(String a[])
    {
        PrintSpoolerSingleton ps1,ps2;
        try
        {
            ps1=PrintSpoolerSingleton.getInstance();
            ps1.manageJobs();
        }
        catch(PrintSpoolerException e)
        {
            System.out.println(e.getMessage());
        }
        System.out.println("--------------------------");
        try
        {
            ps2=PrintSpoolerSingleton.getInstance();
            ps2.manageJobs();
        }
        catch(PrintSpoolerException e)
        {
            System.out.println(e.getMessage());
        }
    }
}


执行结果:

创建打印池!
管理打印任务!


打印池正在工作中!