DesingPatterns-LiveCodingSession-1-Builder and Prototype
DesingPatterns-LiveCodingSession-1-Builder and Prototype
design patterns
22
Contents
3
To help us on designing applications we (re-)use
4
Now, we want to focus on the detailed design:
How should I organize my code?
On which method/class should I put my code?
5
Design patterns
A pattern is a general
Descripción de lossolution to a recurring
subsistemas design problem
y componentes de that expresses a
un sistema
relationship
softwarebetween
y de lasainterrelaciones
context, a problem, andellos.
entre a solution.
Intuitive Idea: "When you find this problem, apply this solution”.
Problem Solution
7
Catalog of design patterns of interest for game design
In this
session:
10
Design Pattern: Builder
Intent: Builder lets you construct complex objects step by step. The pattern allows
you to produce different types and representations of an object using the same
construction code.
11
Design Pattern: Builder
12
Design Pattern: Builder
14
Design Pattern: Builder
Example:
// ...
Request allUsersRequest=RequestBuilder
.get("http://somewhere.com/api/users")
.withoutParameters()
.build();
• Use:
CarBuilder.ofType(CarType.COUPE)
.seats(4)
.engine(new TurboEngine());
.transmission(new AutomaticTransmission())
.build();
16
Design Pattern: Builder
17
Design Pattern: Builder
Advantages
• You can construct objects step-by-step, defer construction steps or run steps
recursively.
• You can reuse the same construction code when building various
representations of products.
• You can isolate complex construction code from the business logic of the
product.
Drawbacks
• The overall complexity of the code increases since the pattern requires creating
multiple new classes.
18
WHERE WHOULD YOU APPLY THIS PATTERN IN THE/YOUR
SOFTWARE IMPLEMENTATION OF A BOARD GAME?
19
Design Pattern: Prototype (a.k.a. Clone)
20
Design Pattern: Prototype
Problem: You have an object, and you want to create an exact copy of it.
Approach:
1.- Create a new object of the same class.
2.- Copy the values of the fields
But … some of the fields may be private and not visible from outside. Moreover, your
code becomes dependent on the class of the original object, and sometimes you only
know the interface of the object. 21
Design Pattern: Prototype
The Prototype interface
Structure: declares the cloning
methods. In most cases,
it’s a single clone method.
Advantages
• You can clone objects without coupling to their concrete classes.
• You can get rid of repeated initialization code in favor of cloning
pre-built prototypes.
• You can produce complex objects more conveniently.
• You get an alternative to inheritance when dealing with
configuration presets for complex objects.
Drawbacks
• Cloning complex objects that have circular references might be very
tricky.
23
WHERE WHOULD YOU APPLY THIS PATTERN IN THE/YOUR
SOFTWARE IMPLEMENTATION OF A BOARD GAME?
24
Reminders
25
A+ Criteria published
26
Sneak peak on some A+ criteria: a hard one
➔Except for Thymeleaf, It usually implies the implementation of some other A+ criteria
such as “Implementation of REST API controllers for at least 3 of the application
services, and invocation of these from the client through AJAX calls”
27
Sneak peak on some A+ criteria: an easy one
@StatePattern
“Specification of the application of public class StatePatternApplication {
the design patterns in the project @StatePattern.State
interface SpeedLevel {
using the jpatterns library” void rotate(FanWallControl fanWallControl);}
@StatePattern.Context
static class FanWallControl {
<dependency> private SpeedLevel current;
<groupId>org.jpatterns</groupId>
<artifactId>jpatterns</artifactId> @StatePattern.ConcreteState
<version>0.0.1</version> static class Off implements SpeedLevel {
</dependency> public void rotate(FanWallControl fanWallControl) {
fanWallControl.set_state(new SpeedLevel1());
}
}
@StatePattern.ConcreteState
You have several examples available at static class SpeedLevel1 implements SpeedLevel {
the japatterns branch of the template public void rotate(FanWallControl fanWallControl)
fanWallControl.set_state(new SpeedLevel2());
{
@StatePattern.ConcreteState
https://github.com/gii-is-DP1/spring- static class SpeedLevel2 implements SpeedLevel {
public void rotate(FanWallControl fanWallControl) {
petclinic/tree/jpatterns/src/main/java/org/s fanWallControl.set_state(new SpeedLevel3());
pringframework/samples/petclinic/patterns }
}
@StatePattern.ConcreteState
static class SpeedLevel3 implements SpeedLevel {
public void rotate(FanWallControl fanWallControl) {
fanWallControl.set_state(new Off()); 28
} } }
References
29
Bibliography
30
Bibliography
31
Bibliography
32
Disclaimer and Terms of Use
All material displayed on this presentation is for teaching and personal use only.
Many of the images that have been used in the presentation are Royalty Free images
taken from http://www.everystockphoto.com/. Other images have been sourced directly
from the Public domain, from where in most cases it is unclear whether copyright has
been explicitly claimed. Our intention is not to infringe any artist’s copyright, whether
written or visual. We do not claim ownership of any image that has been freely obtained
from the public domain. In the event that we have freely obtained an image or quotation
that has been placed in the public domain and in doing so have inadvertently used a
copyrighted image without the copyright holder’s express permission we ask that the
copyright holder writes to us directly, upon which we will contact the copyright holder to
request full written permission to use the quote or images.