Professional Documents
Culture Documents
42 DesignPatterns-Part2
42 DesignPatterns-Part2
42 DesignPatterns-Part2
Example:
An actor may depress a button signifying End Sale, but this is
only used to indicate a desired system operation.
(Note that classes such as window, applet, widget, view, document, etc. are not
used. These kinds of classes do not fulfill the tasks associated with system
events; rather, these typically receive events and delegate them to some kind of
controller.)
endSale()
enterItem()
makeNewSale()
makePayment()
…
Select some class name that suggests a ‘cover’ or façade over the
other layers of the application, and that provides the main point
of service calls from the UI layer down to other layers.
Note: ‘Façade’ Controllers are used when there are not too
many events to control, and there is not sufficient need for
alternating controllers.
The View is an interface that displays data (the model) and routes
user commands (events) to the Presenter to act upon that data.
The Presenter acts upon the model and the view. It retrieves data
from repositories (the model), and formats it for display in the
view.
• Here, the most common pattern of access is Façade, a GoF design pattern.
• We have a public façade object defining the services for the subsystem, and clients
collaborate with the façade, not internal subsystem components.
• The façade should not normally expose many low-level operations. Rather, it exposes
a small number of high-level operations – the coarse-grained services.
• A façade does not normally do its own work. Rather, it is consolidator or mediator to the
underlying subsystem objects, which do the work.
• The façade is a wrapper and single point of access into the ‘rules engine’ of a subsystem.
Below: The façade class abstracts Packages 1,2, and 3 from the rest of the
application.
Clients use the façade pattern to access resources from the packages.
They are going to ‘doStuff’ with the objects c1, c2, and c3
We create a special class called a <<Façade>>, which simplifies* the use of the package.
The Façade class simply contains a simplified set of public methods.
*
Result is the creation of a class containing the most often used / needed facilities in the
package. Thus other client elements desiring services of this complex
package can avail themselves of the Façade class, where it is likely what
services they want are simpler to acquire.
Since we are talking about a complex package, other clients that need some of the
complexities of the package can access these services as they normally would – via the
public interfaces of these other classes.
Some design elements, like subsystems, may need to interact with the airline system but
don’t want to be exposed to any changes that might be made to it (via dependencies).
So, we can define and Airline class to be a <<Façade>> that contains methods (access) to
the most important query and booking operations in other classes in the airline system.
Thus any dependency arising from this relation is only on the Façade class.
Here, above, via the Airline façade class, we have access to two other classes from other
likely complex packages
• Context:
—It is very common to find classes for which only one
instance should exist (singleton)
- Examples: a Main Window; Company or University class.
• Problem:
—How do you ensure that it is never possible to create
more than one instance of a singleton class?
• Forces:
—The use of a public constructor cannot guarantee
that no more than one instance will be created.
—The singleton instance must also be accessible to
all classes that require it. But there must be only one!
© Lethbridge/Laganière 2001 Chapter 6: Using design patterns 27
Singleton
«Singleton»
- theInstance
Have a private class variable, getInstance
possibly called, ‘theInstance.’
This stores the instance.
Company
Then have a public class method if (theCompany==null)
theCompany theCompany= new Company();
(static method) possibly called,
‘getInstance.’ Company «private»
getInstance return theCompany;
First time method is called, it creates Here, Company class may embody several