unit 9 façade summary prepared by kirk scott 1. emmer from wikipedia, the free encyclopedia emmer...

137
Unit 9 Façade Summary prepared by Kirk Scott 1

Upload: dylan-turner

Post on 12-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

1

Unit 9Façade

Summary prepared by Kirk Scott

Page 2: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

2

EmmerFrom 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.

Page 3: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

3

Page 4: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

4

SpeltFrom 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.

Page 5: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

5

Page 6: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

6

Einkorn wheatFrom 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.

Page 7: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

7

• 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]

Page 8: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

8

Page 9: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

9

Design Patterns in JavaChapter 4

Façade

Summary prepared by Kirk Scott

Page 10: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

10

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

Page 11: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

11

• 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?

Page 12: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

12

• The authors suggest that an IDE might help isolate a programmer from toolkit complexity, while still making the toolkit available for use

• However, this implies taking the trouble to learn the IDE

• A sufficiently advanced (paint by number) IDE will automatically generate code for the programmer

Page 13: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

13

• The automatically generated code may not be well designed

• Also, the programmer may not understand the code

• It’s bad enough when you don’t understand the toolkit

• It may be worse when you don’t understand what is ostensibly your own code

Page 14: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

14

• 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, no-frills 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

Page 15: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

15

• 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

Page 16: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

16

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

Page 17: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

17

• 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

Page 18: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

18

• 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

Page 19: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

19

• 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

Page 20: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

20

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• Comparing facades and utilities brings out

some points that you probably haven’t considered before

Page 21: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

21

• For example, you can’t override static methods in subclasses

• On the one hand, trying to do this might not seem to make much sense

• On the other hand, if you are trying to develop a utility hierarchy, this is would be a limitation of utilities

Page 22: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

22

• Turning this around, you could make this positive statement about facades:

• 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

Page 23: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

23

• It’s not immediately clear how useful this would be

• Could it be used to support the goal of making a toolkit easier to use, or would it just make the façade more complicated?

• The book doesn’t pursue this idea any further• No example of extending a façade will be

given

Page 24: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

24

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 difference can be expressed in terms of the use that they can be put to

Page 25: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

25

• Challenge 4.1• “Write down two differences between a demo

and a façade.”

Page 26: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

26

• Solution 4.1• “Some differences to note between demos and

facades are as follows.– 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”

Page 27: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

27

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

Page 28: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

28

• 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

Page 29: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

29

• 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

Page 30: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

30

Page 31: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

31

• The use of the JOptionPane should not be foreign to you, because it was used in unit 17 of CS 202

• 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

Page 32: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

32

Page 33: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

33

• Challenge 4.2• “The JOptionPane class makes it easy to

display a dialog. • Say whether this class is a façade, a utility, or a

demo, and justify your answer.”

Page 34: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

34

• Solution 4.2• “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.

Page 35: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

35

• 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.

Page 36: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

36

• Sun Microsystems bundles many demos in with the JDK.

• However, these classes are never part of the Java class libraries.

• That is, these classes do not appear in packages with a java prefix.

• A façade may belong in the Java class libraries, but demos do not.

Page 37: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

37

• 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]

Page 38: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

38

• Challenge 4.3• “Few facades appear in the Java class libraries.

Why is that?”

Page 39: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

39

• 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]

Page 40: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

40

• [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.

Page 41: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

41

• [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.

Page 42: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

42

• [3] The scarcity of facades in the class libraries is a weakness.

• Adding more facades would be a big help.”

Page 43: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

43

Parametric Equations: A Detour

• The book’s next example is based on an application that presents graphical representations of parabolic flight paths of rockets

• This raises the issue of how best to do graphical things based on mathematical formulas in Java

• This has nothing to do with the façade design pattern

Page 44: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

44

• However, graphing can be a real concern• You may recall some of the graphical code I

provided in CS 202• It was not nice• It was full of fudge factors to make sure that it

displayed correctly

Page 45: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

45

• 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

• It is a topic worth considering, so it will be covered now

Page 46: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

46

Parametric Equations: An Initial Example

• 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

• In Java, graphical objects are generally determined by (x, y, w, h)—in other words, their location is specified by an (x, y) pair

Page 47: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

47

• The (x, y) 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

Page 48: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

48

• However, if you convert to polar coordinates, 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 θ

Page 49: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

49

• Polar coordinates arise as a natural alternative to Cartesian coordinates

• When presented as given here, they illustrate parameterization

• x and y are expressed in terms of a constant r, and a variable, or parameter, θ, and the expressions for x and y are functions of θ

• There is no confusion about ± values

Page 50: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

50

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

Page 51: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

51

• 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

Page 52: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

52

• 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 w and h are variables or input parameters to 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

Page 53: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

53

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

Page 54: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

54

Page 55: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

55

• 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

• In addition to fitting the display panel, the parameterization should deal with the orientation and location of the parabola in the panel

Page 56: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

56

• The book empirically derives some parametric equations for this scenario

• It approaches the problem by example, using some rules of thumb and simple mathematical observations

• It doesn’t go into any deep theory about parametric equations

• The example code is then implemented using the parametric equations that are derived

Page 57: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

57

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

Page 58: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

58

• 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

Page 59: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

59

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

Page 60: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

60

• 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

Page 61: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

61

• 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

Page 62: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

62

• 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.

Page 63: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

63

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

Page 64: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

64

• 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

Page 65: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

65

• 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

Page 66: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

66

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

Page 67: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

67

• 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

Page 68: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

68

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

Page 69: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

69

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)

Page 70: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

70

• 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)

