• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a mock quiz about Design Pattern

 
Along huang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the qucstion confusing me:

You are creating a web application for an online product ordering system. You will be using data from several different databases and to reduce the load on the databases you have decided to use connection pooling. You have a ConnectionPool class and you want one pool for each database. However, you will have to connect to a variable number of databases. What pattern should you use?(Select 1 correct choice)
a) Abstract factory
b) Factory Method
c) Builder
d) Prototype
e) Singleton

the choice e) is correct, and the explanation is "The Singleton does not just create a single instance it can also be used to create a variable number of instances of a class."

If so, what structure is the ConnectionPool class? when do it create a instance linked to a database and when create another one linked to another database?
[ March 15, 2005: Message edited by: Along huang ]
 
Yong How, Lim
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Along,

Singleton is correct, every other object that need to have a DB connection need to get from this ConnectionPool object.

Take a look at this article on Singleton :
http://www.onjava.com/pub/a/onjava/2003/08/27/singleton.html

In normal situation for ConnectionPool setup, there will be several connection pre-created, so when a Connection is required, the ConnectionPool object just take the next available Connection object and pass it to the requestor.


HTH
 
Harsha Yadalam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Along,

The connectionPool (for a specific database) class is a singleton which maintains a set of connections. For e.g. these could be maintained in a hashmap instance variable.

Whenever you want a connection you must access the same connectionPool instance (singleton) which could give you a connection object from the hashmap. The singleton pattern ensures that you access the same instance always.


With respect to your message regarding the defintion of singleton

"The Singleton does not just create a single instance it can also be used to create a variable number of instances of a class."


This just implies that you could either have a single instance or a fixed number of instances based on your requirements. In this case you only have 1 instance of a connection pool for a given database.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic