3 DesignPrinciplesEN
3 DesignPrinciplesEN
Information Repositories
2024-2025
Car WorkShop - The initial project
2 of 97
Car workshop use cases
3 of 97
Car workshop domain model
4 of 97
Car workshop Table model
5 of 97
CWS0 package structure
By actors: Each
package contains
text menus and
subpackage (action)
implementing menu
options.
6 of 97
Mechanics Management class diagram
CWS0 implementation
Return to 26
7 of 97
Add mechanic sequence diagram
CWS0 implementation
8 of 97
Cashier class diagram
CWS0 implementation
Return to 28
9 of 97
Create invoice sequence diagram
CWS0 implementation
10 of 97
WorkOrdersBillingAction::execute()
CWS0 implementation
11 of 97
Quality standards
12 of 97
Software system quality attributes
13 of 97
CWS0 evaluation
Poor Maintainability
■ Changing persistence, UI, . . . means changing (almost) every class.
■ Repeated code has a big impact in maintainability
Poor extensibility
■ With tangled code, adding a new behaviour (encrypt data) makes
you worrying about how it affects the features around it
Poor Reusability
■ Reuse data access code into some other application is impossible.
Poor scalability
■ Trying to optimize database access (connection pooling) means
changing everything
14 of 97
Action classes implement all concerns: user interaction + logic +
data access
No separation of concerns
■ Code corresponding to a concern is not well encapsulated in a
module, and ends up being scattered across many modules.
■ It is difficult to locate and understand the implementation of a
concern because it is tangled with code that implements others.
■ Code is repeated everywhere it is needed (access to an specific
table).
The key to creating quality code is adhering to low coupling, high
cohesion principle.
■ Coupling assesses how tightly a module is related to others.
■ Cohesion refers to how closely the functions in a module are
related. Single responsibility (no schizophrenic classes)
15 of 97
Patterns
16 of 97
Common to most enterprise applications
19 of 97
Reference architecture
■ Components are organized into layers.
■ Each layer exposes an interface (API) to be used by the layer
above it (object calls are all inward)
■ Each layer acts as a
□ Server: service provider to layer “above”
□ Client: service consumer of the layer “below”
21 of 97
Downsides
■ Not universally applicable (transactions)
■ Extra layers can harm performance.
■ Layered architecture demands that dependencies run only one way
□ What happens if you want to change entity Account (change account
number to IBAN, support custodian data, ...). You will very likely
have changes in all layers.
□ Poor maintainability
22 of 97
Apply layered architecture to CWS0
24 of 97
Classes in the business layer
■ Business classes
implement business rules
(AddMechanic,
ListMechanics, . . . )
■ All of them look similar
25 of 97
Mechanics Management (CRUD) class diagram
Compare
with 7
26 of 97
Mechanics Management (CRUD) sequence diagram
User interaction code clearly separated from business and data access code
27 of 97
Workorder billing
28 of 97
CWS V1: Layering
Cashier class diagram
29 of 97
CWS V1: Layering
Workorder billing sequence diagram
30 of 97
Façade Pattern
31 of 97
Remaining problems
32 of 97
Context
33 of 97
Applicability
Reduce coupling between subsystems and clients and subsystems
themselves; could be strong within subsystem
public class Client public class Client
{ {
public void method ( ) { public void method ( ) {
Subsy stemClas s sc = new Facade ().
new Subsyst emClass (); c al lD oS o me th in g ();
sc . doSomething (); }
} }
}
34 of 97
A thin facade also hides the details of the interface. It is possible to
change implementation of subsystem in the future, while still
providing consistent functionality through a stable interface
public class Client
{
public void method ( ) {
Scanner s = new Scanner ();
Parser p = new Parser ();
s . scan ();
p . parse ();
}
}
35 of 97
Class diagram
■ DefaultFacadeImpl:
Represents the implementation
of IFacade, which is in charge
of communicating with all the
subsystems.
■ Subsystems: Represents all the
modules or subsystems with
interfaces for communication.
36 of 97
Mechanics Management Facade
37 of 97
Mechanics Management (CRUD) class diagram
Facade pattern
38 of 97
Add mechanic sequence diagram
Facade pattern
39 of 97
Workorder billing
Facade pattern
40 of 97
Cashier class diagram
Facade pattern
41 of 97
Workorder billing sequence diagram
Facade pattern
42 of 97
Simple Factory (or Class Factory)
43 of 97
Remaining problems
44 of 97
Simple Factory Pattern
45 of 97
Class diagram
46 of 97
Simple Factory advantages
47 of 97
Mechanics Management package structure
Class factory
48 of 97
Mechanics Management class diagram
Class factory
■ BusinessFactory
returns façade.
■ Changing service
implementation
means a change in
a method
forXXXService;
nothing else.
49 of 97
Add mechanic sequence diagram
Class factory
50 of 97
Workorder billing
Class factory
51 of 97
Cashier class diagram
Factory pattern
52 of 97
Workorder billing sequence diagram
Factory pattern
53 of 97
Split business layer into business and data access
layers
54 of 97
Problems with data access from OO programs
55 of 97
Split into business and access layers
Split business layer into business layer and data access layer.
56 of 97
DAO (Data Access Object) Pattern
57 of 97
■ They hide all complex details involved in performing CRUD
operations in the underlying storage
■ Business objects use a simpler interface exposed by DAO.
58 of 97
Advantages of using DAO pattern
60 of 97
Example
61 of 97
Participants and responsabilities
62 of 97
DAO related patterns
63 of 97
Row Data Gateway
■ A Row Data Gateway object [11] acts as a gateway to a single
record in a data source.
□ Mimics exactly a single row returned by a query.
□ Each table column is a field in the object
□ Do not have domain logic
□ Finder object
■ https://www.sourcecodeexamples.net/2018/04/
row-data-gateway.html
64 of 97
Table Data Gateway
■ A Table Data Gateway object acts as a Gateway to a database
table or view.
□ One gateway for each table
□ Simple interface to a table with several find methods and methods to
maintain data
□ CRUD methods for accessing a single table or view
□ Finders return Collection of DTOs
■ An example in https://www.sourcecodeexamples.net/2018/
04/table-data-gateway.html
65 of 97
Read the page with DeleteMechanic.java business class and the
underlying database
■ Which DAO or TDG are necessary?
■ Which methods are needed in the TDG to abstract
DeleteMechanic from the storage?
■ Write the TDG (only methods identified above)
■ Rewrite business object using TDG
Problems detected (identify the line of code)
66 of 97
Where to get DAO from?
67 of 97
DAO and Abstract Factory pattern
When the underlying storage is subject to change from one
implementation to another.
68 of 97
DAO and Factory Method pattern
The underlying storage is not subject to change from one
implementation to another, use the Factory Method pattern to
produce a number of DAOs needed by the application.
69 of 97
Simple Factory
70 of 97
Split business into business and persistence:
Business
71 of 97
Business layer
Business logic encodes the real-world business rules that determine
how data can be created, stored, and changed.
It is not concerned with managing database (Data access layer) or
diplaying user interface (interface layer)
Business Layer defines application’s boundary and the set of
available operations from the perspective of interfacing client layers.
72 of 97
Business layer patterns
73 of 97
Transaction scripts
74 of 97
Class diagram
75 of 97
Advantages and downsides
Advantages:
■ It is an ideal solution for systems with only a small amount of logic.
■ Simple, procedural model easy to understand.
■ It has little performance overhead
■ It performs well with simple data source layer when put into action
with patterns such as Row Data Gateway or Table Data Gateway.
Disadvantages:
■ Duplication of code in transactions.
■ Not suitable when domain model is complex
76 of 97
Domain Model pattern
Not one TS that handles all business logic for a user action
Several classes, each of them handles a slice of domain logic with both
the data, attributes, and all the behaviour that is related to that data.
77 of 97
Exercises
78 of 97
Command Pattern
It intends to encapsulate complex operations for doing something (for
example, manage menus), in a single object.
So, instead of
Take in mind
that every menu
will repeat more
or less the same
structure (and
code)
79 of 97
80 of 97
Command Pattern
User interface with menus tried an approach to the Command
pattern (Gof) (also known as Action / Transaction)
■ Encapsulate requests as objects:
AddMechanicAction,
ListMechanicsAction, . . .
■ Menu issues requests without knowing
anything about operations requested or
receiver objects of the requests
Advantages
■ It decouples the classes that invoke the operation from the object
that knows how to execute the operation (AddMechanic in CWS1)
■ Extensibility Adding a new command can be done almost without
changing the existing code
81 of 97
Business implementation classes
82 of 97
83 of 97
Implementation
84 of 97
SL.TS.TDG
85 of 97
SL.TS.TDG 0
86 of 97
SL.TS.TDG 0
87 of 97
SL.TS.TDG 0
Notice layers
■ Presentation
■ Business
■ Persistence
Factories make decoupling a layer and its inmediate lower layer, easy
89 of 97
SL.TS.TDG 0
CRUD Mechanic. Sequence diagram
90 of 97
SL.TS.TDG 0
Invoicing
91 of 97
Invoicing. Step 1: View + SL + TS
92 of 97
Invoicing. Step 2: SL+TS+TDG
Methods as updateBreakdown,
. . . , are not detailed. They use
gateways to access data.
Transactions are controlled in
execute()
93 of 97
Invoicing. Step 3: TS+TDG
94 of 97
Further Reading (1)
[1] https://www.youtube.com/watch?v=8Zoz2njk5f8
[2]
https://www.oreilly.com/library/view/software-architectu
[3] https://javadevguy.wordpress.com/2019/01/06/
reevaluating-the-layered-architecture/
[4] https://martinfowler.com/bliki/
PresentationDomainDataLayering.html
[5] https://refactoring.guru/design-patterns/façade
[6]
https://javajee.com/factory-design-patterns-simple-facto
95 of 97
Further Reading (2)
[7]
https://www.oracle.com/java/technologies/data-access-obj
[8]
https://www.martinfowler.com/eaaCatalog/tableDataGateway
[9] https://www.oracle.com/technetwork/java/
dataaccessobject-138824.html
[10] https://martinfowler.com/eaaCatalog/gateway.html
[11] https://www.sourcecodeexamples.net/2018/04/
row-data-gateway.html
[12] https://martinfowler.com/eaaCatalog/gateway.html
96 of 97
Further Reading (3)
[13] http://what-when-how.com/Tutorial/topic-263rdl/
POJOs-in-Action-Developing-Enterprise-
Applications-with-Lightweight-Frameworks-352.html
97 of 97