Page 71: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

71

The End Result of Parameterization

• Together, then, these are the parametric equations:

• x = wt• y = 4h(t – .5)(t – .5)

Page 72: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

72

• The parameter t defines the quadratic relationship between x and y

• The shape of the parabola as graphed will depend on the constant 4 and the variables w and h

Page 73: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

73

• The parameterization of a parabola as explained here is used in the implementation of the original, un-refactored example code given by the book

• It is also used in the new code which results from refactoring using the Façade design pattern

Page 74: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

74

Refactoring to Facade

• Next the book gives an example with an initial design

• It then re-analyzes the design separating functionality into different classes

• One class in the new design, a façade, provides simplified access to the functionality of other classes, a subsystem

Page 75: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

75

• A UML diagram for a monolithic class that shows the flight path of a shell is shown on the next overhead

• Note this similarity with the initial example of the unit on the mediator design pattern:

• You start with a monolithic class and want to break it up

Page 76: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

76

Page 77: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

77

• The monolithic class in the design, ShowFlight, is a subclass of JPanel

• In this application, ShowFlight, in essence, contains “everything”:

• 1. It is responsible for being the application itself

• 2. It presents a panel to display a flight path graphically, wrapping it in a border

• 3. It calculates the parabolic flight path

Page 78: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

78

• Having three different identifiable purposes in a single class is a sign that redesign might be useful

• The task of being an overall application could be implemented as a separate class

• The task of displaying the parabola could be implemented as a separate class

Page 79: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

79

• This is the element that is critical to redesigning using the façade pattern

• The application is graphical• Ultimately, it has to make use of Java swing

features for display• A separate façade class can be developed so

that it was easy to use Java’s graphical toolkit

Page 80: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

80

• The façade pattern would not just be useful in refactoring individual applications

• An organization might have standards for GUI’s that have to be applied across multiple applications

• These standards might be abstracted out and implemented as a façade for Java swing which all applications could use

Page 81: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

81

Original, Monolithic Code

• The following overheads present and discuss the book’s code for the original, monolithic design

• This gives you an idea of the problem context• Due to limitations of time and space, the code

for the book’s refactored design will not be given in the overheads

Page 82: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

82

• It is more important to see the original code so that you can understand how bits and pieces are moved around in the redesign

• The monolithic ShowFlight class has a paintComponent() method, which calculates and displays the parabola

• The code is shown on the next overhead• Note that it is parameterized

Page 83: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

83

