Seuss: Better Class Responsibilities through Language-Based Dependency Injection

  • Niko Schwarz
  • Mircea Lungu
  • Oscar Nierstrasz
Part of the Lecture Notes in Computer Science book series (LNCS, volume 6705)


Unit testing is often made more difficult by the heavy use of classes as namespaces and the proliferation of static methods to encapsulate configuration code. We have analyzed the use of 120 static methods from 96 projects by categorizing them according to their responsibilities. We find that most static methods support a hodgepodge of mixed responsibilities, held together only by their common need to be globally visible. Tight coupling between instances and their classes breaks encapsulation, and, together with the global visibility of static methods, complicates testing. By making dependency injection a feature of the programming language, we can get rid of static methods altogether. We employ the following semantic changes: (1) Replace every occurrence of a global with an access to an instance variable; (2) Let that instance variable be automatically injected into the object when it is instantiated. We present Seuss, a prototype that implements this change of semantics in Smalltalk. We show how Seuss eliminates the need to use class methods for non-reflective purposes, reduces the need for creational design patterns such as Abstract Factory and simplifies configuration code, particularly for unit tests.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Achermann, F., Nierstrasz, O.: Explicit Namespaces (chapter 8). In: Weck, W., Gutknecht, J. (eds.) JMLC 2000. LNCS, vol. 1897, pp. 77–89. Springer, Heidelberg (2000)CrossRefGoogle Scholar
  2. 2.
    Bracha, G., Ungar, D.: Mirrors: design principles for meta-level facilities of object-oriented programming languages. SIGPLAN Not. 39(10), 331–344 (2004)CrossRefGoogle Scholar
  3. 3.
    Bracha, G., von der Ahé, P., Bykov, V., Kashai, Y., Maddox, W., Miranda, E.: Modules as objects in newspeak. In: D’Hondt, T. (ed.) ECOOP 2010. LNCS, vol. 6183, pp. 405–428. Springer, Heidelberg (2010)CrossRefGoogle Scholar
  4. 4.
    Brock, R.W., Wilkerson, B.: Object-oriented design: a responsibility-driven approach. SIGPLAN Not. 24, 71–75 (1989)CrossRefGoogle Scholar
  5. 5.
    Ellis, B., Stylos, J., Myers, B.: The Factory Pattern in API Design: A Usability Evaluation. In: 29th International Conference on Software Engineering (ICSE 2007), pp. 302–312. IEEE Computer Society Press, Washington, DC, USA (2007)CrossRefGoogle Scholar
  6. 6.
    Fowler, M.: Patterns of Enterprise Application Architecture. Addison-Wesley, Reading (2002)Google Scholar
  7. 7.
    Gamma, E., Helm, R., Johnson, R., Vlissides, J.M.: Design Patterns: Elements of Reusable Object-Oriented Software, 1st edn. Addison-Wesley, Reading (1994)MATHGoogle Scholar
  8. 8.
    Ko, A.J., Myers, B.A., Aung, H.H.: Six Learning Barriers in End-User Programming Systems. In: Proceedings of the 2004 IEEE Symposium on Visual Languages - Human Centric Computing, VLHCC 2004, pp. 199–206. IEEE Computer Society Press, Los Alamitos (2004)Google Scholar
  9. 9.
    Leon, R.: SandstoneDb, simple ActiveRecord style persistence in Squeak,
  10. 10.
    Mackinnon, T., Freeman, S., Craig, P.: Endo-testing: Unit testing with mock objects,  ch.17, pp. 287–301. Addison-Wesley Longman Publishing Co., Inc, Boston (2001)Google Scholar
  11. 11.
    Dhanji Prasanna. Dependency Injection. Manning Publications, pap/pas edition (August 2009)Google Scholar
  12. 12.
    Renggli, L., Ducasse, S., Kuhn, A.: Magritte – A meta-driven approach to empower developers and end users. In: Engels, G., Opdyke, B., Schmidt, D.C., Weil, F. (eds.) MODELS 2007. LNCS, vol. 4735, pp. 106–120. Springer, Heidelberg (2007)CrossRefGoogle Scholar
  13. 13.
    Renggli, L., Gîrba, T., Nierstrasz, O.: Embedding languages without breaking tools. In: D’Hondt, T. (ed.) ECOOP 2010. LNCS, vol. 6183, pp. 380–404. Springer, Heidelberg (2010)CrossRefGoogle Scholar
  14. 14.
    Vanbrabant, R.: Google Guice: Agile Lightweight Dependency Injection Framework. Apress (2008)Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2011

Authors and Affiliations

  • Niko Schwarz
    • 1
  • Mircea Lungu
    • 1
  • Oscar Nierstrasz
    • 1
  1. 1.University of BernSwitzerland

Personalised recommendations