Suppose there is an Online shopping cart application having
different objects like Cart, SelectionItem, Billing,
COnfiguration, Authentication, Authorization,
PersonalDetails etc. Out of this which one can be made a
singleton pattern and why?
Answer Posted / sabir hussain
Singleton ensure that class has only one instance and
provide a global point of access to it.
It can be applied in a situation where your implementation
is independent and utilize resource that should be
initialized once within your application environment.
1) So Configuration can be singleton as we configure
resources that are globally used with your application. (ex:
database connection info.)
2) Authentication and Authorization component can also be
singleton as there implementation are independent and will
not solve any purpose if instantiated twice, if you look in
terms of spring framework all such components are singleton.
Both the component can independently authenticate or
authorize user or user info (validating credit card info).
Cart, SelectionItem and Billing are user specific generally
session scoped, and there can be more then one session per
application, so they cant be Singleton for obvious reason.
| Is This Answer Correct ? | 16 Yes | 2 No |
Post New Answer View All Answers
Is string is a class in java?
How do you sort data in java?
What are the methods of object class ?
What is the difference between Java and C++?
What are the restrictions that are applied to the java static methods?
Can we override constructors in java?
Does anyone still use java?
How does java enable high performance?
What is the difference between JVM and JRE?
What is identifier in java?
What is a Hash Table? What are the advantages of using a hash table?
How do I get a substring?
Differentiate between a class and an object.
Explain about method local inner classes or local inner classes in java?
Name few java.lang classes introduced with java 8 ?