• protected void paintComponent(Graphics g)• {• super.paintComponent(g); // paint the background• int nPoint = 101;• double w = getWidth() - 1;• double h = getHeight() - 1;• int[] x = new int[nPoint];• int[] y = new int[nPoint];• for (int i = 0; i < nPoint; i++) {• // t goes 0 to 1• double t = ((double) i) / (nPoint - 1);• // x goes 0 to w• x[i] = (int) (t * w);• // y is h at t = 0 and t = 1, and y is 0 at t = .5• y[i] = (int) (4 * h * (t - .5) * (t - .5));• }• g.drawPolyline(x, y, nPoint);• }

Page 84: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

84

• The monolithic ShowFlight class also contains static utility methods to create a display panel, wrap a title around it, and define a standard font

• The code for these methods is shown on the following overheads

Page 85: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

85

• public static TitledBorder createTitledBorder(String title)• {• TitledBorder tb =

BorderFactory.createTitledBorder(BorderFactory .createBevelBorder(BevelBorder.RAISED), title, TitledBorder.LEFT, TitledBorder.TOP);

• tb.setTitleColor(Color.black);• tb.setTitleFont(getStandardFont());• return tb;• }

Page 86: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

86

• public static JPanel createTitledPanel(String title, JPanel in)• {• JPanel out = new JPanel();• out.add(in);• out.setBorder(createTitledBorder(title));• return out;• }

• public static Font getStandardFont()• {• return new Font("Dialog", Font.PLAIN, 18);• }

Page 87: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

87

• As a monolithic design, ShowFlight is both a constructable class and a class with a main() method

• ShowFlight’s main() method is shown on the overhead following the next one

Page 88: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

88

• main() calls the constructor ShowFlight()• main() sends this flight path as a parameter

when calling createTitledPanel()• The resulting titled panel is then added to the

content pane of a frame• As seen at the outset, it’s the

paintComponent() method of the FlightPath object that contains the logic for generating the parabolic flight path

Page 89: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

89

• public static void main(String[] args)• {• ShowFlight fp = new ShowFlight();• fp.setPreferredSize(new Dimension(300, 200));• JPanel fp_titled = createTitledPanel("Flight Path",

fp);

• JFrame frame = new JFrame("Flight Path for Shell Duds");

• frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);• frame.getContentPane().add(fp_titled);

• frame.pack();• frame.setVisible(true);• }

Page 90: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

90

• The output of the program is shown again on the next overhead

Page 91: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

91

Page 92: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

92

Changes to the Code

• The following changes to the code might lead to a better design:

• 1. Introduce a Function class with a method f() that accepts a double (the value of time) and returns a double (the function’s value)

• Write two subclasses of the Function class which contain the code for parametric equations of x and y respectively

Page 93: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

93

• 2. Move the plotting code of ShowFlight into a PlotPanel class

• Let the PlotPanel class do plotting by receiving objects of the Function class, which generate the needed x and y values

• 3. Move the createTitledPanel() method into a UI façade class

Page 94: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

94

• An incomplete UML diagram which outlines the changes listed above is shown on the following overhead

• It shows a refactoring of the design of ShowFlight into a set of classes so that each has one job

Page 95: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

95

Page 96: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

96

• 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.”

Page 97: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

97

Solution 4.4

Page 98: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

98

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?

Page 99: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

99

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

Page 100: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

100

• 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

• The UI class, the façade, stands in front of graphical elements existing in the Java API

Page 101: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

101

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

Page 102: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

102

Another Example

• Another example will be given next, which is an adaptation of the book’s work.

• It is based on a small graphical application that allows the user to visualize Monte Carlo integration of the parabola y = x2 from 0 to 1

• The graphing problem is somewhat simpler than that of graphing a parabolic flight path, so the plot panel and function implementations are simpler

Page 103: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

103

• This example borrows the book’s UI class directly

• It also makes use of the structure of the book’s PlotPanel class

• However, the PlotPanel class has to be adapted to the Monte Carlo problem as opposed to the parabolic flight path problem

Page 104: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

104

• As it turns out, a better adaptation could have been done

• The results of this example do not display quite as nicely as the book’s example does

• But no additional, complicating adaptations were made

• This is based on pure borrowing• A screen shot of the application’s output is

shown on the next overhead

Page 105: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

105

Page 106: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

106

A UML Diagram for this Example

• A UML diagram of the application is shown on the overhead following the next one

• In the main() method there is a call to UI.NORMAL.createTitledPanel(…, mypanel)

• mypanel is an instance of PlotPanel, which includes the application specific plotting logic

• The UI method returns an instance of JPanel, which has the plot panel added to it

Page 107: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

107

• These are all just structural details• The point of the example is shown to the right

of the diagram• The UI class is the façade class• It provides an entry point into the GUI tools

that exist in Java• In general, a façade class will appear in a

diagram between an application and a toolkit

Page 108: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

108

Page 109: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

109

Code

• The code for the application is given on the following overheads for reference.

• It will probably not be covered in class.

Page 110: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

110

• import java.awt.Dimension;• import javax.swing.JFrame;

