`

Design Patterns:Singleton in Java code

阅读更多

本文是java简单实现singleton,用代码体会它

 

什么是singleton?

在new之前,需要判断是否存在实例(如果存在,返回它;如果不存在,生成实例)。如:

	static public Singleton instance() {
		if (null == _instance) {
			_instance = new Singleton();
		}
		return _instance;
	}

	static private Singleton _instance = null;

 

singleton有哪些用途?

比如统计网站访问量,数据库连接池。就是能使用它特性的地方(废话)。

 

以下是代码:

PatternSingleton:          singleton类

PatternSingletonTest: singleton类的测试

 

/**
 * A utility class of which at most one instance can exist per VM.
 * 
 * Use Singleton.instance() to access this instance.
 * From: http://radio.weblogs.com/0122027
 * /stories/2003/10/20/implementingTheSingletonPatternInJava.html
 */

public class PatternSingleton {

	/**
	 * A handle to the unique Singleton instance.
	 */
	static private PatternSingleton _instance = null;
	private int count;

	/**
	 * The constructor could be made private to prevent others from
	 * instantiating this class. But this would also make it impossible to
	 * create instances of PatternSingleton subclasses.
	 */
	protected PatternSingleton() {
		count=0;
	}

	/**
	 * @return The unique instance of this class.
	 */
	static public PatternSingleton instance() {
		if (null == _instance) {
			_instance = new PatternSingleton();
		}
		return _instance;
	}

	// . ...additional methods omitted...
	public int getCount(){
		return count;
	}

	public void setCount(int cou){
		count=cou;
	}
	
	public void addCount(){
		count++;
	}
	
}

 

public class PatternSingletonTest {

	public static void main(String args[]){
		PatternSingleton test = PatternSingleton.instance();
		printCount(test,"Original instance ");
		test.addCount();
		printCount(test,"Original instance add");
		test.setCount(10);
		printCount(test,"Original instance set");
		
		//another instance
		PatternSingleton test1 = PatternSingleton.instance();
		printCount(test1,"Another  instance");
		test1.setCount(5);
		printCount(test,"Another  instance set");
		
		printCount(test,"Original instance set");
		
		//reset
	}
	
	public static void printCount(PatternSingleton test,String str1){
		String str=str1+"\tThe count is :"+test.getCount();
		System.out.println(str);
	}

}

 

 

更多资源:

单例模式的一个疑问

http://www.ibm.com/developerworks/cn/java/designpattern/singleton/

http://www.javaworld.com/columns/jw-java-design-patterns-index.shtml

http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html?page=1

分享到:
评论

相关推荐

    Programming.in.the.Large.with.Design.Patterns

    It starts with a general introduction to all types of programming patterns and goes on to describe 10 of the most popular design patterns in detail: Singleton, Iterator, Adapter, Decorator, State, ...

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers.pdf

    ■Chapter 3: Creational Patterns: Singleton, Factory Method, and Prototype ■Chapter 4: Creational Patterns: Abstract Factory and Builder ■Chapter 5: Structural Patterns: Adapter, Bridge, Composite,...

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    Apress.Pro.Design.Patterns.in.Swift

    Pro Design Patterns in Swift brings design patterns to life and shows you how to bring structure and scale to your Swift code with a practical, no-nonsense approach. In this book, you’ll learn: ...

    Modern C++ Design Generic Programming and Design Patterns Applied.pdf

    Alexandrescu offers a cutting-edge approach to design that unites design patterns, generic programming, and C++, enabling programmers to achieve expressive, flexible, and highly reusable code. ...

    Design.Patterns.Explained.Simply

    Because using design patterns will allow you to get your tasks done twice as fast, to write better code and to create efficient and reliable software architecture. How do I become a programming ninja...

    Learning Python Design Patterns 2nd 2016第2版 无水印pdf 0分

    As you progress through the book, you will learn about Singleton patterns, Factory patterns, and Facade patterns in detail. After this, we'll look at how to control object access with proxy patterns....

    Head First Design Patterns

    You also want to learn how patterns are used in the Java API, and how to exploit Java's built-in pattern support in your own code. You want to learn the real OO design principles and why everything ...

    Head First Design Patterns 英文原版

    You also want to learn how patterns are used in the Java API, and how to exploit Java’s built-in pattern support in your own code. You want to learn the real OO design principles and why everything...

    Cocoa Design Patterns

    Cocoa Design Patterns painstakingly isolates 28 design patterns, accompanied with real-world examples and sample code you can apply to your applications today. The book wraps up with coverage of Core...

    设计模式解析习题解答,已经做好目录标签

    Design Patterns Explained - Review Questions and Answers Table of Contents • Chapter 1: The Object-Oriented Paradigm • Chapter 2: The UML - The Unified Modeling Language • Chapter 3: A Problem ...

    C++.Design.Patterns.and.Derivatives.Pricing

    Design patterns are the cutting-edge paradigm for programming in object-oriented languages. Here they are discussed, for the first time in a book, in the context of implementing financial models in ...

    精通Objective-C设计模式 源代码

    Pro Objective-C Design Patterns for iOS will teach you those design patterns that have always been present at some level in your code, but were never recognized, acknowledged, or fully utilized....

    Quality Code: Software Testing Principles, Practices, and Patterns

    Now, in Quality Code: Software Testing Principles, Practices, and Patterns, Stephen Vance builds on all that’s been learned about test-driven development, helping you achieve unprecedented levels of...

    JavaScript Patterns

    * Study sample JavaScript approaches to common design patterns such as Singleton, Factory, Decorator, and more * Examine patterns that apply specifically to the client-side browser environment

    JavaScript Best Practice

    JavaScript Design Patterns: The Singleton by Samier Saeed JavaScript Object Creation: Patterns and Best Practices by Jeff Mott Best Practices for Using Modern JavaScript Syntax by M. David Green Flow ...

    Benning.Game.Programming.Patterns

    You'll dive deep into how scripting engines encode behavior, how quadtrees and other spatial partitions optimize your engine, and how other classic design patterns can be used in games. Table of ...

    Javascript.Object.Oriented.Programming.pdf

    Identify and apply the most common design patterns such as Singleton, Factory, Observer, Model-View-Controller, and Mediator Patterns Design applications using a modular architecture based on SOLID ...

Global site tag (gtag.js) - Google Analytics