The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

protected constructors(?) and group loading

I have a class that I don't want anyone to just be able to instantiate.  They should be forced to use a factory if they want an instance of one.(because, if you do create a new one, and it [as in an entity] already exists in the factory's repository, there will be big problems).  BUT, i also want to be able to load them all at once. Which means, I can't just have my usual method in my DataAccess class that loops through a ResultSet, creates a new Object, and puts it into teh factory's ArrayList or whatnot.  So, what should I do?  Is it worth having to load each object individually for the sake of "proper" design and security?  Or should I say screw it, allow for public constructors and hope no one uses the class wrong?  Is there a magic design pattern that will solve my problem? thanks for advice in advance.
Wednesday, September 08, 2004
Loading and creating are different in the way they handle identity: loading assumes and existing object identity, creation needs to generate a new id. I'll just assume loading.

Why not have 2 factory methods? One for single instance loading, the other for bulk loading. Constructor can be private.

static public Object load(final Identity id);
static public Object[] load(final Identity[] ids);
static public Object[] load(final String criteria);


Superclass (since your constructors are only protected) and provide concrete creation logic. Provide to methods load single and multiple.

interface ClassFactory {
public Object load(final Identity id);
public Object[] load(final Identity[] ids);
public Object[] load(final String criteria);

class MyFactory implements ClassFactory {

class Client {
void foo(final ClassFactory cf) {
  try {
      MyObject = cf.load("000"));
  } catch (ClassCastException cce) {
      // wrong class factory; handle the exception;
Wednesday, September 08, 2004

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz