Awesome Special Case Patterns

I have found some more usages of “special case pattern” coined by Martin Fowler and I have tried to name them rhetorically to make you remember – dummy, dead, and deformed.

If you are not familiar with “special case pattern” please read Writing Effective “Special Case Pattern” and “Null Object Pattern” first. This is a architectural pattern popularized by one of my idols, Martin Fowler.

1. Dummy Objects

This one is specially helpful for Desktop applications. Let’s say we need to bind an object to a view for which no record in the database exists YET. To keep the coding pattern consistent, I may often create a special object having the same parent as the data object or extend it. This one feels like Null Object Pattern, too. This special object serves a few purposes

  1. Displaying instructional data in object fields with localized texts which helps as hints in modern forms. In a new user object the name field can have “Write your name here”.
  2. It’s easy to understand that this is a new object as it won’t have any entity ID. That’s better than creating an actual record for which you will need to take more steps to identify if this object or record is incomplete. In addition, it’s helpful if you just save the object in session so that the user can come back later and start from where he left.

So, a dummy user object for user registration may look like this:

public class RegistrationUser extends User {

    public long getId() {

        return 0;


    public RegistrationUser() {

        this.setName( "Write your name here" );
        // or Localisation.get("Write your name here" )




Or you can use it for saving form data, incomplete input, unvalidated data, dangerous data, expendable data, or whatever-data-which-you-are-not-ready-to-save-yet.

2. Dead Objects

Let’s say, you want to allow users to recover deleted data in last one hour. Simpler solution may be storing them in a repository that saves deleted objects in memory. But when you have an application where this “just-in-death” information will be useful before and after restoring, a Dead object pattern might save a lot of time and keep the code reusable. Think of a business logic like this “save the object, and if it was deleted before, fine the user 10 bucks

I would declare a new class like “DeletedMessage” which extends the Message class. I could event raise events depending on the object class if I am doing too much RxJava.

3. Deformed objects

This one is my favorite and I usually do it effortlessly. When I need to change some values for a purpose, especially computing derived values, or just hide a few fields for security/access-level reasons, I create projections. And sometimes it’s more effective to create projections by special case pattern:

Let’s say I have only firstName and lastName in my actual user class. But often I need the full name from frequently accessed user objects. If I create a separate projection class I would not be able to use a lot of functionality written for user objects before. Like a list of online users which has already be written. So, here is what I can do:

public class OnlineUser extends User {

    String cachedFullName = null;

    public String getFullName() {

        if ( null == cachedFullName )
            cachedFullName = getLastName() + ", " + getFirstName();

        return cachedFullName;




Seems overkill? Think of a thousand times you have created new strings for an user’s full name for thousands of views of a post where the author’s name proudly glows. If you are not convinced, write a simple application and test it with a memory profiler. You can pretty easily have thousands of your full name strings. This example is trivial, btw.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s