«« Seeing the Forest for the Tests Thinking About the Sweet Spot »»
blog header image
Does Java Need a Higher Level Pattern Validation?

After reading this interview with Charles Simonyi, I wondered: What if there was a design pattern language built on top of Java? Java code is verified by the Java compiler and maybe as it is written in an IDE like Eclipse. The Java programming language is quite flexible and generally portable.

If you wanted an object to represent part of a design pattern, couldn't you do it in a language built on top of Java which would verify that the class was properly structured to represent that pattern after it compiled correctly? In a regular or legacy Java compiler these high language directives would be ignored.

Simple examples would be immutable objects and singletons. For immutable objects, the design pattern verifier would ensure that all of the members variables were private final, and that they were set only by a constructor and had the proper get accessor methods. Otherwise, you'd get warning or error. For singleton classes it would ensure that the constructor was private, there was a private static member variable representing the singleton in the class and that a static getInstance() method was present.

The logical extension of this system would then be wizards that pump out immutable objects and singletons, based on a few pieces of information. The design pattern verifier enforces the design pattern contract you are trying to use, or at the very least warns you when you go astray.

It might also make it easier to refactor one design pattern to another: going from a singleton class to a factory class, for example.

Some people might be saying "so what? I can already write my patterns in Java". You're right, you can. The power comes from the machine verification of the design pattern. If the patterns are rigidly enforced, quality of the code will improve because you're no longer breaking basic pattern contracts and abstractions needed to assemble larger systems. However, building it on top of Java ensures that the parts of the patterns that need implementing can be done in a flexible way and in a language that many people are familiar with.

Example of a pattern-enforcing Javadoc comment

What would a "language" like this look like? Well obviously it would have to be in Java comments so that it could be safely ignored by standard issue Java compilers. It might look something like Javadoc. Who's going to build it? Do we wait for Sun to step up to the plate or start a community effort?

Posted at February 20, 2004 at 10:16 AM EST
Last updated February 20, 2004 at 10:16 AM EST
Comments

So CodePro
http://www.instantiations.com/codepro/ws/docs/express.html
already contains a tool for emitting patterns.

But that's different from storing and emitting the patterns. At worst, I could picture attaching meta-information in a javadoc comment at the end of the file:

/**

*/

I think I would prefer something like XML, so that you can parameterize it... after all, sometimes you want "a Factory of Five Wuzzits", which means that both Five and Wuzzit need to be definable.

Alternately, I guess, you could do a CPP-style preprocessor.

WussitFactory.jpp {
FACTORY( FIVE, WUZZIT )
}

expands into the full pattern for the compiler. Is that desirable? I'm not sure.

Danyel

» Posted by: Danyel Fisher at February 20, 2004 05:46 PM

Yeah, my emphasis was more on pattern validation than generation. Generating general skeleton code with hooks for a pattern would be a fairly simple task -- and it's very easy to break this code once you start playing with it and implementing the hooks. Nothing prevents you from destroying the skeleton.

Instead the validation would take the pattern you want to do and look for 1) code that violates the pattern or 2) code that is missing to satisfy the pattern. The validation would be a harder to implement but it would also keep the programmer or software engineer on track with the pattern they ultimately want to produce, keeping important contracts in place for larger design considerations that depend on patterns.

That's a good point about parameterizing, though I'm not sure if it belongs in the pattern itself. Maybe it could be a constant or a passed parameter. For example you might want a thread pool of 12 threads to be initialized, which is sort of factory-like. But later on you might want to change the pool to 24 or something when your server requirements change. The pattern hasn't changed though, just the number of threads or wuzzits.

» Posted by: Ryan at February 20, 2004 09:06 PM

Incidently, I did have something in the javadoc involving angle brackets...

(pattern type = "factory")
... (count/ number = "five")
... (type/ class = "wuzzit")
(/pattern)

I see what you mean about validation. Perhaps a different model is to look at VEP
http://www.eclipse.org/vep/
which manages excatly what you're talking about. It knows you have a JFrame with a JComponent inside it. Of course, it knows that ... because it runs it. A VM instance runs the code and sees what comes up.

Could we go one step further, and actually extract whether it's still the real pattern or not? I'm not sure.

The other thing that comes to mind is that with JDK 1.5 and some clever reflection, it might be possible to make pattern abstract classes.

ThingFactory{X}

gives you a bare XFactory. (Those are angle brackets around the X.)

public MyXFactory extends ThingFactory {
.. public froble() {
.. }
}

gives you an XFactory that also frobles.

» Posted by: Danyel Fisher at February 21, 2004 03:08 PM

Isn't the VEP for GUI builders?

Something I forgot was namespaces.

For example, I could use my own org.audioman.Immutable but Sun may have their own com.sun.Immutable or another organization. Then the validator would know which thing to validate.

Most importantly if the patterns were standardized somewhere programmers/architects could trust they'd follow the same rules every time, rather than have to create custom patterns every time. Custom patterns would be relatively rare.

» Posted by: Ryan at February 23, 2004 10:25 AM
Google
 
Search scope: Web ryanlowe.ca