David West nel suo libro Thinking degli oggetti (capitolo 10, sezione 1, sottosezione 2) ha proposto che in un ambiente OO ideale, ogni oggetto dovrebbe essere in grado di presentarsi su richiesta; sia per gli umani (come GUI), componenti non nativi (come JSON e / o XML), o qualsiasi altra parte interessata:
Object thinking says that a view (sometimes called an interface) —graphical or otherwise—is a means for an object to communicate with another object and nothing more. The need for a view arises when an object needs to present itself in a “non-native” form to some other object (usually a human being) or application (for example, an XML view for data objects being shared across platforms).
Discovery of the need and the parameters that must be satisfied by a view is manifest in the scenarios in which the object participates. Whenever an object is asked to display itself, it must use a view—a representation—appropriate for the sender of that display message. If, for example, an object is trying to instantiate itself (get a value for itself), it must present a view of itself as an implicit request to a human being (or other service-providing object) for a value. If we are building a GUI that will serve as an intermediary between a software object and a human object, we will use glyphs for display purposes and widgets for interaction purposes.
But which glyphs and widgets need to be included in the GUI ? Only those necessary to complete the scenario or scenarios 4 of immediate interest as the application runs. This perspective is counterintuitive for most developers because it suggests that a GUI be defined from the application out.
As an example, consider a brewery. Off to one side are vats filled with beer. At the a complex production line consisting of bottle washers, filler stations, capping machines, and package assemblers. Above it all is a control station that monitors the brewery and notifies human managers of status and problems. Traditional developers are likely to begin their analysis and design of “a brewery management system” from the point of view of the control panel. This is analogous to designing from the interface in.
Object thinking would suggest, instead, that you consider which object is the prime customer of the brewery and all its myriad machines. On whose behalf does the complex maze of equipment exist? The correct business answer is, of course, “The customer.” But an answer more reflective of object thinking is, “The beer.” All scenarios are written from the perspective of the beer, trying to get itself into a bottle, with a cap, placed in a package, and resident in a truck. The control panel is a passive observer 5 of the state of the brewery. If the beer encounters a problem at some point, it’s the responsibility of the beer to request intervention of the human operators by sending a message to the control panel (or machine-specific control panels) requesting an intervention service.
This perspective will simplify GUI design and, more important, eliminate the host of manager and controller objects that seem to inevitably arise when designing from the control panel’s ( GUI ’s) perspective.
Venendo da un principiante nel mondo OO: dovrebbe essere proprio questo il caso?
Avere oggetti che sanno come rappresentarsi sicuramente potrebbe ridurre il numero di oggetti controller / manager che West ha ripetutamente affermato nel suo libro che un Thinker degli oggetti dovrebbe cercare di evitare a tutti i costi. Ma non rispettando questa "regola" interrompi SRP ?
Inoltre (se risulta essere il caso), data un'implementazione tipica, ad esempio, in un'applicazione Android: come si può raggiungere questo tipo di obiettivo? Se ogni oggetto che creiamo sa come presentarsi come View
?