Singleton design pattern
i am a novice in java programming.suppose we create an object in static block of a class,then how to access that object outside that class.
i want to create only one object in static block and
(not in static method) and use it in another
class.how to do it.That is what I have answered. Create a static attribute, initialize it, and create a static method which returns the static attribute.
Kaj
Similar Messages
-
Code for singleton design pattern
Hi,
This is kalyan. Can u provide code for Singleton design pattern.
Thanks in Advancepublic class Singleton {
private Singleton() {
private static Singleton instance;
public Singleton getSingleton() {
if (instance == null) instance = net Singleton();
return instance;
}Does it make sense? -
Any good article on Singleton design pattern recently updated.
Hi,
Can anyone suggest me a good article on Singleton design pattern recently updated..?
Thanks in advance.Check out the following Singleton Design Pattern Tutorial
http://www.beginner-java-tutorial.com/singleton.html -
What is singleton design pattern
Hi All,
Anybody please tell me what is Singleton Design Pattern ?
What is the right definition of it and where can I get the right information about it.
Please help and reply soon
Thanks
AmitindiaNot meaning to be arrogant: check the topic "Public singleton". It explains what the singleton pattern tries to achieve and show various ways of creating singleton classes.
-
Singleton design pattern doubt
Hi All,
I have created a helper class (which is singleton) and this class is having a method which will eventually calls a web service and returns back the result in the form of VO(value object).
The number of clients accessing this helper class would be very high (approx., 10lakhs hits per hour).
Is it safe to have singleton? Please let me know. I am not having any member variables defined in the class only methods are present.
Multiple threads would be sharing methods and not the variable(as I didn't define any member variables).
Thanks in advance.
Thanks & Regards,
Girish.K
Edited by: Girish.Kgk on May 5, 2010 12:34 AMGirish.Kgk wrote:
Hi All,
I have created a helper class (which is singleton) and this class is having a method which will eventually calls a web service and returns back the result in the form of VO(value object).
The number of clients accessing this helper class would be very high (approx., 10lakhs hits per hour). I don't know how big a lakh is, but I bet that 10 lakh/hour is nothing.
Multiple threads would be sharing methods and not the variable(as I didn't define any member variables).Then thread-safety is not an issue. Thread safety only comes into play when you have multiple concurrent threads accessing shared variables, and only member variables can be shared. -
SERVICE LOCATOR ?? Is it really an interesting Design pattern??
Hi everybody,
i've a problem with the J2EE Design Pattern "Services locator" (it's a singleton).
It is said that by making use of a Service Locator we can :
- hide to the client the complexities of initial context creation, EJB home object lookup,and EJB objectre-creation.
- multiple clients can reuse the Service Locator object to reduce code complexity, provide a single point of control, and improve performance by providing a caching facility.
But i would like to understand at which side should that service locator object reside??!!??
If it is at server side then the clients need well an initial context in order to make a lookup on that object.
Conclusion :
the service locator doesn't hide the complexities of initial context!!
Furthermore the client has to perform a look-up on that service locator object!! The only advantage left is caching facility.
If it is at client side, each client needs his own services locator object
Conclusion :
multiple client don't reuse the same service locator. What's the advantage to be a singleton ???
There is certainly something that i don't understand so help me please!! Thanks.Hi Yves,
But i would like to understand at which side should
that service locator object reside??!!??
If it is at client side, each client needs his own
services locator object
Conclusion :
multiple client don't reuse the same service locator.
What's the advantage to be a singleton ???The service locator resides on the client side and is implemented as
a singleton. Since it is possible that there could be multiple
class loaders/JVMs on the client side, and therefore, multiple
instances of the "singleton" service locator. This is typical
in a distributed environment (e.g. servlets/JSPs in a web-tier
cluster using service locator). Thus service locator is not
a truly "distributed singleton" object. But, the empahsis
is to design the service locator such that it does not hold
any state that needs to be replicated across multiple
instances across different JVMs as mentioned. Thus, there
is no need for multiple clients to use the "same" service locator,
but still the benefits of implementing this pattern is realized.
By making it a singleton, and keeping it from holding state
that needs to be replicated, we realize the benefits of this pattern.
You may also want to visit the J2EE Pattern interest list
and see these relevant discussions :
Topic: Service Locator and passivation
http://archives.java.sun.com/cgi-bin/wa?A2=ind0106&L=j2eepatterns-interest&F=&S=&P=1026
Topic: Caching EJBHome interfaces
http://archives.java.sun.com/cgi-bin/wa?A2=ind0106&L=j2eepatterns-interest&F=&S=&P=9226
Topic: Using Service Locator for Data Source caching
http://archives.java.sun.com/cgi-bin/wa?A1=ind0106&L=j2eepatterns-interest#31
hope this helps,
thanks,
-deepak -
hi all,
can any one send me the Design patterns used for ABAP objects programming in ABAP. eg mvc and sample code which uses the design patterns
cheers
senthilOf course that program is not an object design pattern
(Its just a little ABAP Objects syntax demo that I wrote years ago
I don't know about a list of OO design patterns implemented in ABAP Objects.
A very simple one, a Singleton might look like this:
<b>* cl_singleton: Eager Variant
CLASS cl_singleton DEFINITION FINAL
CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS: class_constructor,
get_singleton RETURNING VALUE(singleton)
TYPE REF TO cl_singleton.
PRIVATE SECTION.
CLASS-DATA singleton TYPE REF TO cl_singleton.
ENDCLASS.
CLASS cl_singleton IMPLEMENTATION.
METHOD class_constructor.
CREATE OBJECT singleton.
ENDMETHOD.
METHOD get_singleton.
singleton = cl_singleton=>singleton.
ENDMETHOD.
ENDCLASS.
cl_singleton: Lazy Variant
CLASS cl_singleton DEFINITION FINAL
CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS get_singleton RETURNING VALUE(singleton)
TYPE REF TO cl_singleton.
PRIVATE SECTION.
CLASS-DATA singleton TYPE REF TO cl_singleton.
ENDCLASS.
CLASS cl_singleton IMPLEMENTATION.
METHOD get_singleton.
IF cl_singleton=>singleton IS INITIAL.
CREATE OBJECT cl_singleton=>singleton.
ENDIF.
singleton = cl_singleton=>singleton.
ENDMETHOD.
ENDCLASS.</b> -
Hi,
I am new to these forums.
I know that there are 4 types of J2EE Design Patterns.
1. Fundamental
2. Structural
3. Creational
4. Behavioral.
Hope that's correct. Can anyone plz tell me what's the difference between them?
Also, examples for them. ie; Under which category mentioned above, patterns like Singleton,ServiceLocator,Session facade, DAO,DTO etc fall?
Thanks in advance.BigDaddyLoveHandles wrote:
Kumaari wrote:
Please give me some explanation in your words.
Thanks in advance.In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems.
Not all software patterns are design patterns. Design patterns deal specifically with problems at the level of software design. Other kinds of patterns, such as architectural patterns, describe problems and solutions that have alternative scopes.
Better?
Brilliant. Bravo sir, bravo -
Design Patterns in Dynamic Programming
I mentioned this on a thread some time ago - that many of the GoF patterns disappear in languages such as Lisp, but didn't have the reference handy. Came across it again today so I thought I'd post the link:
http://www.norvig.com/design-patterns/
Petehi sourdi
Below are the list of Design pattern in abap .
Singleton: ensuring single class instantiation
Adapter: making class interfaces compatible
Factory: encapsulating object creation
MVC: decoupling business logic from the view
Facade: providing a simplified interface
Composite: treating individual objects and compositions uniformly
Decorator: forming a dynamic chain of components to be used as one by the client
regards
chinnaiya P -
Design Patterns in ABAP.
Hi,
I want to know about the Design Patterns in ABAP.
Regards,
Souravhi sourdi
Below are the list of Design pattern in abap .
Singleton: ensuring single class instantiation
Adapter: making class interfaces compatible
Factory: encapsulating object creation
MVC: decoupling business logic from the view
Facade: providing a simplified interface
Composite: treating individual objects and compositions uniformly
Decorator: forming a dynamic chain of components to be used as one by the client
regards
chinnaiya P -
Location manager design pattern?
What is the best design pattern for using the location manager with intermittent but time-sensitive use? My app records location each time the user creates a photo, creates a note, records audio, etc and stores it using CoreData. The problem is the location manager has to spin for a bit to get the best reading, and in that time the user could easily have navigated the UI to delete the object they've created and create a new one. In that case I'd want to interrupt any location services active for the deleted object.
Is a singleton the best pattern, keeping the active objects in an array and using @synchronize(object)...but running each in a different thread?
Or assigning the location manager as a transient property of the object and calling stopUpdatingLocation if it gets deleted?
Any thoughts? TIA...I created a singleton for this - seems to work quite well
-
Hi all,
First of all, im pretty new to design patterns so apologies for any errors. I am creating a program that maintains a large index of string values (maybe a vector or hash table) but for the sake of performance, i would like more than one instance of the application to run at once, so that they can each add to the index.
As i said, i am new to patterns, but this seems like an ideal scenario for the Singleton. Would i be right in this assumption and could you give me any advice or example code as to how i might implement it?
Thanks very much
Wraithgeorgemc wrote:
Imagist wrote:
final public class Singleton
private Singleton() { }
final private static class SingletonHolder
final private static Singleton instance = new Singleton();
final private static Singleton getInstance()
return SingletonHolder.instance;
}This is the paradigmatic way to implement a singleton in Java these days. There are other ways more commonly used, but this has the advantage of allowing lazy loading AND thread safety without incurring the performance penalty of synchronization.
Edited by: Imagist on Nov 19, 2008 9:53 AM
public class Singleton
private Singleton() { }
final public static Singleton instance = new Singleton();
}Oh look. Lazy loading, thread safety and simplicity, all in one lump. Why did you bother making the class final, anyway? The singleton pattern as prescribed by the GoF book explicitly talks about inheritanceAs far as I can tell, that's not lazy-loading. If you call Singleton.someStaticMethod(), the classloader will initialize the instance variable, causing the constructor to call. Admittedly, that doesn't happen often. I haven't looked at Bloch's book in a while, but if I remember correctly this is the method he recommended.
I made the class final because that allows more optimizations to occur at compile time. The JIT compiler usually figures stuff like that out, but in some older versions of Java it might not happen until it has been running for a while. I use older versions of Java often, so that's a good habit to be in (and it doesn't hurt anything). -
Hey,
I do wanna know if is recommendable to use a Java Design Pattern (like Singleton, Fa�ade, etc)... in a J2ME application. I mean, a colleague of mine made a j2me application using these kind of technologies, but, when he installed in his cell phone, the application occuppied much memory. So, another guy told me that due to the limited memory space we find on these devices it's better for us to use only the MVC idea. So, is it recommendable for me to use this kind of resources???
Thanks!Hi,
it is a good programming practice to use Design patterns. But too much of design patterns results in more classes which will be a problem in j2me sometimes.
Most of the MIDP classes uses Singleton and abstract design patterns -
Can any one let me know the design patterns
implemented in Cairngorm?Are you talking about Cairngorm 2 or 3 ?
Cairngorm 2:
Model View Controller
Command
Responder
Delegate
Singleton
Cairngorm 3 (on top of Parsley):
Dependency Injection
Presentation Model -
Design Patterns that are used in standard j2se/j2ee classes/interfaces
Hi All,
I am understanding following design patterns (used within standard j2se/j2ee):
Adapter
Facade
Composite
Bridge
Singleton
Observer
Mediator
Proxy
Chain of Responsibility
Flyweight
Builder
Factory Method
Abstract Factory
Prototype
Memento
Template Method
State
Strategy
Command
Interpreter
Decorator
Iterator
Visitor
I want to see if/where these design patterns are used in j2se/j2ee classes/interfaces. i am looking for few examples of standard java classes/interfaces/cases where these design patterns are used by jdk developers.
for e.g.
WindowAdapter class is an example of Adapter DP.
JOptionPane is an exmple of Facade DP.
MouseListener is an example of Observer DP.
Similarly, where can find examples of jdk classes/interfaces of the remaining DPs.
I searched a lot of books, but they explain the DPs by creating their own classes/interfaces.
I would like to see where these DPs are already utilised in std j2se/j2ee
thanks,
Madhu_1980877316 wrote:
I searched a lot of books, but they explain the DPs by creating their own classes/interfaces.
I would like to see where these DPs are already utilised in std j2se/j2eeWell, you can go through the javadocs first, they sometimes mention the pattern used.
Then you can get the sources for the jdk and go through the classes yourself, identifying the patterns.
Maybe you are looking for
-
How to pass a variable from one class to another class?
Hi, Is it possible to pass a variable from one class to another? For e.g., I need the value of int a for calculation purpose in method doB() but I get an error <identifier> expected. What does the error mean? I know, it's a very, very simple question
-
Hello! I'm new to Dreamweaver 8 as of today. Been struggling along with FrontPage for way too long! What I need to do is include a 10,000 line Excel database (auto parts) into our web site that a only few special clients can search, but NOT SEE the e
-
Is there anyway to download this through the cloud?
-
Hello, My system is crashing when running Cleaner 6.5. The Cleaner website says to 'downgrade' back to QuickTime 7.0.1 and supplies a link for that download. But I am running 7.1.2 and the downgrade only takes version 7.0.2 back to 7.0.1 Can anyone h
-
Will i be able to move from mobile me to iclould and use my iphone 3g
will i be able to move from mobile me to iclould and use my iphone 3g