Design Patterns

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Introduction

Somewhere along the line someone thought it was a great idea to talk about how to write code rather than actually write code.

This book is Design Patterns. I don't recommend it.

Premise

The idea behind Design Patterns is that there are patterns to the way code is written. This isn't an unusual idea, except it violates the principle of write it once.

The fundamental problem with the authors of Design Patterns is that they fail to see the difference between Java code being compiled into Java byte code, and human thoughts being compiled to Java code.

They believe that computers should not define and iterate patterns, but human fingers typing on keyboards should. That's the fundamental issue.

Another premise is that Java is such a poor language that in order to make it useful, you have to abstract away large chunks of it behind design patterns. That is, you have to look at a thousand line chunk of code and then say, "Ahh, that's just the X pattern." In any sane programming language, the programmer would simply write, "Use the X pattern here" in about as many words.

Singleton

The most offensive design pattern, and the most commonly used, is the Singleton. The Singleton is nothing more than a global variable.

In Java, the need arises, occasionally, for a unique global instance of a class shared by all the code in the program.

This is usually to overcome some of Java's deficiencies, such as the fact that class methods (static methods) are not inherited nor is there a vtable to dispatch calls to the appropriate method.

Other languages don't have such an absurd restriction as Java.

Observer

The observer pattern is a freak of nature as well. This pattern is what the rest of the world called a "callback routine."

See, a callback is a function that is called when a certain event happens. This is a really convenient way to specify behavior that should occur.

The Observer Pattern takes that and turns it into an ugly class. Rather than dealing with functions in general, you are now dealing with a class with a peculiar interface.

The Observer Pattern, along with a whole lot of others, are simply glorified basic data types.