---
title: "单例模式(Singleton)"
author: "langyan"
date: "2012-06-15T14:17:21Z"
tags: []
categories: ["编程语言"]
slug: "singleton"
draft: false
lastmod: "2012-06-15T14:20:33Z"
id: 1561587
url: "https://langyan.iteye.com/blog/1561587"
---

单例模式：

一.特点

 

1\. 只能有一个实例。

2\. 必须由自己创建唯一的实例

二.条件

 

    在一个系统要求一个类只有一个实例时，才应当使用单例模式

 

三.案例

 

```
package java.lang;

public class Runtime {
    private static Runtime currentRuntime = new Runtime();

   /** Don't let anyone else instantiate this class */
    private Runtime() {}

    public static Runtime getRuntime() { 
	return currentRuntime;
    }
}
```