`

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, ...

    《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....

    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 ...

    Mastering Python Design Patterns(pdf+epub+mobi+code_files).zip

    附带的`Mastering Python Design Patterns_code.zip`文件包含了书中所有示例代码,读者可以下载后进行实践操作,加深理解。 总的来说,《精通Python设计模式》是一本实用的指南,适合有一定Python基础,并希望提升...

    full source code of java design patterns

    Java设计模式是软件开发中的重要概念,它是一种在特定情境下解决常见问题的模板,能够帮助开发者编写可维护、可扩展且易于理解的代码。这个压缩包包含了创建模式、结构模式和行为模式的经典源代码实例,是学习和理解...

    Go Design Patterns(pdf+epub+mobi+code_files)

    6. 代码文件:提供的`Go Design Patterns_Code.zip`包含了书中实例的源代码,这将有助于读者直观地理解每个设计模式在Go语言中的具体实现和应用,通过实际操作加深理解。 7. 多格式阅读:提供epub、mobi和pdf等多种...

    Apress Pro JavaScript Design Patterns Ebook With Source Code

    《Apress Pro JavaScript Design Patterns Ebook With Source Code》是一本深入探讨JavaScript设计模式的专业书籍,它结合源代码提供了全面的学习资源,旨在帮助开发者提升JavaScript编程能力并优化代码结构。...

    Design Patterns by Tutorials in Swift 4.2, 2nd Edition_code

    1. **单例模式 (Singleton Pattern)**:在06-singleton-pattern中,可以看到如何在Swift中创建一个确保类只有一个实例的单例。单例模式常用于管理全局共享资源,如网络连接或偏好设置。 2. **策略模式 (Strategy ...

    mastering python design patterns 第二版

    1. **创建型模式**:这些模式主要涉及对象的创建,如单例模式(Singleton)、工厂方法模式(Factory Method)和抽象工厂模式(Abstract Factory)。单例模式确保一个类只有一个实例,而工厂方法和抽象工厂模式则提供...

    精通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....

    设计模式java源码-Design-Patterns-In-Java-source-code:Java设计模式学习

    "Design Patterns In Java Source Code" 是一个开源项目,旨在通过实际的Java源代码来帮助开发者理解和掌握各种设计模式。 这个项目包含了许多经典的设计模式,例如创建型模式(Singleton,Factory Method,...

    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

    Ray Wenderlich Design Patterns source code

    4. **单例模式(Singleton Pattern)**(06-singleton-pattern) 单例模式确保一个类只有一个实例,并提供一个全局访问点。在iOS中,常用于管理共享资源,如网络连接、偏好设置或者数据库连接。 5. **模型-视图-...

Global site tag (gtag.js) - Google Analytics