Builder design pattern is useful in two cases:
- When you want to create a complex object and having one constructor with millions of parameters is not a good option (it NEVER is).
- When you want to abstract the way an object is created by using different builders depending on the required task.
These are actually two very different needs. I will focus on the first type of builder, because the second kind of builder is very similar to an Factory.
In a class diagram, a builder looks like this:
Builder works like this: first, we tell it that we want to build a new
ComplexClass, maybe passing some initial parameters. Then we call all the steps required to build that class (
initStep2, etc..) and when the class is completely initialized, we can request the instance from the builder. Using sequence diagrams:
Builder->-ComplexClass:complexClass = new()
Builder->-ComplexClass:step 1 initializations
Builder->-ComplexClass:step 2 initializations
I don't specify what is done in the initialization steps called by the
Builder. This is the actual work that the
Builder abstracts from the client. It can be a simple method call or a long set of operations, but to the client this is irrelevant.
Last week I read about something called the
Wizard design pattern, which seems an improved version of the
Builder pattern. I will dig into it and post about soon.
The Adapter (or Wrapper) design pattern is used when you want to adapt a class to an interface that it can implement.
One example where this pattern can be used is in the eclipse draw2d framwork. This framework is build on top of SWT, but this is just because of historical reasons. To work, draw2d only needs a
Canvas on which it draws. In the current implementation, there is a class that takes the canvas and wraps it into a
Canvas interface for use with draw2d. So theoretically (and practically, since this is being done in the new version of draw2d), we could take an AWT canvas and wrap it so that it can be used with draw2d. And we could do the same for a GWT canvas! cool, right? But anyway, If you didn’t get this, let’s try one more time using UML class and sequence diagrams:
The class diagram of the Adapter patterns is pretty simple. You have a client that know how to work with an existing class and you want to replace this class with a new implementation without having to change: 1) the functionality of the client (except maybe how he creates the class); and 2) Without changing the interface of your new implementation (because while I say here new, it doesn’t mean you wrote it. It simply means that it replaces the existing implementation). The result would look something like this:
As usual, the class diagram doesn’t tell us enough information to understand what is happening, so let’s look at the sequence diagram of this pattern:
note right of Adapter:
note right of Adapter:
As you can see, the adapter simply translates the parameters back and forth so the client and the implementation can communicate. That is all. No magic here .
The distance between what they teach us at the university and what happens in the real world is sometimes very large. And I can say this from my personal experience as a lecturer in an Information Systems Analysis and Design course at the Technion. I’ve taught this course for 3 semesters (including this). The course teaches model based/driven/whatever analysis and design of software systems using UML and OPM. I didn’t choose the course, it chose me (it is the course my supervisor teaches it, him being the creator of OPM). We usually divide the course in two, he teaches OPM and I teach UML.
So for the last 3 semesters I have been teaching UML to undergraduate students. And the main problem is that before I was in the academia I worked 9 years as a software engineer (programmer/architect), and almost never found UML to be of any use (except for class diagrams which are really good) Never in my 9 years of work in a big software entity did I see someone create a state-machine, sequence diagram or activity diagram before coding. Nobody though it was useful, not even management.
How can I keep a straight face in front of the students? I can because I believe that there is a problem of lack of abstraction in software development and someone needs to fix it. But is UML modeling the solution? If UML is promoted by so many academics and companies as the solution to all of our problems, then maybe I am missing something. Maybe there is some proof out there that UML does improve software development, that UML is useful.
Therefore I embarked myself in the mission to find this proof by doing a “systematical literature review” of all works that have tested the “usefulness” of UML either by experimenting with mice (i.e. undergraduate students) or in the industry. I search the main publishers of software engineering articles (IEEE, ACM, Springer) since 2005 (year UML 2.0 was introduced), read 3000+ titles and probably half the amount of abstracts to filter these articles, reducing the number of possible matches to 223 articles. Now I am fast-reading the articles to see if they are really empirical or their title/abstract was misleading. So fat 70/114 did do some empirical evaluations, but most of them between “variations” of UML. But I still haven’t found what I’m looking for, which is real evidence that UML does improve software development (there are 2 industrial projects where the usage of UML was seen as beneficial, but that is NOT proof). And I am really skeptical that I will find any proof.
WOW! This post just went in a completely different direction from the one I wanted it to take… I started writing this because of something I read a couple of weeks ago about the distance that exists between the academia and the industry in software engineering. I guess my view is like theirs, and I wanted to add my personal touch.
Oh well, back to filtering UML articles