Learn how to effectively split apart large interfaces in an effective and sensible way.
Join the DZone community and get the full member experience.Join For Free
programmers easily spot bloated interfaces, and usually carry with them an assortment of "knives and stabbing weapons" for just such encounters. a previous post presented an interface-efficiency equation and demonstrated an algorithm - fueled by this equation - to guide this butchery.
a trickier problem to spot, however, is when the members of a family of interfaces have been cut so small that a skillful re-combination might offer design benefits.
put another way: if a collection of small interfaces spontaneously coalesced back into one large interface, and the programmer had to split that large interface, would the same small interfaces reappear? if they do, then those small interfaces have retained their claim to independence. if they do not, then this might suggest an over-segregating of interfaces and an unmerited allocation of behavior among those interfaces.
let us take a look at a recently reviewed program, apache lucene , to see how successfully its interfaces have been segregated when considered as related collections. here, we will assume that interfaces within the same package are, "related."
figure 1 shows the 6 interfaces in lucene's org.apache.lucene.search.spans package, which contain a total of 25 methods (this analysis makes no distinction between interfaces and abstract classes).
figure 1: interfaces in lucene's spans package.
we shall gather all these methods into a single interface and decompose that interface based entirely on objective interface efficiency calculations.
(recall that if class a is a client of interface i , and i has 10 methods of which a calls 10, then i is 100% efficient with respect to a . if a only uses 3 of the methods, then i is only 30% efficient. if a second class b uses 6 of the methods, then i 's efficiency is the average for both clients = (30% + 60%) / 2 = 45%.)
figure 2 shows the resulting hypothetical reallocation of methods among the freshly segregated interfaces using the algorithm introduced in the previous post .
figure 2: lucene's spans package interfaces re-imagined.
the re-allocated interfaces of figure 2 have largely retained their integrity and only one has disappeared. the greatest impact is the combining of interfaces conjunctionspans and spans into interface 2 , indicating that clients use both interfaces together, but there seems little wrong in separating these interfaces, as they are in figure 1. these interfaces thus justify their current configuration.
if we look at another lucene package, however, we see a different story. package org.apache.lucene.analysis.tokenattributes contains 9 interfaces of 23 methods, see figure 3.
figure 3: interfaces in lucene's tokenattributes package.
if the interfaces of figure 3 are combined and then our algorithm used to split this large interface into an efficient collection, we arrive at figure 4.
figure 4: lucene's tokenattributes package interfaces re-imagined.
figure 4 has reduced the collection from 9 to just 4 interfaces. interface 1 contains largely the chartermattribute interface with minor additions, and interface 3 is a combination of two small interfaces. interface 2 , however, has amalgamated 4 entire interfaces into one, suggesting that - from an efficiency point of view alone - the interface collection merits further investigation.
of course, programmers segregate interfaces for more reasons than just interface efficiency: it may be that the smaller interfaces reflect different implementations that may be combined in various forms, or that their semantic distinctness justifies the separation.
this is, furthermore, merely a static code analysis, and static analyses never answer design questions: they only pose questions. nevertheless, the question posed here is clear: what motivates the splitting of the methods of interface 2 in the current code-base?
the interface segregation principle advises not to break large interfaces into smaller ones, but to break large inefficient interfaces into smaller efficient ones. if all twenty client classes call all sixty methods of an interface (admittedly something of a rarity in modern software systems), then that interface is well designed and should not be decomposed.
small interfaces are a pragmatic compromise, but maximally efficient large interfaces are the goal.
over-segregating interfaces can result in interface-fragments that do more to cloud, rather than clarify, design intent.
Published at DZone with permission of Edmund Kirwan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.