Effective use of abstract classes

Posted in Software on July 28th, 2009 by Jan Willem Tulp – Be the first to comment

Every now and then I run into code where use of abstract classes are not always as optimal as can be. The thing at hand here is reuse of common functionality. There are at least two common ways to reuse common functionality:

  1. calling methods in an abstract superclass
  2. calling methods that are delegated to some other class (not a superclass)

Even though both options may have the same final result, I believe that one option is better suited for some situations, and the other for other situations. Notice that when you are calling methods in an abstract superclass, the direction is up: from the subclass to the superclass. In many cases the subclass does the heavy work, and reuses some common functionality from the superclass.

Now the power of abstract super classes is to use the Template Method design pattern. Instead of doing the heavy work in subclasses, you do the heavy work in the abstract class itself, you define some abstract methods that must be implemented by subclasses. These abstract methods are the specifics that differ for each subclass. This way, the direction is more or less down: from the abstract class you want to get something from a subclass that is specific for this subclass, even though the abstract doesn’t have to be aware of the exact subclass. The subclass just has to comply with the signature of the abstract methods defined in the abstract class.

When you just need some reusable functionality, I prefer using delegation instead of using an abstract class. This way the classes become more loosely-coupled, and the class with the delegated functionality can be used by more classes than just a subclass of some abstract class.

So, recap:

  • let abstract classes do the heavy work, and have subclasses implement specifics that will be used by an abstract class
  • delegate common behavior to a separate class if you just want to reuse some functionality

Tip: a ‘code-smell’ that may indicate that you’d better use delegation instead of an abstract class: if your abstract class does not contain any abstract methods, your abstract class is probably only there so that subclasses can reuse its functionality. In that case: use delegation!

  • Share/Save/Bookmark

Technology as driver for component subdivision

Posted in Software on July 28th, 2009 by Jan Willem Tulp – Be the first to comment

One of the recurring patterns I find myself doing that successfully help in creating flexible component based application, is using technology as a driver for my component subdivisions. I usually try to create a domain model component that is completely technology agnostic. By technology agnostic I mean that my domain model should not know anything about HTTP, Web Services, LDAP, Databases, HTML, etc. It should be just an implementation of the business domain (only POJO’s or POCO’s). And usually I end up with a lot of interfaces and abstract classes in my domain model, so that components that are technology-aware are able to implement these interfaces. The important thing here is that the domain component is unaware of any technology or any of its implementors.

Then I have some technology aware components that implement the interfaces of my domain model. So for instance, I have an LDAP component that implements some interface of my domain model, using LDAP technology to fulfill it’s goals. This way the implementation of my domain becomes swappable, so that I am free to replace this component with another implementation, for instance with a SQL implementation. The power of this approach is that my domain model contains the business logic of my application, and requires some information or processing from it’s implementors, but it is unaware ‘how’ this is done. This will give you enormous freedom and flexibility.

Then usually I need final component that wires up everything together. This final component is aware of both the domain model and its implementors. This final component is responsible for wiring up the domain model with the correct implementations. A dependency injection framework could be used for this, or just hard-coding the wiring my also be an option (compile-time validation of your configuration may be a benefit here).

Another benefit of this approach is that you are creating a durable application. I have seen numerous situations where software applications are rebuilt from scratch because the legacy application is to complex to modify, extend or improve. So quite often the decision is made that it is cheaper to rebuild it than continuing working on the current application.

By creating the right dependencies, the right modules or components, you create durable software. Although the there will probably be changes in the business domain through time, in general you business domain can now outlive the implementations it uses. Upgrading to a new technology that is incompatible with the current technology you’re using for a certain implementation now only means creating a new implementation component, re-wiring the application with the new component, and everything keeps working. No need to trash the entire application.

The important principles to keep in mind:

  • create technology agnostig domain model components
  • create technology aware components that implement implement the domain
  • use a single component that wires up your entire application
  • think of composing your application from small components instead of one big moloch that does it all, so: composition
  • Share/Save/Bookmark

Cool infographic video about… butter

Posted in Uncategorized on June 22nd, 2009 by Jan Willem Tulp – 1 Comment


