Becoming Familiar With Interfaces

Interfaces have always been something that I’ve struggled with but after going through Head First Design Patters, I can see I’ll be using them a lot more. I’ve learnt that I never fully understood the purpose of an interface but following along through this book has let me see just how powerful the extra layer of abstraction can be. I’ll probably also be using abstract classes and methods more often because they fill a similar role to interfaces (as well as help cut down on code duplication) and have some pretty useful implementations when paired with interfaces.

The first chapter of the book sets up a situation that is easily relatable and steps through meeting the specifications as they change. At first, the application only needs to simulate the behaviour of different types of ducks. The first solution that is proposed is to create a general class and allow the different kinds of ducks to subclass it. When new types of ducks are added, however, the design begins to show its flaws. Since each specific duck class subclasses the same superclass, some of the subclasses inherit behaviours they shouldn’t posses, like the rubber duck inheriting a fly() method.

To get around this, the authors suggest the code be refactored such that the duck superclass is an interface. This way, every subclass implements the behaviours in a way that make sense to it. This isn’t good practise though, as it leads to code duplication. To get around the code duplication, the authors introduce the Strategy Pattern. The bahaviours that change, fly() and quack() in this case, are removed from the duck class and turned into interfaces. Each interface then has concrete classes that implement them. The Duck superclass references the interfaces which are inherited by the concrete duck classes that then instantiate concrete implementations of those interfaces (e.g., the rubber duck instantiates the Squeak class).

Polymorphism also plays a big part in the design. The simulator class, the controller of the simulation, uses the superclass as the type for each duck and calls its methods. These methods then delegate to the concrete implementations that then perform the appropriate actions.

As a beginner, I could see myself falling into the exact trap that was presented before the pattern was introduced. The rest of the book covers other situations where “following your instincts” would have led you into a trap, or problem, that the creators of the pattern faced and solved.

I won’t claim that I’m now a pattern expert but I feel like the book has given me a good introduction to some basic patterns and the situations in which they are useful as well as the drawbacks some of them have if used in situations in which they are not needed.

P60702201kshortside.jpg

 
1
Kudos
 
1
Kudos

Now read this

The Not So Simple Z-Index

Web development is still a relatively new field for me. I know my basic HTML elements and structure as well as how CSS works but there are aspects of both that I have yet to learn. Z-index, I thought, was not one of them. On the surface,... Continue →