• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a mock quiz about Design Pattern

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic