Professional Documents
Culture Documents
- Second Edition
14
A Component-Based Approach to
Software Architecture
When we’re starting a software project, we never know all the require-
ments that the users will throw at us once they are actually using the soft-
ware. A software project is always associated with taking chances and
making educated guesses (we like to call them “assumptions” to make it
sound more professional). The environment of a software project is just
too volatile to know in advance how everything will play out. This volatil-
ity is why the Agile movement was born. Agile practices make organiza-
tions flexible enough to adapt to change.
But how can we create a software architecture that can cope with such an
agile environment? If everything can change at any time, should we even
bother with architecture?
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 1/11
7/25/23, 1:57 AM Chapter 14: A Component-Based Approach to Software Architecture | Get Your Hands Dirty on Clean Architecture - Second Edition
ports don’t change, we can evolve anything within the application to re-
act to changes in the agile environment.
Also, I’ve heard quite a few times that Hexagonal Architecture feels hard,
especially for a software project just starting out. It’s hard to get the team
on board because not everyone understands the value of dependency in-
version and the mapping between the domain model and the outside
world. Hexagonal Architecture might just be overkill for a fledgling
application.
For cases like this, we might want to start out with a simpler architecture
style that still provides the modularity we need to evolve into something
else in the future but that’s simple enough to get everyone on board. I
propose that a component-based architecture is a good starting point, and
we’ll use this chapter to discuss this architecture style.
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 2/11
7/25/23, 1:57 AM Chapter 14: A Component-Based Approach to Software Architecture | Get Your Hands Dirty on Clean Architecture - Second Edition
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 3/11
7/25/23, 1:57 AM Chapter 14: A Component-Based Approach to Software Architecture | Get Your Hands Dirty on Clean Architecture - Second Edition
For the sake of this chapter, a component is a set of classes that has a ded-
icated namespace and a clearly defined API. If another component needs
this component’s functionality, it can call it via its API, but it may not
reach into its internals. A component may be made up of smaller compo-
nents. By default, these sub-components live inside the internals of the
parent component, so that they are not accessible from the outside. They
can, however, contribute to the parent component’s API if they imple-
ment functionality that should be accessible from the outside.
If A1 needs access to B’s functionality, it can get it by calling B’s API. It can-
not, however, access B1’s API, because, as a sub-component, it’s part of its
parent’s internals and thus hidden from the outside. B1 can still contrib-
ute functionality to its parent’s API, though, by implementing an interface
in the parent API. We will see this in action in the case study later.
The same rules apply between the sibling components, A1 and A2. If A1
needs access to A2’s functionality, it can call its API, but it cannot call into
A2’s internals.
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 5/11
7/25/23, 1:57 AM Chapter 14: A Component-Based Approach to Software Architecture | Get Your Hands Dirty on Clean Architecture - Second Edition
Since a lot can go wrong when scraping web pages, we decided to run the
checks asynchronously. That means that the component needs to provide
an API to schedule checks and an API to retrieve the results of a check af-
ter it has been executed. This implies a queue in which to store incoming
check requests and a database in which to store the results of these
checks.
From the outside, it doesn’t matter whether we build the check engine “in
one piece” or split it up into sub-components. As long as the component
has a dedicated API, these details are hidden from the outside. The re-
quirements above, however, outline certain natural boundaries for sub-
components within the check engine. Breaking the check engine up along
these boundaries allows us to manage complexity within the check en-
gine component because each sub-component will be simpler to manage
than the whole problem.
A queue component that wraps the access to a queue to queue and de-
queue check requests.
A database component that wraps the access to a database to store and
retrieve check results.
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 6/11
7/25/23, 1:57 AM Chapter 14: A Component-Based Approach to Software Architecture | Get Your Hands Dirty on Clean Architecture - Second Edition
The diagram mirrors the structure of the code. You can think of each box
as a Java package (or a simple source code folder in other programming
languages). If a box is within a larger box, it’s a sub-package of that larger
box. The boxes at the lowest level, finally, are classes.
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 7/11
7/25/23, 1:57 AM Chapter 14: A Component-Based Approach to Software Architecture | Get Your Hands Dirty on Clean Architecture - Second Edition
When a check has finished, the DefaultCheckRunner class stores the re-
sults in the database by calling the CheckMutations interface of the data-
base sub-component’s API. This interface is implemented by the
CheckRepository class, which handles the details of connecting and talk-
ing to a database. Again, the database technology is not leaked to the out-
side of the database sub-component.
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 8/11
7/25/23, 1:57 AM Chapter 14: A Component-Based Approach to Software Architecture | Get Your Hands Dirty on Clean Architecture - Second Edition
All this we get by structuring our code into components, following the
convention of having dedicated API and internal packages.
Conventions are good to have, but if that’s all there is, someone will break
them, and the architecture will erode. We need to enforce the conven-
tions of the component architecture.
The nice thing about the component architecture is that we can apply a
relatively simple fitness function to make sure that no accidental depen-
dencies have crept into our component architecture:
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 9/11
7/25/23, 1:57 AM Chapter 14: A Component-Based Approach to Software Architecture | Get Your Hands Dirty on Clean Architecture - Second Edition
We just need a way to identify internal packages during each build and
feed them all into the function above, and the build will fail if we have ac-
cidentally introduced a dependency to an internal class.
The fitness function doesn’t even need to know anything about the com-
ponents in our architecture. We just need to follow a convention for iden-
tifying internal packages and then feed those packages into the function.
This means that we don’t need to update the test that’s running the fitness
function whenever we add or remove a component to/from the code
base. Very convenient!
Note
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 10/11
7/25/23, 1:57 AM Chapter 14: A Component-Based Approach to Software Architecture | Get Your Hands Dirty on Clean Architecture - Second Edition
Even though there are loopholes to get around the rules of the component
architecture, the architecture itself is so simple that it’s very easy to un-
derstand and communicate. If it’s easy to understand, it’s easy to main-
tain. If it’s easy to maintain, the loopholes are less likely to be exploited.
In the next and last chapter, we’ll close the discussion around architec-
ture and try to answer the question of when we should choose which ar-
chitecture style.
https://learning.oreilly.com/library/view/get-your-hands/9781805128373/B19916_14.xhtml#_idParaDest-120 11/11