本文是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
相关推荐
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, ...
■Chapter 3: Creational Patterns: Singleton, Factory Method, and Prototype ■Chapter 4: Creational Patterns: Abstract Factory and Builder ■Chapter 5: Structural Patterns: Adapter, Bridge, Composite,...
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....
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: ...
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. ...
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...
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....
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 ...
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 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 ...
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 ...
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....
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...
* 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 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 ...
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 ...
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 ...