• public class MonteCarloRefactored• {• public static void main(String[] args)• {• PlotPanel mypanel = new PlotPanel(101, 50, new T(), new

YFunction());• mypanel.setPreferredSize(new Dimension(300, 200));• JFrame frame = new JFrame("Parametric Parabolic

Function.");• frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);•

frame.getContentPane().add(UI.NORMAL.createTitledPanel("Simulation of Monte Carlo integration.", mypanel));

• frame.pack();• frame.setVisible(true);• }• }

Page 111: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

111

• /* This class is used as given. It is not simplified. The only change is commenting out

• the package name, which may be necessary depending on how you are dealing with packages in

• your development environment. */

• //package com.oozinoz.ui;

• /*• * Copyright (c) 2001, 2005. Steven J. Metsker.• *• * Steve Metsker makes no representations or warranties about• * the fitness of this software for any particular purpose,• * including the implied warranty of merchantability.• *• * Please use this software as you wish with the sole• * restriction that you may not claim that you wrote it.• */

• import java.awt.*;

• import javax.swing.*;• import javax.swing.border.BevelBorder;• import javax.swing.border.TitledBorder;

• /**• * User interface utilities.• */

Page 112: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

112

• public class UI {• protected Font font = new Font("Book Antiqua", Font.PLAIN, 18);

• public static final int STANDARD_PAD = 10;

• public static final UI NORMAL = new UI();

• /**• * @return a standard font that subclasses can override• */• public Font getFont() {• return font;• }

• /**• * @return a standard padding amount that subclasses can override• */• public int getPad() {• return STANDARD_PAD;• }

Page 113: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

113

• /**• * @return a standard button• */• public JButton createButton() {• JButton button = new JButton();• button.setSize(128, 128);• button.setFont(getFont());•

button.setVerticalTextPosition(SwingConstants.BOTTOM);•

button.setHorizontalTextPosition(SwingConstants.CENTER);

• return button;• }

Page 114: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

114

• /**• * @return a standard OK! (or affirmation) button• */• public JButton createButtonOk() {• JButton button = createButton();• button.setIcon(getIcon("images/rocket-large.gif"));• button.setText("Ok!");• return button;• }

• /**• * @return a standard Cancel! (or negation) button• */• public JButton createButtonCancel() {• JButton button = createButton();• button.setIcon(getIcon("images/rocket-large-down.gif"));• button.setText("Cancel!");• return button;• }

• /**• * @return a panel with a standard amount of padding around any controls• */• public JPanel createPaddedPanel() {• JPanel panel = new JPanel();• panel.setBorder(• BorderFactory.createEmptyBorder(getPad(), getPad(), getPad(), getPad()));• return panel;• }

Page 115: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

115

• /**• * @return a panel with a standard amount of padding around any

particular• * control• * @param c• * the control• */• public JPanel createPaddedPanel(Component c) {• JPanel panel = createPaddedPanel();• panel.add(c);• return panel;• }

• public static Icon getIcon(String imageName) {• return new ImageIcon(imageName);• }

• public JList createList(Object[] contents) {• JList result = new JList(contents);• result.setFont(getFont());• result.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);• return result;• }

Page 116: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

116

• /**• * @return a titled border with the given title.• */• public TitledBorder createTitledBorder(String title) {• TitledBorder border = BorderFactory.createTitledBorder(• BorderFactory.createBevelBorder(BevelBorder.RAISED),• title,• TitledBorder.LEFT,• TitledBorder.TOP);• border.setTitleColor(Color.black);• border.setTitleFont(getFont());• return border;• }

• /**• * @return a new panel that wraps a titled border around a given

panel.• */• public JPanel createTitledPanel(String title, JPanel in) {• JPanel out = new JPanel();• out.add(in);• out.setBorder(createTitledBorder(title));• return out;• }• }

Page 117: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

117

• //package com.oozinoz.ui;

• /*• * Copyright (c) 2001, 2005. Steven J. Metsker.• *• * Steve Metsker makes no representations or warranties about• * the fitness of this software for any particular purpose,• * including the implied warranty of merchantability.• *• * Please use this software as you wish with the sole• * restriction that you may not claim that you wrote it.• */

• import java.awt.Color;• import java.awt.Graphics;• import java.awt.geom.Ellipse2D;• import java.util.Random;• import java.awt.Graphics2D;

• import javax.swing.JPanel;

• //import com.oozinoz.function.Function;

Page 118: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

118

