Frameworks consist of a number of classes working together, containing some reusable logic, be it presentation-, business- or persistence-logic. In this Blog I will present an example framework that builds homes of any kind, in other words, the framework's logic is "building houses".
As you may guess, the classes working together here will be walls, roofs, doors and so on. To be able to build homes of any type, any new
operator in the framework must be encapsulated into a protected
factory method. Only that way the framework can be customized and reused entirely. (Exception is the allocation of standard data types like Integer, String, Boolean
etc.)
The encapsulated home-building business logic is
Homes can be houses, tents, wooden huts, ... all of them will be built the same way.
So here is that logic in an abstract
class. To make it short, I left out the imports
and JavaDoc (mind that you always should write documentation for public
and protected
classes, methods and fields!).
1 | public abstract class Home |
These are the factory methods, creating walls and roof:
protected abstract Wall newWall(int number);
protected abstract Roof newRoof();
Here comes an abstract wall for that home.
1 | public abstract class Wall |
Any wall could have a door, modelled as inner interface. Again a factory method is responsible for creating it, in case setDoor()
gets called:
protected abstract Door newDoor();
Here comes the roof, as interface, so that walls also could implement roofs, useful for tents.
1 | public interface Roof |
We could set a material
property into this, to show the roof's reliability.
Until now we just got abstract classes and interfaces. Not a very good shelter, time to get concrete :-)
1 | public class House extends Home |
Just the factory methods are implemented, and the according classes. Very short and concise.
1 | public class BrickWall extends Wall |
This wall is made of bricks and got a wooden door. The following roof is covered with tiles.
1 | public class TiledRoof implements Roof |
Test code:
1 | public class Demo |
Output is:
==== Starting to build House ====
Erecting wall BrickWall 1
Erecting wall BrickWall 2
Erecting wall BrickWall 3
Erecting wall BrickWall 4
Building WoodenDoor into BrickWall 1
Covering with TiledRoof: [BrickWall 1, BrickWall 2, BrickWall 3, BrickWall 4]
==== Finished building House ====
With this house we are ready for the rainy season :-)
In summer we prefer to enjoy fresh air by living in a tent.
1 | public class Tent extends Home |
A wall is used also as roof of the tent. There is no separate door, it is cut into the wall, thus the setDoor()
override.
1 | public class TextileWall extends Wall implements Roof |
Test code:
1 | public class Demo |
This outputs:
==== Starting to build Tent ====
Erecting wall TextileWall 1
Erecting wall TextileWall 2
Erecting wall TextileWall 3
Erecting wall TextileWall 4
Cutting a zipper door into TextileWall 1
Covering with TextileWall 0: [TextileWall 1, TextileWall 2, TextileWall 3, TextileWall 4]
==== Finished building Tent ====
What can be done with named classes can also be done using anonymous classes. Following example shows the real power of overriding.
Thanks to factory-methods you can customize the framework down to any level, here down to the wall's door. Mind how overrides nest into other overrides. Method overrides contain anonymous class overrides, that again contain method overrides.
Might be a little hard to read, but this is the framework style. Summit of OO in my opinion, because you can reuse not just one class but a whole set of them.
1 | public class Demo |
Output is:
==== Starting to build Phantasy Home ====
Erecting wall Phantasy Wall 1
Erecting wall Phantasy Wall 2
Erecting wall Phantasy Wall 3
Erecting wall Phantasy Wall 4
Building Phantasy Door into Phantasy Wall 1
Covering with Phantasy Roof: [Phantasy Wall 1, Phantasy Wall 2, Phantasy Wall 3, Phantasy Wall 4]
==== Finished building Phantasy Home ====
So, isn't frameworking an alternative to networking :-?
You just need to state what you want, the framework will call you and make something of whatever you return.
ɔ⃝ Fritz Ritzberger, 2016-11-27