<< Chapter < Page | Chapter >> Page > |
Information hiding is a tried-and-true design principle that advocates hiding all implementation details of software components from the user in order to facilitate code maintenance. It was first formulated by David L. Parnas (in 1971-1972) as follows.
IList
is implemented in order to use it. The user should only program to the abstract specification.IList
will be used. The implementor should write the implementation code based solely on the abstract specification.By adhering to the above, code written by both users and implementors will have a high degree of flexibility, extensibility, interoperability and interchangeability.
The list framework that we have developed so far has failed to hide
MTList
and
NEList
, which are concrete implementations of
IList
, the abstract specification of the list structure. In many of the list algorithms that we have developed so far, we need to call on
MTList.Singleton
or the constructor of
NEList
to instantiate concrete
IList
objects. The following is another such examples.
InsertInOrder.java |
import listFW.*;
/*** Inserts an Integer into an ordered host list, assuming the host list contains
* only Integer objects.*/
public class InsertInOrder implements IListAlgo {public static final InsertInOrder Singleton = new InsertInOrder();
private InsertInOrder() {}
/*** This is easy, don't you think?
* @param inp inp[0]is an Integer to be inserted in order into host.
*/public Object emptyCase(MTList host, Object... inp) {
return new NEList(inp[0], host);
}/**
* Delegate (to recur)!* @param inp inp[0] is an Integer to be inserted in order into host.*/
public Object nonEmptyCase(NEList host, Object... inp) {int n = (Integer)inp[0];int f = (Integer)host.getFirst();
return n<f ?
new NEList(inp[0], host):
new NEList(host.getFirst(), (IList)host.getRest().execute(this, inp[0]));
}} |
Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?