Chapter 4
Façade
Summary prepared by Kirk Scott
1
Emmer
From Wikipedia, the free encyclopedia
• Emmer wheat (Triticum dicoccum), also
known as farro especially in Italy, or hulled
wheat,[1] is a type of awned wheat. It was one
of the first crops domesticated in the Near
East. It was widely cultivated in the ancient
world, but is now a relict crop in mountainous
regions of Europe and Asia.
2
3
Spelt
From Wikipedia, the free encyclopedia
• This article is about the wheat species.
• For the alternative version of the word "spelled", see
Spelling.Spelt, also known as dinkel wheat,[2] or hulled
wheat,[2] is a hexaploid species of wheat. Spelt was an
important staple in parts of Europe from the Bronze
Age to medieval times; it now survives as a relict crop
in Central Europe and northern Spain and has found a
new market as a health food. Spelt is sometimes
considered a subspecies of the closely related species
common wheat (T. aestivum), in which case its
botanical name is considered to be Triticum aestivum
subsp. spelta.
4
5
Einkorn wheat
From Wikipedia, the free encyclopedia
(Redirected from Einkorn)
• Einkorn wheat (from German Einkorn, literally
"single grain") can refer either to the wild species
of wheat, Triticum boeoticum , or to the
domesticated form, Triticum monococcum. The
wild and domesticated forms are either
considered separate species, as here, or as
subspecies of T. monococcum. Einkorn is a diploid
species of hulled wheat, with tough glumes
('husks') that tightly enclose the grains. The
cultivated form is similar to the wild, except that
the ear stays intact when ripe and the seeds are
larger.
6
• Einkorn wheat was one of the earliest cultivated forms of wheat,
alongside emmer wheat (T. dicoccum). Grains of wild einkorn have
been found in Epi-Paleolithic sites of the Fertile Crescent. It was first
domesticated approximately 7500 BC (7050 BC ≈ 9000 BP), in the
Pre-Pottery Neolithic A (PPNA) or B (PPNB) periods.[1] Evidence
from DNA finger-printing suggests einkorn was domesticated near
Karaca Dağ in southeast Turkey, an area in which a number of PPNB
farming villages have been found.[2] Its cultivation decreased in the
Bronze Age, and today it is a relict crop that is rarely planted,
though it has found a new market as a health food. It remains as a
local crop, often for bulgur (cracked wheat) or as animal feed, in
mountainous areas of France, Morocco, the former Yugoslavia,
Turkey and other countries. It often survives on poor soils where
other species of wheat fail.[3]
7
8
Design Patterns in Java
Chapter 4
Façade
Summary prepared by Kirk Scott
9
Façade
• The book starts with a review of some of the
benefits of object-oriented code development
• The idea is that families/hierarchies of classes
are developed which are known as toolkits
• Then application programs can be written
which make use of the functionality
implemented in the toolkits
10
• In the broadest sense, you might think of the
Java API as a toolkit
• This would be an example of a toolkit that has
too much in it to fully understand
• How do you know what features to use or
where to start when trying to use it to
implement an application?
11
• The façade design pattern is an approach to
managing the complexity of a toolkit
• The book describes a façade as a “small
amount” of code which provides a typical, nofrills usage of the classes in a class library
• It expands this description by saying that a
façade is a class with a level of functionality
between a toolkit and an application
12
• This class offers simplified usage of the classes
in a package or subsystem (the toolkit)
• It can do this by providing an interface with
fewer methods
• It can also do this by providing an interface
containing methods with fewer parameters
13
Book Definition of the Design Pattern
• Book definition:
• The intent of the Façade pattern is to provide
an interface that makes a subsystem easy to
use
14
• The book will take two approaches to
illustrating what a façade is
• The first approach is to point out a class in the
Java API which it says meets the definition of a
façade
• The second approach will be to give a
concrete example of a design, including code,
and refactor it to reflect the façade pattern
15
• The refactoring involves adding new classes to
the design and dividing methods among them.
• A basic design question is always, what are the
classes and what functionality do they
contain?
• You may get this right in an initial design or
you may have to rethink it on a redesign
16
• The façade pattern can be used as a model for
abstracting out certain functionalities into one
class, the façade
• The remaining functionalities are then
reorganized in a set of classes that is more
logical or convenient
17
Facades and Utilities
• If a class fits the intent of the Façade design
pattern and consists entirely of static
methods, then it is called a utility
• A utility can be a useful construct
• Incidentally, with a utility, you can’t override
static methods in subclasses
18
• Facades don’t have to consist entirely of static
methods
• If you develop a façade, in theory it would be
possible to give it subclasses
• You could override methods as necessary
• There is no particular reason to pursue this
line of thinking in order to understand the
design pattern
19
Facades and Demos
• A demo is an example that shows how to use
a class or subsystem
• Demos provide much of the same information
or value to the programmer as a façade, but
they are not exactly the same thing
• The book lists several ways in which they differ
20
• “A demo is usually a stand-alone application; a
façade is usually not.
• A demo usually includes sample data; a façade
does not.
• A façade is usually configurable; a demo is not
• A façade is intended for reuse; a demo is not
• A façade is intended for use in production; a
demo is not”
21
An Example of a Façade in the Java API
• The book gives the JOptionPane class from the
Java API as an example of a façade
• If you look in the API documentation, this
class has dozens of methods of its own and it
inherits literally hundreds of others
• It is in the javax.swing package, and it can be
used in the writing of graphical user interface
code
22
• It is impossible to say how many discrete
elements of the swing package the
JOptionPane class is based on, or provides a
way of using
• The book provides one simple example of its
use
• It gives an application that pops up a confirm
dialog box
23
• This is done with a static call to the
JOptionPane class
• There is no reason to look at the complete
code where the call occurs
• The one line of code in question looks like this
• option = JOptionPane.showConfirmDialog(…);
• Its output is shown on the next overhead
24
25
• The use of the JOptionPane should not be
foreign to you, because it was used in unit 17
of CSCE 222
• In the code for Echo1.java there was a line of
code like this
• inputString = JOptionPane.showInputDialog(…);
• Its output is shown on the next overhead
26
27
• The book makes the following statements:
• “The JOptionPane class is one of the few
examples of a Façade in the Java class libraries.
• It is production worthy, configurable, and
designed for reuse.
• Above all else, the JOptionPane class fulfills the
intent of the Façade pattern by providing a simple
interface that makes it easy to use the JDialog
class.
28
• JOptionPane has dozens of static methods
that effectively make it a utility as well as a
façade.
• Strictly speaking, though, it does not meet the
UML definition of a utility, which requires it to
possess solely static methods.”
• [End of solution 4.2]
29
• You might argue that a façade simplifies a
“subsystem” and that the solitary JDialog class
does not qualify as a subsystem.
• But it is exactly the richness of this class’s
features that make a façade valuable.
30
• Challenge 4.3
• “Few facades appear in the Java class libraries.
Why is that?”
31
• Solution 4.3:
• “Here are a few reasonable but opposing
views regarding the paucity of facades in the
Java class libraries.
• [given on the following overheads]
32
• [1] As a Java developer, you are well advised
to develop a thorough knowledge of the tools
in the library.
• Facades necessarily limit the way you might
apply any system.
• They would be a distracting and potentially
misleading element of the class libraries in
which they might appear.
33
• [2] A façade lies somewhere between the
richness of a toolkit and the specificity of a
particular application.
• To create a façade requires some notion of the
type of applications it will support.
• This predictability is impossible given the huge
and diverse audience of the Java class
libraries.
34
• [3] The scarcity of facades in the class libraries
is a weakness.
• Adding more facades would be a big help.”
35
Parametric Equations: A Detour
• The book’s next example is based on an
application that presents graphical
representations of parabolic flight paths of
rockets
• You will see that this is closely related to some
of the things that happened both visually and
with the use of functions that cropped up in
the MVC example with graphs of thrust and
burn rate
36
• This example and the MVC example raise the
issue of how best to do graphical things based
on mathematical formulas in Java
• These questions have nothing to do with the
façade design pattern, so they will not be
covered in depth
• However, they have to be covered somewhat
in order to follow the example
37
• Every example more complicated than rockbottom will have a context
• Graphing in an application can be a significant
implementation concern
38
• The correct use of parametric equations can
make the graphing of mathematical equations
much neater and clearer
• The book devotes a subsection to the use of
parametric equations
• For the purposes of understanding the
example application, the topic of parametric
equations will be covered now
39
Parametric Equations: An Initial
Example
• In Java, graphical objects are generally
determined by (x, y, w, h)—in other words,
their location is specified by an (x, y) pair
• One example of a case where parametric
equations would be useful is when you are
interested in plotting a mathematical
relationship which isn’t a mathematical
function
40
• In other words, for a given value of x, the
relationships has more than one value of y
• A circle in Cartesian coordinates is an example
• The equation for the circle centered at the
origin with radius 1 is x2 + y2 = 1.
• If you solve for y, you get y = ±√(1 – x2), which
is not a function
• For each x value there are 2 y values
41
• However, you could switch your
representation scheme to polar coordinates
• For a circle with its center at the origin, r = a
constant
• This is a very simple function
• You can then translate from polar to Cartesian
coordinates
42
• For a circle of radius r, for any angle θ
(consider the range 0 to 2π), these
relationships hold:
• sin θ = y / r
• cos θ = x / r
• Solving for x and y gives:
• y = r sin θ
• x = r cos θ
43
• Polar coordinates arise as a natural alternative
to Cartesian coordinates
• The last two equations given above illustrate
parameterization
• Instead of one equation, you have two
equations jointly expressing the values of x
and y in terms of r and θ
44
• x and y are expressed in terms of a constant r,
and a variable, or parameter, θ
• These two equations for x and y are functions
of θ
• There is no confusion about ± values
45
Parametric Equations: Matching Equations
to the Requirements of Java Graphing
• Converting a given equation to a parametric
form can also be useful even if the equation is
already a function
• Consider some of the challenges of
representing a mathematical function using
the simple graphical capabilities of Java
46
• A. You have to match up the actual width of
the display panel in Java with the range of
values x takes on in the function of interest
• B. You have to do the same for y.
• C. You also have to deal with the fact that in
Cartesian coordinates, the positive y axis
points up, while in Java, the positive y axis
points down
47
• Parameterizing an equation can give expressions
for x and y that take into account the inversion of
y and the width and height of the display panel
• The code can be written so that the width and
height of the display area, w and h, are variables
or input parameters to display methods
• The graphing code automatically factors changes
in w or h, namely, the dimensions of the display
panel, into the output values of x and y
48
Parametric Equations: Fireworks Flight
Paths
• The book’s example code involves displaying the
trajectory of a shell
• This is a projectile that is shot without any
propellant of its own (it’s not a rocket)
• If air resistance and wind are ignored, and if the
shell doesn’t explode in mid-air and it returns to
earth, its flight path is a simple parabola
• Sample output from their example application is
shown on the following overhead
49
50
• In general, a parabola has a quadratic
equation of the form y = x2
• A parabola with this simple equation would
have its vertex at the origin and it would open
upward
• This is a function, but it is desirable to
parameterize it so that we can display it nicely
51
• In addition to fitting the display panel, the
parameterization should deal with the
orientation and location of the parabola in the
panel
• The book empirically derives some parametric
equations for this scenario
52
How to Parameterize
• The first task in parameterizing is to decide on
a parameter
• In many physical cases it is customary to use
the variable t, denoting time
• In practice, t might range from some given
starting time to some given ending time
• In this example, it may be thought of as
representing time—but not clock time
53
• It is useful to normalize t, having it start at 0 and
end at 1
• Then any value of t between 0 and 1 represents
the proportion of time that has passed between
the beginning and the end of some physical
process
• It is not really necessary to think of this as time at
all
• It can be regarded as a useful mathematical
convenience and nothing more
54
Parameterizing x
• Consider the x axis for a function
• It will correspond to the x axis in a Java
• It runs positive from left to right in both
Cartesian coordinates and Java display
• Parameterizing x involves making its range of
values correspond to the amount of time that
has passed
55
• Practically speaking, what you want to
accomplish from parameterization is this:
• t takes on values from 0 to 1
• x depends on t
• As t goes from 0 to 1, x takes on the values
from 0 to w, the width of the panel in which
the graph is to be displayed
56
• Notice how, by default, the range of x values
has been dealt with in the statement of the
problem.
• The range of x starts at 0, not at some
negative value to the left of the origin, as
would be the case for a centered graph of y =
x2
57
• The book gives this parametric equation for x,
which satisfies this requirement:
• x=w*t
• As the value of t goes from 0 to 1, the value of
x goes from 0 to w.
58
Parameterizing y
• Taking care of y involves a few more steps
• Recall the general form y = x2
• If you simply graphed that equation in
Cartesian coordinates, the vertex would be at
the origin and the parabola would open
upward
• However, the parabola for a flight path has its
vertex at the top center of the display panel,
and the parabola opens downward
59
• Parameterizing y involves making its range of
values correspond to the amount of time that
has passed
• Practically speaking, what you want to
accomplish from parameterization is this:
• t takes on values from 0 to 1
• y depends on t
60
• These 3 things have to be achieved:
• 1. The vertex of the parabola is centered in
the panel
• 2. The parabola opens downward (y is
inverted)
• 3. As t goes from 0 to 1, y takes on values
between 0 and h, the height of the panel in
which the graph is to be displayed
61
1. Centering the Vertex
• The x coordinate of the vertex should be at
the midpoint of the panel the graph is
displayed in
• The x coordinate of the vertex would
correspond to half of w, and in terms of t, it
would correspond to half of t
62
• The goal is to have the min value for y, 0,
occur at the center of w
• This parametric equation for y has the vertex
in the middle:
• y = (t – .5)(t – .5)
• When t = .5, y = 0
• When t = 0 or 1, y = (.5)(.5) = .25
63
2. Inverting y
• The y coordinate of the vertex should be at
the top of the panel the graph is displayed in
and the parabola should open downward
• This “inversion” in y turns out not be an issue
at all
• Because positive y points downward in Java
graphics, no adjustment has to be made to the
parametric equation
64
3. y Takes on Values Between 0 and h
• The next step is to normalize the maximum
value attained by y to 1
• To do this, you introduce a constant to offset
the product’s giving a maximum value of .25
• In other words, you introduce a factor of 4
into the parametric equation:
• y = 4(t – .5)(t – .5)
65
• The last step is to include h as a variable so
that the maximum value of y is h:
• y = 4h(t – .5)(t – .5)
66
The End Result of Parameterization
• Together, then, these are the parametric
equations:
• x = wt
• y = 4h(t – .5)(t – .5)
• The parameter t defines the quadratic
relationship between x and y
67
• The shape of the parabola as graphed will
depend on the constant 4 and the variables w
and h
• In other words, the shape as shown on the
display will not be “purely” y = x2
• The graph will be stretched or squeezed so
that it fits perfectly into the bounds defined
by w and h
68
Applying Facade
• As usual, the book gives an initial example and
then refactors to façade
• This presentation will simply go directly to the
façade example
• Shown on the next overhead is the output of
the example
69
70
• The façade based design will have these
features:
• 1. A Function class with a method f() that
accepts a double (the value of time) and
returns a double (the function’s value)
• Two subclasses of the Function class which
contain the code for parametric equations of x
and y respectively
71
• 2. A PlotPanel class that shows the flight of a
shell
• The PlotPanel class will do plotting by
receiving objects of the Function class, which
generate the needed x and y values
• 3. There will be a UI class, which is the façade
in the design
• It will contain a method named
createTitledPanel(), which is the visible output
72
• An incomplete UML diagram which outlines
the design listed above is shown on the
following overhead
• It shows a design for the ShowFlight
application divided into a set of classes so that
each has one job
73
74
• Challenge 4.4
• “Complete the diagram in Figure 4.5 to show the
code for ShowFlight refactored into three types:
• a Function class, a PlotPanel class that plots two
parametric functions, and a UI façade class.
• In your redesign, make the ShowFlight2 class
create a Function for y values, and have a main()
method that launches the application.”
75
Solution 4.4
76
Disappointment
• Note that the given solution is incomplete
• In particular the facade, the UI class, is
floating in space
• This is a disappointment
• The goal is to understand the façade pattern
• The UI class is the façade
• But what is its relationship to the other classes
in the example application?
77
Redemption
• The UI class contains the createTitledPanel()
method in the application
• Where is this called from?
• It is called from main()
• In other words, in the UML diagram one
missing link is between the ShowFlight2 class
and the UI class
78
• The ShowFlight2 class makes use of the
PlotPanel class to generate the contents of the
graphical elements
• The ShowFlight2 class makes use of the façade
class in generating the standardized graphical
elements of the application that will be
displayed
79
• In other words, in complete code, in a
complete UML diagram, you would see this:
• Main() calls the UI to make the titled panel
• It also creates an instance of PlotPanel
• The titled panel wraps the PlotPanel
80
• So what is the UI a façade to?
• That is the magic question, and the diagram
simply doesn’t show it
• This is the answer:
• The UI class, the façade, stands in front of
graphical elements existing in the Java API
81
• This is a good example, because it shows how
your code might make use of a façade into the
system supplied classes
• Your own code may be simple enough that
you don’t need a façade into it
• On the other hand, it’s funny that at the end
the authors leave out the final, explanatory
pieces
82
Another Missing Element in the
Diagram
• There is another missing link in the diagram
• The Function class is floating in space
• The PlotPanel class paintComponent() method
relies on the Function class to contain the
logic for generating the points of a parabola
• In other words, the other missing link in the
UML diagram is between the PlotPanel Class
and the Function class
83
Considering the UML for the Pattern
Again
• Here the diagram for the façade pattern is
considered in the context of the preceding
pattern, the mediator
• Literally, the mediator is the class “in
between”
• The façade is the class “in front of”
• Practically, though, the façade is also “in
between” whatever it’s in front of and
whatever makes use of what it’s in front of
84
• On the overhead following the next one, two
UML diagrams are shown
• The first is my simplified mediator diagram,
repeated for the sake of comparison
• The second is an attempt to abstract out and
simplify the façade pattern
• The dashed arrows in the façade diagram are
meant to indicate “makes use of”
85
• The basic idea is this:
• For the mediator, either the references both go
into the mediator, or both go out, (or maybe
there are references both ways)
• For the façade, the references form a directed
path
• The client uses the façade, which in turn uses the
toolkit
• The end result is that the client uses the toolkit
86
87
The End
88
© Copyright 2026 Paperzz