Butter & Marge: A Tale of Two Spreads from Outside Line on Vimeo.

  • Share/Save/Bookmark

Great cartoon!

Posted in Uncategorized on June 21st, 2009 by Jan Willem Tulp – Be the first to comment

Browser cartoon

From http://www.flickr.com/photos/robotjohnny/3629069606/sizes/o/

  • Share/Save/Bookmark

Great introduction to Info Graphic Design

Posted in Data Visualization on May 11th, 2009 by Jan Willem Tulp – Be the first to comment

At backspace.com a great introduction to Info Graphic Design is posted as PDF document. Click here to check it out!!

  • Share/Save/Bookmark

How long will it last?

Posted in Data Visualization on April 26th, 2009 by Jan Willem Tulp – 1 Comment

A few days ago New Scientist has published a great Info Graphic that illustrates the mass consumption of earth’s resources. Although the Info Graphic is great, its message is quite a scary one…

How Many Years Left

Click for a full size image…

  • Share/Save/Bookmark

Software durability labels

Posted in Software on March 31st, 2009 by Jan Willem Tulp – Be the first to comment

According to several EU directives, light bulbs, white good, cars and even homes must have energy labels. These labels indicate the energy efficiency in a range from A to G, where A is the most efficient and G is the least efficient. Yesterday I was discussing my current software project with a colleague, and we concluded that they way we’ve divided our project into components is a very flexible one. And because it is so flexible, our software has become very durable, because you can replace one component with a new one, without affecting all the others in any way, as long as the functionality of the software is not changed. And because durability is a hot environmental topic today, we thought that energy-label-like quality ratings for software would not be such a bad idea.

EU energy label

I believe that today there is no such thing as a standardized labeling system for software indicating the quality of the software.Wouldn’t it be a nice thing for customers to see that their software suppliers deliver A quality (durable) software instead of G quality (unmaintainable spaghetti code)? And I believe that one of the key factors in creating durable software is managing your dependencies well. As soon as one component becomes entangled with another one, you’ve already lost some of your flexibility.

I’ve read the book Agile Software Development, Principles, Patterns, and Practices by Robert C. Martin quite some time ago. He explains really well how to handle package or component dependencies, but it wasn’t until my current project that it really got through to me.

The following are my own set of guidelines for deciding how to separate my project into components. It works really well for me:

1. Create one or more domain components
These components should have NO dependencies on frameworks, implementations, XML, or what so ever. These components will most likely contain interfaces, and domain logic. These components should capture the (abstraction of) the functionality of your application. And most important: no dependencies to implementations or frameworks!!

2. Create well isolated implementation components
Implement your domain logic in separate components. And don’t put ALL of the implementation in 1 component. For example, if your application has a view and some sort of data storage that should be used by your domain logic, create a separate view implementation component and a separate data source component. If you would have put them in the same component, your view depends on the data source implementation which is not a good thing.

3. Don’t fall back on shortcuts, it will never help you in the end
Once you use shortcuts or the fastest solution instead of the best solution, software durability and flexibility drops, especially if you create dependencies between components. If you think “oh well, lets just implement the quickest way to pass this extra information” while this information is not related to this component, you mix up responsibilities, create unwanted dependencies, and eventually you’ll end up with a highly entangled product. So be aware of shortcuts!

4. Think: “can I replace this component without affecting something unrelated”
This is a thought I keep in my mind constantly. For our front-end application we used to have only 1 component. Now we’ve separated this application into components with their own responsibilities and their own dependencies. This increased the durability of out application immensely. As long as the functionality of our application does not change, one can decide to use a different front-end framework, while the rest of the components can remain.

5. Think composition
You’re almost always better of composing your application out of small components than creating one bulky component that does all. Being able to compose your application is the path to flexibility.

Creating a highly durable product is fun and very satisfying. Summarized I would recommend: no shortcuts, separate responsibilities, domain logic with no dependencies, application composition.

  • Share/Save/Bookmark

Get funky!!

Posted in Music on March 28th, 2009 by Jan Willem Tulp – 1 Comment

Being a piano player, I like to follow technological development of digital stage pianos. Every year at NAMM, the National Association of Music Merchants, vendors of musical instruments always show their latest products. I usually go to a music store to check out some of the new instruments I like. Although I’ve only checked out one of them, these two new products have already entered my wishlist:

- the Roland V-Piano
- the Nord Stage EX

Watch this guy play amazingly funky music on a Nord Electro 3:



  • Share/Save/Bookmark

Financial Crisis visualized

Posted in Data Visualization, Inspiration on March 18th, 2009 by Jan Willem Tulp – Be the first to comment

Here is a great visualization of the financial crisis, by Jonathan Jarvis:



The Crisis of Credit Visualized from Jonathan Jarvis on Vimeo.

And for an overview of more visualizations of the financial crisis, see one of my favorite websites: Flowing Data

  • Share/Save/Bookmark

Contemplating layering

Posted in Software on March 7th, 2009 by Jan Willem Tulp – Be the first to comment

This is going to a somewhat technical post, be warned :)

Currently I am working on a project where I have to build a web application that allows users to search a large database with user data (an LDAP database). This web application is actually composed of 2 separate applications that communicate with each other using a Web Service. One of them you can call a front-end application, which contains the web pages, the front-end logic (like sorting results) and a client for the Web Service. And you can call the other one the back-end application, which hosts the web service, the domain logic and the logic for communicating with LDAP. In this post I want to focus on layering in the back-end application.

layers

When I write software, there are always good software design principles in my head, like: keep methods short, make your code readable, seperation of concerns, minimize the side effects, etc. Now, in deciding how to package my back-end application into deployable components, I learned some great lessons from the design principles by Robert C. Martin (Uncle Bob). And especially the fact that different parts of the application may evolve at different speeds helped me to decide to define the components.

Now in this application I actually had 3 components:

  • a Web Service component that exposes the domain logic to the outside world
  • a domain component that contains all the domain logic of search for user data
  • an LDAP component that implements the domain logic using LDAP specifics

And to have maximum flexibility, I wired the dependencies like this:

  • the Web Service component only knows about the the domain component, since it’s only purpose is to expose domain logic to the outside world. The Web Service component doesn’t know anything about how the domain component does that, so it could be an LDAP database, a SQL database, text files, some over enthusiastic employee typing in responses in real-time, etc.
  • the domain component only knows about itself. It even doesn’t know anything about external frameworks
  • the LDAP component only know about the domain component, since the purpose of the LDAP component is to implement the domain logic using LDAP
  • all these components are wired using Dependency Injection, which means that these components are connected to each other by means of an external configuration file

This setup makes the design extremely flexible, and these components now can evolve separately from each other without breaking (or just re-deploying) the other components. It also allows for swapping components, like using a REST Web Service instead of a SOAP Web Service to expose the domain logic to the outside world without touching the other ones. And using a SQL database instead of the current LDAP database without touching the other components.

Though I am quite content with this separation of concerns, this flexibility, and this independence, yesterday I ran into a point where I decided to create another component, namely, a specific LDAP component. The reason for this is the following: the current LDAP database we’re using contains inconsistent data. There are many parties responsible for delivering their own data to this LDAP database, without complying to certain standards (at least they appear not to be strict enough). The result is that the LDAP database is cluttered with attributes that are filled for one person, and left out for another person. In order to deal with these inconsistencies, I had to write some logic that deals with this.

But the point is this: this logic that deals with these specific inconsistencies, is not representative for all LDAP implementations. You can see this kind of logic as some sort of anti-corruption layer. The general purpose LDAP logic is now located in the other LDAP component, and specifics for dealing with inconsistent data is now located in this new LDAP component. With regards to flexibility this decision means that you can swap from one LDAP implementation to another without touching the original LDAP component. Or it could also mean that if the organization manages to fix the data so that it is consistent again, this new LDAP component is the only one that needs to be modified (or it may even become obsolete).

Even though I am quite satisfied with this decision, more components also means a need for more decisions: “where should I put this piece of logic, is it general purpose LDAP or is it specific LDAP?”. The direction of my thinking is to make the general purpose LDAP component implement the domain logic as much as possible, and then only put the exceptions in the specific LDAP component.

Anyway, it’s really nice to think about layering, responsibilities and dependencies, and trying to make it as flexible and independent as possible.

  • Share/Save/Bookmark