-
Ideally the client knows only the service interface and need know nothing of the service
implementation. We also want the server to be entirely ignorant of the client. Ideally
the server only knows that a request has been made and it responds based on the content of
the request, having nothing to do with the client's details.
This is the Principle of Least Knowledge (LKP)
- Consider an XML document being represented by a parse tree holding XmlNodes. The tree is a client of its nodes. We don't want the tree to have any knowledge of the various types of nodes that may be part of that representation, e.g., element node, text node, processing instruction node, etc. Thus, the tree needs a service interface that can handle uniformly each of the nodes in the XML representation. Also, we don't want an XmlNode to know anything about the tree, so it won't be passed a reference to the tree.
- We accomplish this "ignorance by design" by creating a derivation hierarchy of XmlNodes that derive from a base that provides to the tree a service interface rich enough to for the tree to carry out its obligations. We also partition the responsiblities between tree and nodes so that none of the nodes needs information about or from the tree, beyond the parameters passed in a request, to execute its assigned tasks.
- You will find an example of how this can be accomplished in Handouts/CSE687/code/XmlProcessing.
Conclusions:
The Principle of Least Knowledge requires that:
- Client callers making requests know only the calling interface.
- Service responders know nothing of the caller beyond the contents of the request.