Patterns & principles
Pattern are useful only in a certain context.
Patterns always have two parts:
- the how: how to implement them
- the when: when to use them and when to leave them alone.
GoodPractices & ThinkingAbout & UseCases
Good Practices
Using IoC and programming to interfaces we can keep decouple our code between the four layers architecture and still access data between two layers. For example I can access a DTO, defined on the Service Layer, from the Presentation Layer.
using composition (of an interface) and delegating interchangeable concrete classes, which implement the interface, we can:
substitute conditional code
change behaviours at runtime
If you need the same object graph between calls to the server, you have to save the server state somewhere, which is the subject of the section on saving server state
Randy's advice is "start with a locally invocable Service Layer whose method signatures deal in domain objects. Add remotability when you need it (if ever) by putting Remote Facades /fÉ™'sÉ‘Ëd/ on our Service Layer or having your Service Layer objects implement remote interfaces.â€
Thinking Abouts
HFDP p.417
Use Cases
A
n Object A delegates other Object(s) X to implement functionalities. If I want to different clone of Object A I may want to move the Ogject(s) X instance into static instance variables and share them.
Design Patterns
I
oC
Principle used
Definition
When to use
H
ow it is implemented
Spring Framework is an Inversion Control container .
Instead of explicitly requesting an object, a requesting code can make use of Inversion of Control containers which provide instantiated objects as needed which inject them into the requesting code resolving the object dependencies.
Using IoC containers you don’t care how services are created and how to get references to the ones you need.
In IoC:
clients declare their dependency on servicing objects though a configuration file (like spring-conf.xml) and
IoC, the Spring Framework, locate and instantiate these serving components.
Dependencies can be wired by either /'a ɪ ðər using annotations or using XML configuration files .IoC containers support eager instantiation, for starting services when the server starts, and lazy loading of services, for services rarely used.
You can easily add additional services by adding a new constructor or setter method with little or no extra configuration.
Dependency injection is an elegant way to decouple client and services .
It can be a substitute of the factory classes, singleton classes that make use of static methods and field variables precluding the use of inheritance. Factory design pattern is more intrusive because components or services need to be requested explicitly
JEE Service Locator pattern
To reduce the lookup complexity of our objects we can use this pattern.
MVC
MVC promote reuse by factoring out the UI widgetry from the domain objects. But this still does not address the connection of the domain to the outside world.
Principle used
Definition
When to use
How it is implemented
State (linked to Strategy)
Principle used
composition and delegation
Definition
Encapsulates state-based behaviours and uses delegation to switch between the different behaviours in different states. This allows an object to alter its behaviour when its internal state changes. The object appear to change its class by delegating a different State object which reimplement the all state-based behaviours.
When to use
It is used in State Machine and substitute conditional code.
How it is implemented
Concrete Context:
has a number of internal states behaviours.
The implementation of the internal states behaviours is delegated to the State interface
an instance variable keep track of the context’s current state
State interface:Define the set of internal states behaviours
is implemented by all the concrete states
Concrete States:each concrete state appropriately implements the State interface’s behaviours
use the concrete Context to set the context’s current state with a reference to the next sibling concrete State
Strategy (linked to State)
Principle used
Definition
When to use
Instead of inheriting behaviours
How it is implemented