- The Use of Components Today
- Limitation of Components
- Approaching a Solution
- Keeping Concerns Separate
1.2 Limitation of Components
To understand the limitations of components, we start with concerns. The goal of a system is to meet requirements or, more generally, concerns. A concern is anything that is of interest to a stakeholder, whether an end user, project sponsor, or developer. For example, a concern can be a functional requirement, a nonfunctional requirement, or a design constraint on the system. It can be more than a requirement of the system. It can even be a low-level concern such as caching or buffering.
Breaking down a problem into smaller parts is called separation of concerns in computer science. Ideally, we want to be able to cleanly separate the different concerns into modules of some kind and explore and develop each in isolation, one at a time. Thereafter, you compose these software modules to yield the complete system. Thus, the concept of separation of concerns and the concept of modularity are two sides of a coin—you separate concerns into modules, and each module solves or implements some distinct set of concerns.
Successful separation of concerns must start early. You begin software development by attempting to understand the stakeholder concerns. You explore and collect the requirements for the system according to stakeholder concerns. Although some concerns can be realized by distinct and separate components, in general, you find many concerns for which components are not adequate. These are known as crosscutting concerns—concerns that impact multiple components. There are different kinds of crosscutting concerns: infrastructure concerns are crosscutting concerns to meet nonfunctional requirements—for instance, logging, distribution, and transaction management. Some crosscutting concerns deal with functional requirements as well. You frequently find that the realization of functional requirements (which can be specified as use-cases) cut across multiple components. Thus, even use-cases are crosscutting concerns.
1.2.1 Inability to Keep Peers Separate
We particularly want to highlight two kinds of crosscutting concerns. The first is what we call peers. These are concerns that are distinct from each other. No one peer is more important than another. If you consider the familiar ATM example, cash withdrawal, fund transfer, and cash deposit are all peers. In our hotel management example, Reserve Room, Check In Customer, and Check Out Customer are peers. These concerns do not need each other to exist. In fact, you can build separate systems for each one. However, when you start to implement peers in the same system, you find significant overlap between them. This is illustrated in Figure 1-2.
Figure 1-2 Tangling and scattering when realizing peers.
Figure 1-2 depicts concerns in different shades on the left-hand side. The right-hand side shows the components with multiple shades. Each shade represents the codes that implement the respective concerns. The limitation of components to keep peers separate is evident in Figure 1-2. It results in two effects, which in aspect-speak are known as tangling and scattering.
Tangling
You find that each component contains the implementation (i.e., code) to satisfy different concerns. For example, in Figure 1-2, you see that the Room component is involved in the realization of three different concerns. This means that as a developer/owner of a component, you need to understand a diverse set of concerns. The component, instead of single-mindedly fulfilling a particular concern, participates in many. This hinders understandability and makes the learning curve steeper for developers.
Do not confuse tangling with reuse. Reuse implies that the same code or behaviors are useable under different contexts. Definitely, some parts of the Room component will be reusable without changes. However, in many cases, as highlighted in Figure 1-2, each concern demands additional and distinct behaviors on the Room component not needed to realize other concerns. There is no reuse among them, and they result in tangling.
Scattering
You also find codes that realize a particular concern are spread across multiple components. For example, in Figure 1-2, you see that the realization of Check In Customer imposes additional behaviors on four components. So, if ever the requirements about that concern change, or if the design of that concern changes, you must update many components.
More importantly, scattering means that it is not easy to understand the internals of a system. For instance, it is not easy to uncover requirements by reading the source code of each component or a set of components. If the requirement for a particular concern changes, different classes need to be updated as well. Poor understandability leads to poor maintainability, and it is not easy to make enhancements, especially for large systems.
1.2.2 Inability to Keep Extensions Separate
The second kind of crosscutting concern is what we call extensions. Extensions are components that you define on top of a base. They represent additional service or features. For example, the Hotel Management System has a waiting list for room reservations. If there are no rooms, the system puts the customer on a waiting list. Thus, the provision of a waiting list is an extension of Reserve Room. Keeping extensions separate is a technique to make a complex problem understandable. You do not want to be entangled by too many issues, so you keep them separate as extensions.
Although it is natural to describe the base and extension separately, there is a problem when it comes to implementing the extension, as exemplified in Figure 1-3.
Figure 1-3 Extensions inserted intrusively.
Figure 1-3 shows the Reserve Room component, which serves as the base. To incorporate the Waiting List extension, a corresponding component is added (shown in a darker shade). But in addition, you need to add some code fragments in the Reserve Room component at a particular location, which we call an extension point. The purpose of this code fragment is to connect or invoke the Waiting List component.
The problem is this: some code has been added to a place that didn't really need it before we added the new feature. It is there for the purpose of hooking the new component onto the existing component. This code fragment is affectionately known as glue code. In aspect-speak, such a change is known as intrusive.
No matter how good your design is, you still need glue code, and if you need to extend the system at another location, you must add the glue code there too. For example, if you need to support different payment methods for the Hotel Reservation System, you need additional glue code to open up an extension point in the system.
Adding all this glue code and making all these changes to existing code definitely makes the original classes harder to comprehend. But a greater problem exists: you cannot possibly identify all the extension points a priori. Thus, what is really needed is a way for you to designate extension points on demand at any time during the system's life cycle. Although this is a significant advantage, there is a limit to how far you can go. If a system is poorly designed, designating extension points is definitely not easy. In addition, after adding several enhancements, you have a better picture of the whole system and you might want to separate concerns differently. In this case, you might dispense some effort to refine the base.