Interface Inheritance versus Implementation Inheritance
In the first case, we are referring to a class inheriting
from an interface (or in VB terms, implementing the interface). In the second
case, we mean a class inheriting from a super-class.
Classes having knowledge of other classes
When one class knows about another class, it simply means
that it holds a reference to the other class and its methods and properties.
Let us look at it from another angle. When coding a class
which knows another class, you have to know exactly which class it will need to
use, the name of the class, etc. You may also need to know its internal
workings. Obviously, it translates into higher coupling since a change to the
referred class can mean a change the referee class.
Wide Interfaces
When a class interacts with, accesses and/or modifies
another class through a wide interface, it means that it has intimacy with that
other class. It means that the "interactor" has wide knowledge of
the "interactee" and is able to get its work done through that wide
knowledge. A wide interface means that the class knows all the methods and
member variables which are available in order to manipulate them intimately. We
see this type of interaction between director and concrete-builder in the
Builder-pattern.
Obviously, this not only breaks encapsulation, but it also
greatly increases coupling. It is somewhat similar to the wife who knows every
little detail about the husband and how he thinks. She knows just the right
words to say and the little looks to drop to get him to do something. She is
interacting through a wide interface since she can reach across a wide set of
psychological handles that affect his behaviour and the action she desires. She
knows the exact way to angle her face, remove a lock of hair and crease her
lips to stimulate just the right part of the man’s psyche into action. I do
not say this is a bad thing; in fact, it is very necessary. Men are lazy and
need pushing and prodding. It is simply an example of a wide interface.
Interaction through a wide interface makes for stability in
marriage since a wife and husband are the hardest people to replace, their
interactions being so complex, multi-faceted and fine-tuned. In the average
circumstances, it gives children a stable environment in which to grow up and
enjoy the input of both parties in their development. So in fact, society
benefits from the fact that wives interact with their husbands through wide
interfaces.
Narrow Interfaces
A narrow interface is the interface through which most
classes would normally interact with another class. It normally consists of
the interaction with properties and methods in a way that assumes a minimum of
knowledge about the class, possibly just a single method name to invoke.
Aggregation versus acquaintance
Do not fall into the trap that I did when I confused
aggregation with the use of a data-structure which contains several references to
the aggregatee. Aggregation has nothing to do with numbers and may involve one
aggregatee or many. So it may in fact be through a data-structure, but more
times than not through a single object reference.
So what is it? Aggregation means that the aggregator and
aggregatee are bound in such a way that the useful object lifetime of the
aggregatee is also the useful lifetime of the aggregator. For as long as the
aggregator has useful work to do, it needs the aggregatee to do so.
So think object lifetime and not numbers. Due to the link
between their useful lifetimes, it becomes useful for the aggregator’s
constructor method to include the instantiation of a new aggregatee-helper
class at the same time. The destructor of the helper class is also tied to
that of the main class.
For on-line browsing the web-browser needs to have a
reference to a live internet connection and so the browser aggregates the
internet-connection. Therefore we see the aggregator/aggregatee principle in
the web browsers’ connection settings where the activation of the browser can
be tied to the activation of the connection in the settings below.
Listing 1: Internet Options showing aggregator
principle
Launching Internet explorer (in the absence of a network
internet connection) automatically brings up the following.
Listing 2: Web browser instantiating a connection
Similarly, when there is no web browser to send useful work
through the internet connection, there is no point in keeping it alive and we
can configure dial-up connections to reflect this with the following dialog. We
configure the connection to be disposed of with the browser by setting the
number of minutes we should allow the connection to be idle before
disconnecting it.
Listing 3: Disposing connections when the browser
shuts down
In the Builder pattern we can see a director as the
aggregator and the concrete-builder as the aggregatee.
Acquaintanceship
Acquaintanceship is when there is a reference to an
acquaintance class. However, the usefulness of the main class is not tied to
the usefulness of the acquaintance. It used to be that someone’s heart
dictated the number of years they could live. If your ticker gave up, you were
forced to do the same. Thankfully, this is not so anymore. Your heart is now a
helper object, not the main object. You are the one with the relationship to
your kids, spouse, co-workers, community, clubs, country and God. Your heart
is here to help you to keep on living; you are not here to live just because
your heart decides to continue beating.
Medical science recognizes this and has made dramatic and
far reaching progress in cardiac medicine. We have valve replacement,
bypasses, pace-makers and, in the most extreme circumstances, heart
transplants. All these techniques mean that you now have an (albeit very
strong) acquaintance with your heart. Their lifetimes are no longer
inextricably linked in an aggregator/aggregatee relationship.
Acquaintance is more typical in the bridge pattern.
Factory-Classes
In the creational patterns, factory classes perform
instantiation services (return new product classes) on behalf of client code. They
make the decision about which class should be instantiated based on the needs
communicated by the client code. This allows clients to focus on the main work
they are supposed to perform without worrying about how to procure the
tool-classes they need. In the same way you do not want to go to work everyday
and worry about how they supply drinking water, clean air and office supplies
at your office, the factory classes free and decouple the main classes from the
tedium of instantiating other classes.
Product-Classes
The classes which are instantiated by factory classes and
returned to the client are called product-classes. They are usually returned
not as an instance of the class they are defined as, but as the interface they
implement. This ensures that they are interchangeable and, regardless of the
actual product class returned, it works seamlessly with the client.