A problem: VaryingImplementations and Abstractions • In a collection hierarchy, on one hand, the abstractions vary – Collection – set, queue, stack • On the other hand, their implementations may also vary – array based, linked list based
Next problem: Somesteps in an implementation can vary • In a hierarchy of classes, a behavior is quite common for all classes, but only that some steps are dependent on the nature of these classes • How to avoid redundancy in defining such behavior • Where should such a method be located?
A Problem: Separatecollection and iterations over the collection D. K. Yadav • A collection supports members to add and remove elements, to check for membership • Iterations over the collection can be specified separately such that the two abstractions are not intermingled • The iterator hides the internal implementation of the collection • Multiple ways of iteration can be supported • Iterators can be used concurrently
Next problem: Anobject alters its behavior as it changes its state D. K. Yadav • Example: – A TCP connection object provides methods such as open(), close(), send().. – The connection object changes the behavior of these methods as it changes its state from disconnected to listening to established to closed
Next Problem: Usedifferent algorithms for different situations in a given problem context: Strategy Pattern • Example: – A document is composed of text. Various line breaking algorithms can be used in formatting the document before printing • Consider the following strategies: – simple compose: determine line breaks, one line at a time – para compose: consider lines in an entire paragraph – array compose: each row has a fixed number of letters