• /**• * This class displays pair of functions in a panel. The functions• * are x and y functions, parameterized by time. As the panel plots,• * time goes 0 to 1.• */• public class PlotPanel extends JPanel• {• private int points;• private int trials;• private int count = 0;• private double area = 0.0;

• private int[] xPoints;• private int[] yPoints;

• private Function xFunction;• private Function yFunction;

Page 119: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

119

• /**• * Create a plot calculated from the provided x and y functions.• * These functions must be functions of time; time goes 0 to 1• * as the panel plots.• */• public PlotPanel(int nPoint, int nTrial, Function xFunc,

Function yFunc)• {• points = nPoint;• trials = nTrial;• xPoints = new int[points];• yPoints = new int[points];• xFunction = xFunc;• yFunction = yFunc;• setBackground(Color.WHITE);• }

Page 120: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

120

• protected void paintComponent(Graphics graphics)• {• Graphics2D g2 = (Graphics2D) graphics;• Random mygenerator = new Random();• double randx, randy;• double circlex, circley, calcy;• int count = 0;• Ellipse2D.Double mycircle;

• double w = getWidth() - 1;• double h = getHeight() - 1;

• Color mycolor = new Color(0.75F, 0.0F, 0.75F);• g2.setColor(mycolor);

• for (int i = 0; i < points; i++)• {• double t = ((double) i) / (points - 1);• xPoints[i] = (int) (xFunction.f(t) * w);• yPoints[i] = (int) (h * (1 - yFunction.f(t)));• }

• g2.drawPolyline(xPoints, yPoints, points);

• /* New junk to do Monte Carlo circles. */

• g2.setColor(Color.blue);

Page 121: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

121

• for(int i = 0; i < trials; i++)• {• randx = mygenerator.nextDouble();• randy = mygenerator.nextDouble();

• circlex = w * xFunction.f(randx) - 5;• circley = h * (1 - randy) - 5;

• calcy = h * (1 - yFunction.f(randx)) - 5;

• mycircle = new Ellipse2D.Double(circlex, circley, 10, 10);

• if((1 - randy) > (1 - yFunction.f(randx)))• {• count++;• g2.fill(mycircle);• }• else• {• g2.draw(mycircle);• }• }

• area = (double) count / (double) trials;• g2.setColor(mycolor);• g2.drawString("The area under the curve is " + area, 15, 15);• }• }

Page 122: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

122

• /* This is a simplification of the book's code. */

• public abstract class Function• {• public Function()• {• }

• public abstract double f(double t);• }• /* This is a simplification of the book's code. */

• public class T extends Function• {• public T()• {• }

• public double f(double t) {• return t;• }• }• /* This is a simplification of the book's code. */

• public class YFunction extends Function• {• public YFunction()• {• }

• public double f(double t)• {• return (t * t);• }• }

Page 123: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

123

A UML Diagram for the Pattern from Lasater

• Lasater’s UML diagram is given on the next overhead.

• Using that author’s terminology, the façade class provides an entry point to one or more subsystems

• The client code isn’t shown• This emphasizes the idea that the façade

“stands in front of” something, but the reality is that it stands between things

Page 124: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

124

Page 125: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

125

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

Page 126: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

126

• The façade design pattern is supposed to make a toolkit easier to use

• The mediator design pattern is supposed to capture relationships between the base objects

• Structurally, in simplest form, the diagrams for both patterns would consist of 3 connected boxes

• The difference in purpose is represented by different connectors between them

Page 127: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

127

• 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”

Page 128: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

128

• 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 mediator, 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

Page 129: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

129

Page 130: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

130

Summary

• A façade class is supposed to make a subsystem easier to use

• The term façade suggests that this class stands in front of the subsystem

• The reality is that a façade, like a mediator, is a class in the middle

• It stands between client code and the subsystem

Page 131: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

131

• The examples of the pattern essentially had to do with writing a class that made the Java GUI tools easier to use

• More generally, this could also arise with code that you yourself are writing

Page 132: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

132

• Individual classes are limited to one basic purpose

• Some larger purpose may be served by the collective contents of a group of classes

• The problem is that a programmer may not easily comprehend and be able to use all of the functionality in the group of classes

Page 133: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

133

• A façade class can then be devised which serves as a comprehensible (limited) entry point into the richness that lies below

• In practice, this may mean a subset of all of the methods, perhaps other simplified methods, methods with fewer parameters, and so on

Page 134: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

134

• The façade should be configurable and reusable

• This means the façade should of practical use in real code

• The façade may embody a certain set of standards for using the underlying subsystem

Page 135: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

135

• On the other hand, a programmers eventually might also want to learn the details of the underlying system in order to use more of its features

• Having learned about the subsystem, programmers might also handcraft their own façade into it for use with certain applications

• Recall that a utility is essentially a façade, but with only static methods in it

Page 136: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

136

• A demo can serve a similar purpose to a façade

• It gives an example of how to use a subset of the features of the toolkit

• It’s then up to the programmer to take the demo as an example when writing code to use the basic features of the toolkit

Page 137: Unit 9 Façade Summary prepared by Kirk Scott 1. Emmer From Wikipedia, the free encyclopedia Emmer wheat (Triticum dicoccum), also known as farro especially

137

The End