---
title: "常用设计模式"
author: "giraffeql"
date: "2012-11-18T17:16:54Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2012-12-22T15:37:14Z"
id: 1729054
url: "https://qlit.iteye.com/blog/1729054"
---

```java

1.单例设计模式
	(1)饿汉式
	public class Single{
		// 2.创建唯一对象
		private static Single s = new Single();
		// 1.私有构造函数，防止创建对象
		private Single(){}
		// 3.提供公有的访问方式
		public static Single getInstance(){
			return s;
		}
	}
	(2)懒汉式(延迟加载方式)
	class Single{
		private static Single s = null;
		private Single(){}
		public Static Single getInstance(){
			// 双重判断，既能保证同步，有能提高运行效率
			if(s == null){
				synchronized(Single.class){
					if(s == null){
						s = new Single();
					}
				}
			}
			return s;
		}
	}

```