Constructor Object

This is a JavaIdiom rather than a proper pattern, as it uses dynamic loading. It is closely related to the AbstractFactoryPattern; in fact it's a special case of it.

Originally by TomAnderson, in violation of BuschmannsLaw and the RuleOfThree. Using CanonicalForm.

Name

Constructor Object.

(no aliases known)

Context

You have dynamically loaded a class ("the implementing class") which conforms to a statically known interface ("the main interface"). You need to be able to create instances of the implementing class; this calls for an abstract factory.

Problem

How do you create the abstract factory which matches the implementing class?

Forces

Solution

Define an inner 'constructor' abstract class in the main polymorphic interface, which defines methods for creating instances of the main interface (as it were). In each implementing class, define a static inner class with a standard name which extends the constructor class and has a no-args constructor. The constructor objects thus serve as lightweight factories which can be obtained in a simple manner.

Example

/* Consider a drawing package, where we have Drawings and Tools which can be applied to them. We want to be able to load Tools dynamically.
 public class Drawing
 {
 	// details not important here
 }

public interface Tool { public String getName() ; public void apply( int x, int y ) ; public static abstract class Constructor { public static final String NAME = "Constructor" ; public Tool create( Drawing d ) ; } }

public class SpraycanTool implements Tool { private Drawing d ; public SpraycanTool( Drawing d ) { this.d = d ; } public String getName() { return "Spraycan" ; } public void apply( int x, int y ) { // do spraying here } public static class Constructor extendsTool.Constructor { // no-args constructor! public Tool create( Drawing d ) { return new SpraycanTool( d ) ; } } }

// client code

public Tool createTool( String clName, Drawing d ) { /* // minor gripe; what i want to do is: Class cl = Class.forName( clName ) ; Class ccl = cl.getClass( Tool.Constructor.NAME ) ; // but there is no such method (in 1.3, at least) */ String cclName = clName + '$' + Tool.Constructor.NAME ; Class ccl = Class.forName( cclName ) ; Tool.Constructor ctor = (Tool.Constructor)ccl.newInstance() ; Tool t = ctor.create( d ) ; return t ; }

Resulting Context

Tool.Constructor could have some convenience methods as follow:

 public static Tool createConstructor( String clName )
 {
 	String cclName = clName + '$' + Tool.Constructor.NAME ;
 	Class ccl = Class.forName( cclName ) ;
 	Tool.Constructor ctor = (Tool.Constructor)ccl.newInstance() ;
 	return ctor ;
 }

public static Tool createTool( String clName, Drawing d ) { Tool.Constructor ctor = createConstructor( clName ) ; Tool t = ctor.create( d ) ; return t ; }

Rationale

The AbstractFactoryPattern is the most elegant way to handle object creation in this situation; this idiom is just a simple way of getting hold of a factory.

What i mean is, "it just seems obviously right to me", which is either a very good or a very bad reason. Hmm.

Known Uses

Related patterns


EditText of this page (last edited July 24, 2012) or FindPage with title or text search