How to Design a Java Framework? – A Simple Example

You may be curious about how a framework works? A simple framework example will be made here to demonstrate the idea of frameworks.

Goal of a Framework

First of all, why do we need a framework other than just a normal library? The goal of framework is defining a process which let developers implement certain functions based on individual requirements. In other words, framework defines the skeleton and developers fill in the flesh when using it.

The Simplest Framework

In the following example, the first 3 classes are defined as a part of the framework and the 4th class is the client code of the framework.

Main.java is the entry point of the framework. This can not be changed.

//imagine this is the entry point for a framework, it can not be changed
public class Main {
	public static void main(String[] args) {
		Human h = new Human(new Walk());
		h.doMove();		
	}
}

Move.java is the Hook. A hook is where developers can define / extend functions based on their own requirements.

public abstract class Move {
	public abstract void action();
}

Human.java is the Template, which reflects the idea of how the framework works.

public class Human {
	private Move move;
 
	public Human(Move m){
		this.move = m;
	}
 
	public void doMove(){
		this.move.action();
	}
}

This simple framework allows and requires developers to extend "Move" class. Actually, in this simple framework, action() method is the only thing developers are able to change.

Inside of the implementation, different "action" can be programmed to different purpose. E.g. the example below print "5 miles per hour", of course, you can redefine it as "50 miles per hour".

public class Walk extends Move {
 
	@Override
	public void action() {
		// TODO Auto-generated method stub
		System.out.println("5 miles per hour - it is slow!");
	}
}

Conclusion

The example here just shows how a simple Template and Hook works. A real framework is more complicated than this. Not only does it contain other relations like template-temple relation, but also very complex process about how to efficiently improve performance and programming usability.

Category >> Framework Concepts  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Ishti on 2013-11-4

    very nice

  2. Noël Widmer on 2014-2-24

    Sounds pretty nice!
    You said that the main method won’t change after deploying the framework.
    How can you know that the next developer will implement a class named “Walk”?
    (You are using the class “Walk” in your main method. But you acctually don’t know if this class will ever exist. You just assume that. Right? Or did I just mindf***ed myself?)

  3. ryanlr on 2014-2-24

    Good point. In reality, it should be configurable, e.g., in XML file. Then use reflection to load that class.

  4. Noël Widmer on 2014-2-25

    Oh, thanks!
    I completly forgott this!
    You’re right. I saw this in UDK too, thank you very much!

  5. Paul on 2014-7-30

    Excuse me for being anal, but “framework defines the skeleton and developers fill in the flash ..” should be “flesh” instead of “flash”.

  6. amir on 2014-9-24

    hey greeting guys can u plz suggest me how actually i can build my framework?

Leave a comment

*