If you have models separated from the rest of your application logic, maintenance will be much easier, regardless of how complicated your application becomes. But readers following these examples realize, only after years, how counterproductive it is to have monolithic chunks of code in their project. They are not meant to teach software design. Examples like “let’s say we have this ORM X for our models, templating engine Y for our views and we will have controllers to manage it all” achieve nothing other than humongous controllers.Īlthough in defense of these books, the examples are meant to demonstrate the ease at which you can get started with their framework. But they also end up providing bad advice for beginners. These examples try to show what the framework has to offer. Model Is EverythingĪlmost every book about some new MVC (MVP, MVVM, or other M**) framework is littered with examples of bad code. Writing great code is an art, but some principles can always help give your development work the direction it needs to head towards to produce robust and maintainable software. In this article, I will discuss how the Single Responsibility Principle and some techniques that revolve around it can give your code this very quality. An example of this responsibility is a method that orchestrates calls to components that each have other responsibilities:Ĭlass BufferedLogger implements Logger ", t ) ĪlertingService. Control FlowĪ piece of an application’s control flow, execution, or data flow. A specific kind of data is configuration: a collection of parameters for some other method, class, or system. Embodying a piece of data can only be a responsibility of a class (object), but not of a method, module, or service. DataĪ profile of a person on a website, a JSON document, a message. On the level of classes, modules, and services, an external integration responsibility is knowing how to integrate (or encapsulating integration with) the external part: for example, a class knowing how to read the system time (which is exactly what is), or a service encapsulating talking with an external API. Finally, there are integrations with external systems, such as database transactions, reading from or writing to a distributed message queue such as Kafka, or RPC calls to other services. Then, there are integrations with the system, such as logging or checking the system time ( System.currentTimeMillis()). On the lowest level, this can be an integration between modules within the application, such as putting a message into a queue which is processed by another subsystem. On the level of classes and above, a business logic responsibility is knowing how to do (or encapsulating) the business function: for example, a class knowing how to convert XML documents into JSON, or a service encapsulating the detection of fraud transactions. Here are some examples (they are derived from Adam Warski’s classification of objects in applications which he distilled in his thought-provoking post about dependency injection in Scala): Business Logicįor example, extracting a phone number from text, converting an XML document into JSON, or classifying a money transaction as fraud. Instead of defining a responsibility in abstract terms, it may be more intuitive to list the actual types of responsibilities. This phrase is a little more concrete, but it still doesn’t explain what a responsibility is and how small or large a responsibility should be for each particular method, class, module, or service. So, the SRP states that each component should have a single responsibility. The Single Responsibility Principle applies to the software that we develop on different levels: methods, classes, modules, and services (collectively, I’ll call all these things components later in this article). Let’s try to deconstruct it and look at what it actually means. The Single Responsibility Principle may feel a bit vague at first. This article explains the Single Responsibility Principle (SRP): what does it practically mean? And when and how should we apply it? What Does the Single Responsibility Principle Say?
0 Comments
Leave a Reply. |