SlideShare a Scribd company logo
Design Patterns
EECS 3311
Song Wang
[email protected]
eecs.yorku.ca/~wangsong/
Acknowledge
• Some of the covered materials are based on SOEN 344 at
Concordia, SE463 at UW, SENG321 at UVic, CSE331 at
University of Washington and previous EECS3311 offerings:
• Nikolaos Tsantalis, Jo Atlee, Marty Stepp, Mike Godfrey,
Jonathan S. Ostroff, M. Ernst, S. Reges, D. Notkin, R.
Mercer, Davor Svetinovic, Jack Jiang, Jackie Wang
Outlines
▪ Java Design Patterns (Behavioral Patterns)
▪ Visitor Design Pattern
▪ Iterator Pattern
▪ State Design Pattern
▪ Observer design patterns
Pattern: Visitor
What’s It All About?
• Allows for new operations to be defined and used
on elements of an object structure without
changing the contents of those elements.
• The Key is Double Dispatch
• choosing the method to invoke based both on receiver
and argument types
Where Applicable
• Rarely Changing Object Structures
• Using Unrelated Operations
• Many Classes with Differing Interfaces
Visitor design pattern
• Add an accept(Visitor) method to the "element" hierarchy
• Create a "visitor" base class w/ a visit() method for every
"element" type
• Create a "visitor" derived class for each "operation" to do on
"elements"
• Client creates "visitor" objects and passes each
to accept() calls
How it Works
• Concrete Object
Structure
• Assume Rarely
Changing
• Bank Accounts
Add an Inquiry Operation
• Check balances and
display account summary
• Don’t Want to Change
Structure
• Create a Visitor Structure
• Account Visitor
Account Visitor Interface
Inquiry Visitor
Account Structure Change
Account Interface
Checking Account
Savings Account
Main Method
Pattern: Iterator
What’s It All About?
• Provide a way to access the elements of an
aggregate object sequentially without exposing its
underlying representation.
• Separate traversal code from the data collection
itself.
Java Collections
Figure source: https://techvidvan.com/tutorials/wp-
content/uploads/sites/2/2020/03/collection-framework-
hierarchy-in-java.jpg
Basic Structure
• Iterator keeps a Cursor to current location in a collection.
• Most collections store their elements in simple lists. However,
some of them are based on stacks, trees, graphs and other
complex data structures.
• But no matter how a collection is structured, it must provide
some way of accessing its elements so that other code can use
these elements. There should be a way to go through each
element of the collection without accessing the same
elements over and over.
Visualizing iterator pattern at runtime
ArrayList
ArrayList
Person
Iterator
isDonefirst
Iterator for A Binary Tree
Pattern: State Model
Motivating Problem
Consider the reservation panel of an online booking
system:
Motivating Problem
State Transition Diagram
State Pattern
• State Design Pattern allows an object to change
its behavior when the internal state of that object
changes.
• The state design pattern is generally used in
cases where an object depends on its state and
its behavior must be changed during run time
depending on its internal state.
• A typical behavioral design pattern;
• Context: Defines an interface to client to interact. It maintains
references
to concrete state object which may be used to define current
state of
object.
• State: Defines interface for declaring what each concrete state
should do.
• ConcreteState: Provides implementation for methods defined
in State.
State Pattern Diagram
A Simple Example
In this example we will model a mobile state scenario.
With respect to alerts, a mobile can be in different
states. For example, vibration and silent. Based on
this alert state, behavior of the mobile changes when
an alert is to be done.
Source: https://www.geeksforgeeks.org/state-design-pattern/
The key classes
• The State Interface
interface MobileAlertState
{
public void alert(AlertStateContext ctx);
}
The key classes
• Context
class AlertStateContext
{
private MobileAlertState currentState;
public AlertStateContext()
{
currentState = new Vibration();
}
public void setState(MobileAlertState state)
{
currentState = state;
}
public void alert()
{
currentState.alert(this);
}
}
The key classes
• States
class Vibration implements MobileAlertState
{
@Override
public void alert(AlertStateContext ctx)
{
System.out.println("vibration...");
}
}
class Silent implements MobileAlertState
{
@Override
public void alert(AlertStateContext ctx)
{
System.out.println("silent...");
}
}
The key classes
• Demo
class StatePattern
{
public static void main(String[] args)
{
AlertStateContext stateContext = new AlertStateContext();
stateContext.alert();
stateContext.alert();
stateContext.setState(new Silent());
stateContext.alert();
stateContext.alert();
stateContext.alert();
}
}
Output:
vibration...
vibration...
silent...
silent...
silent...
Pattern: Observer
Observer Pattern
• Defines a “one-to-many” dependency between
objects so that when one object changes state,
all its dependents are notified and updated
automatically
• a.k.a Dependence mechanism / publish-
subscribe / broadcast / change-update
Subject & Observer
• Subject
• the object which will frequently change its state and
upon which other objects depend
• Observer
• the object which depends on a subject and updates
according to its subject's state.
Observer Pattern - Example
a b c
60
y
x
5030
30
20
10
z 801010 a b c
a
b
c
a = 50%
b = 30%
c = 20%
change notification
requests, modifications
Observers
Subject
Observer Pattern - Working
A number of Observers “register” to receive notifications of
changes to the
Subject. Observers are not aware of the presence of each other.
When a certain event or “change” in Subject occurs, all
Observers are “notified’.
Observer Pattern - Key Players
• Subject
• has a list of observers
• Interfaces for attaching/detaching an observer
• Observer
• An updating interface for objects that gets notified of changes
in a
subject
• ConcreteSubject
• Stores “state of interest” to observers
• Sends notification when state changes
• ConcreteObserver
• Implements updating interface
Observer Pattern - UML
Subject
attach (Observer)
detach (Observer)
Notify ()
Observer
Update()
ConcreteObserver
Update()
observerState
ConcreteSubject
SetState()
GetState()
subjectState
observers
subject
For all x in observers{
x.Update(); }
observerState=
subject.getState();
Observer Pattern -
Collaborations
:ConcreteSubject :ConcreteObserver-1 :ConcreteObserver-2
GetState()
Notify()
Update()
SetState()
GetState()
Update()
Observer Pattern - Implementation
interface Observer {
void update (Observable sub, Object arg)
// repaint the pi-chart
}
Java terminology for Subject.
public void addObserver(Observer o) {}
public void deleteObserver (Observer o) {}
public void notifyObservers(Object arg) {}
class Observable {
…
}
public boolean hasChanged() {}
Observer Pattern - Consequences
• Loosely Coupled
• Reuse subjects without reusing their observers, and vice versa
• Add observers without modifying the subject or other
observers
• Abstract coupling between subject and observer
• Concrete class of none of the observers is known
• Support for broadcast communication
• Subject doesn’t need to know its receivers
• Unexpected updates
• Can be blind to changes in the system if the subject is changed
(i.e.
doesn’t know “what” has changed in the subject)
Exercise
• Design the system for the flight online booking
system;
• Draw the class diagram of the online booking
system;
The idea of 'tone at the top explains the mindset of business
executives' driving principles and an ethical atmosphere within
the business. International focus has been paid to the link
between bribery and an organization's 'tone at the top.' In recent
years. Tone at the top is the moral environment generated in the
environment by the management of the organization. Any tonal
collection that has a trickle-down impact on business workers
Repetition
It's necessary to repeat. Leaders and staff around the
organization must understand and recognize the message
regularly. The leadership must speak about it and perform up to
the ideals of the organization.
Establish a Code of Ethics
When management makes ethical values clear and speaks freely,
they mean that honesty is appreciated. The Business Code of
Ethics must be read and signed annually by both employees and
vendors. New staff should still have to read the paper and sign
it before they begin their job. This means that each employee
respects and meets the same ethical standards (Kang, 2018).
Annual Fraud and Ethics Training
Practice sessions should warn workers about the business and
the legislation prohibiting activities. This helps workers
essentially escape circumstances that can lead to bad behavior.
New staff may also be expected to attend ethics training at
business startup. Regularly scheduled ethics training can help
managers detect possible issues and prevent compromises (Gunz
& Thorne, 2014).
Integrity
Employees typically form their job ethic following top
management. Ethical judgments by management can require
both written and physical communications. Besides that, the
management should be modeled upon openness, integrity, and
open contact. In the eyes of staff, failure to maintain honesty
will create uncertainty. Employees should not behave ethically
if the management does not handle people with dignity.
Work ethic
When senior management is late at work, leaves early, or
regularly leaves work, staff may believe this is appropriate to
copy. If management orders remain late or arrive early, workers
should still work for certain hours. For management to be
receptive, hardworking, and committed, these qualities must
also be presented. The highest-ranking, hardworking, and agile
staff of the business should be the top management.
Management should be in a position to collaborate with others,
constructive and fruitful. A working tone ensures that ideas
from others are to be examined during everyday procedures.
Staff must be able to disregard their views and listen to other
people's thoughts and viewpoints. This suggests that workers,
even though they are distinct from their own, think for their
thoughts and viewpoints.
References
The Tone at the top here means the values and ethics followed
by the people in the top of the hierarchy chart. The people at the
top level in hierarchy are board of directors, executives and the
management at top level. The factors and actions that are
performed by the people at top levels largely affects the
organizations because these people at the top level will lead by
an example. I would like to discussion an example here in this
discussion forum which the entire class has worked on a
previous case study on Starbucks. The CEO of Starbucks has
fired an employee and closed out many stores for time being to
give its employees trainings on ethics and cultural values. The
reason for these trainings and firing of an employee was
because a manager has passed an inappropriate comment on one
of its customers and has asked that customer to leave the store
as they were sitting ideally without purchasing anything from
the store.
People at higher management level were involved in this
discussion and made sure that this incident was taken for
granted and assured customers of Starbucks can sit in the store
even if they are not making any purchases. The top-level
employees of Starbucks lead by an example and set the right
level of attitude. This is a perfect example of tone of the top as
the people in higher management has acted in the right way and
they have led by an example so that the other employees and
managers can follow their footsteps. Today, we all know how
Starbucks is strong in their ethical values and one of the
organizations that people would love to work because of a good
ethical culture.
Source:
Design Patterns
EECS 3311
Song Wang
[email protected]
eecs.yorku.ca/~wangsong/
Overview of Last Lectures
• Control Flow Graphs
• If Statements
• Loop Statements
• Code Coverage
• JaCoCo
• UML
• Use Case Diagram
• Activity Diagram
• Sequence Diagram
• Class Diagram
• different relations
• Only your first Attempt counts
• Average of Quiz 3 is 85.8%
Quiz 3
84.4
84.6
84.8
85
85.2
85.4
85.6
85.8
86
86.2
Quiz 1 Quiz 2 Quiz 3
Average
Average
Acknowledge
• Some of the covered materials are based on SE463
at UW, SENG321 at UVic, CSE331 at University of
Washington and previous EECS3311 offerings:
• Jo Atlee, Dan Berry, Daniela Damian, Marty Stepp, Mike
Godfrey, Jonathan S. Ostroff, M. Ernst, S. Reges, D. Notkin,
R. Mercer, Davor Svetinovic, Jack Jiang, Jackie Wang
Outlines
▪ Introduction to Design Patterns
▪ Pattern’s Elements
▪ Types of Design Patterns
▪ Java Design Patterns
▪ The Singleton Pattern
▪ The Factory Pattern
▪ The Builder Pattern
▪ The Prototype Pattern
▪ The Adapter Pattern
▪ The Bridge Pattern
▪ The Composite Pattern
▪ Visitor Design Pattern
▪ Iterator Pattern
▪ State Design Pattern
▪ Event-driven pattern
▪ Observer design patterns
Design patterns
• Design pattern: A standard solution to a common
software problem in a context.
• describes a recurring software structure or idiom
• is abstract from any particular programming language
• identifies classes and their roles in the solution to a
problem
• In 1990 a group called the Gang of Four or "GoF"
(Gamma, Helm, Johnson, Vlissides) compile a
catalog of design patterns
• 1995 book Design Patterns:
Elements of Reusable Object-Oriented
Software is a classic of the field
In general, a pattern has four essential elements.
▪ The pattern name
▪ The problem
▪ The solution
▪ The consequences
Pattern’s Elements
The pattern name is a handle we can use to describe a
design problem, its solutions, and consequences in a
word or two.
▪ Naming a pattern immediately increases the design
vocabulary. It lets us design at a higher level of
abstraction.
▪ Having a vocabulary for patterns lets us talk about
them.
▪ It makes it easier to think about designs and to
communicate them and their trade-offs to others.
Pattern’s Elements – The Pattern Name
The problem describes when to apply the pattern.
▪ It explains the problem and its context.
▪ It might describe specific design problems such as how to
represent algorithms as objects.
▪ It might describe class or object structures that are
symptomatic of an inflexible design.
▪ Sometimes the problem will include a list of conditions that
must be met before it makes sense to apply the pattern.
Pattern’s Elements – The Problem
The solution describes the elements that make up the design,
their
relationships, responsibilities, and collaborations.
▪ The solution doesn't describe a particular concrete design or
implementation, because a pattern is like a template that can be
applied in many different situations.
▪ Instead, the pattern provides an abstract description of a
design
problem and how a general arrangement of elements (classes
and objects in our case) solves it.
Pattern’s Elements – The
Solution
The consequences are the results and trade-offs of applying the
pattern.
▪ The consequences for software often concern space and time
trade-offs.
▪ They may address language and implementation issues as well.
▪ Since reuse is often a factor in object-oriented design, the
consequences of a pattern include its impact on a system's
flexibility, extensibility, or portability.
▪ Listing these consequences explicitly helps you understand
and
evaluate them.
Pattern’s Elements – The Consequences
Benefits of using patterns
• Patterns give a design common vocabulary for software
design:
• Allows engineers to abstract a problem and talk about that
abstraction in isolation from its implementation.
• A culture; domain-specific patterns increase design speed.
• Capture expertise and allow it to be communicated:
• Promotes design reuse and avoid mistakes.
• Makes it easier for other developers to understand a system.
• Improve documentation (less is needed):
• Improve understandability (patterns are described well, once).
Gang of Four (GoF) patterns
Erich Gamma, Richard Helm, Ralph Johnson and John
Vlisides in their Design Patterns book define 23 design
patterns divided into three types:
▪ Creational patterns are ones that create objects for you,
rather than having you instantiate objects directly. This
gives your program more flexibility in deciding which
objects need to be created for a given case.
▪ Structural patterns help you compose groups of objects
into larger structures, such as complex user interfaces or
accounting data.
▪ Behavioral patterns help you define the communication
between objects in your system and how the flow is
controlled in a complex program.
Gang of Four (GoF) patterns (cont.)
• Creational Patterns (abstracting the object-instantiation
process)
• Singleton Factory
• Builder Prototype …
• Structural Patterns (how objects/classes can be combined)
• Adapter Bridge Composite
• Decorator Facade Flyweight
• Proxy …
• Behavioral Patterns (communication between objects)
• Command Interpreter Iterator
• Observer State
• Visitor Even-driven …
Pattern: Singleton
A class that has only a single instance
Singleton pattern
• singleton: An object that is the only object of its type.
(one of the most known / popular design patterns)
• Ensuring that a class has at most one instance.
• Providing a global access point to that instance.
• e.g. Provide an accessor method that allows users to see the
instance.
• Benefits:
• Takes responsibility of managing that instance away from the
programmer (illegal to construct more instances).
• Saves memory.
• Avoids bugs arising from multiple instances.
Restricting objects
• One way to avoid creating objects: use static methods
• Examples: Math, System
• Is this a good alternative choice? Why or why not?
• Disadvantage: Lacks flexibility.
• Static methods can't be passed as an argument, nor
returned.
• Disadvantage: Cannot be extended.
• Static methods can't be subclassed and overridden like an
object's methods could be.
• Make constructor(s) private so that they can not be
called from outside by clients.
• Declare a single private static instance of the class.
• Write a public getInstance() or similar method that
allows access to the single instance.
• May need to protect / synchronize this method to ensure
that it will work in a multi-threaded program.
Implementing Singleton
Singleton sequence diagram
• Class RandomGenerator generates random numbers.
public class RandomGenerator {
private static final RandomGenerator gen =
new RandomGenerator();
public static RandomGenerator getInstance() {
return gen;
}
private RandomGenerator() {}
...
}
Singleton example
• Can wait until client asks for the instance to create it:
public class RandomGenerator {
private static RandomGenerator gen = null;
public static RandomGenerator getInstance() {
if (gen == null) {
gen = new RandomGenerator();
}
return gen;
}
private RandomGenerator() {}
...
}
Lazy initialization
Pattern: Factory
The Factory pattern returns an instance of one of several
possible
classes depending on the data provided to it.
▪ Here, x is a base class and classes xy and xz are derived from
it.
▪ The Factory is a class that decides which of these subclasses
to
return depending on the arguments you give it.
▪ The getClass() method passes in some value abc, and returns
some instance of the class x. Which one it returns doesn't matter
to
the programmer since they all have the same methods, but
different
implementations.
How does it Work?
▪ Let's consider a simple case where we could use a Factory
class. Suppose we
have an entry form and we want to allow the user to enter his
name either as
“firstname lastname” or as “lastname, firstname”(a comma in-
between).
▪ Let’s make the assumption that we will always be able to
decide the name
order by whether there is a comma between the last and first
name.
class Namer { //a class to take a string apart into two names
protected String last; //store last name here
protected String first; //store first name here
public String getFirst() {
return first; //return first name
}
public String getLast() {
return last; //return last name
}
}
The Base Class
In the FirstFirst class, we assume that everything before the l ast
space is part of the first name.
class FirstFirst extends Namer {
public FirstFirst(String s) {
int i = s.lastIndexOf(" "); //find separating space
if (i > 0) {
first = s.substring(0, i).trim(); //left = first name
last =s.substring(i+1).trim(); //right = last name
} else {
first = “” // put all in last name
last = s; // if no space
}
}
}
The First Derived Class
In the LastFirst class, we assume that a comma delimits the last
name.
class LastFirst extends Namer { //split last, first
public LastFirst(String s) {
int i = s.indexOf(","); //find comma
if (i > 0) {
last = s.substring(0, i).trim(); //left= last name
first = s.substring(i + 1).trim(); //right= first name
} else {
last = s; // put all in last name
first = ""; // if no comma
}
}
}
The Second Derived Class
The Factory class is relatively simple. We just test for the
existence
of a comma and then return an instance of one class or the
other.
class NameFactory {
//returns an instance of LastFirst or FirstFirst
//depending on whether a comma is found
public Namer getNamer(String entry) {
int i = entry.indexOf(","); //comma determines name order
if (i>0)
return new LastFirst(entry); //return one class
else
return new FirstFirst(entry); //or the other
}
}
Building the Factory
NameFactory nfactory = new NameFactory();
String sFirstName, sLastName;
….
private void computeName() {
//send the text to the factory and get a class back
namer = nfactory.getNamer(entryField.getText());
//compute the first and last names using the returned class
sFirstName = namer.getFirst();
sLastName = namer.getLast();
}
Using the Factory
You should consider using a Factory pattern when:
▪ Create objects dynamically
▪ A class can’t anticipate which kind of class of objects it must
create.
▪ A class uses its subclasses to specify which objects it creates.
▪ You want to localize the knowledge of which class gets
created.
There are several similar variations on the factory pattern to
recognize:
▪ The base class is abstract and the pattern must return a
complete working class.
▪ The base class contains default methods and is only subclassed
for cases where
the default methods are insufficient.
▪ Parameters are passed to the factory telling it which of several
class types to
return. In this case the classes may share the same method
names but may do
something quite different.
When to Use a Factory Pattern
The Abstract Factory pattern is one level of abstraction higher
than the factory
pattern. This pattern returns one of several related classes, each
of which can
return several different objects on request. In other words, the
Abstract Factory
is a factory object that returns one of several factories.
One classic application of the abstract factory is the case where
your system
needs to support multiple “look-and-feel” user interfaces, such
as Windows, Motif
or Macintosh:
▪ You tell the factory that you want your program to look like
Windows and it
returns a GUI factory which returns Windows-like objects.
▪ When you request specific objects such as buttons, check
boxes and
windows, the GUI factory returns Windows instances of these
visual
interface components.
The Abstract Factory Pattern
How does it Work?
Suppose you are writing a program to plan the layout of
gardens. These could be
annual gardens, vegetable gardens or perennial gardens.
However, no matter
which kind of garden you are planning, you want to ask the
same questions:
▪ What are good border plants?
▪ What are good center plants?
▪ What plants do well in partial shade?
We want a base Garden class that can answer these questions:
public abstract class Garden {
public abstract Plant getCenter();
public abstract Plant getBorder();
public abstract Plant getShade();
}
A Garden Maker Factory
The Plant class simply contains and returns the plant name:
public class Plant {
String name;
public Plant(String pname) {
name = pname; //save name
}
public String getName() {
return name;
}
}
The Plant Class
A Garden class simply returns one kind of each plant. So, for
example, for the
vegetable garden we simply write:
public class VegieGarden extends Garden {
public Plant getShade() {
return new Plant("Broccoli");
}
public Plant getCenter() {
return new Plant("Corn");
}
public Plant getBorder() {
return new Plant("Peas");
}
}
A Garden Class
We create a series of Garden classes - VegieGarden,
PerennialGarden, and
AnnualGarden, each of which returns one of several Plant
objects. Next, we
construct our abstract factory to return an object instantiated
from one of these
Garden classes and based on the string it is given as an
argument:
class GardenMaker {
//Abstract Factory which returns one of three gardens
private Garden gd;
public Garden getGarden(String gtype) {
gd = new VegieGarden(); //default
if(gtype.equals("Perennial"))
gd = new PerennialGarden();
if(gtype.equals("Annual"))
gd = new AnnualGarden();
return gd;
}
}
A Garden Maker Class – The Abstract
Factory
▪ One of the main purposes of the Abstract Factory is that it
isolates the concrete classes that are generated.
▪ The actual class names of these classes are hidden in the
factory
and need not be known at the client level at all.
▪ Because of the isolation of classes, you can change or
interchange
these product class families freely.
▪ Since you generate only one kind of concrete class, this
system
keeps you for inadvertently using classes from different
families
of products.
▪ While all of the classes that the Abstract Factory generates
have
the same base class, there is nothing to prevent some derived
classes from having additional methods that differ from the
methods of other classes.
Consequences of Abstract Factory
Pattern: Builder
The Builder Pattern separates the construction of a complex
object from its
representation so that the same construction process can create
different
representations.
▪ Builder - specifies an interface for creating parts of a Product
object.
▪ ConcreteBuilder - constructs and assembles parts of the
product by
implementing the Builder interface. Also, it defines and keeps
track of the
representation it creates and provides an interface for retrieving
the product .
▪ Director - constructs an object using the Builder interface.
▪ Product - represents the complex object under construction.
How does it Work?
▪ The client creates the Director object and configures it with
the desired
Builder object.
▪ Director notifies the builder whenever a part of the product
should be built.
▪ Builder handles requests from the director and adds parts to
the product.
▪ The client retrieves the product from the builder.
The following interaction diagram illustrates how Builder and
Director cooperate
with a client.
How does it Work?
Use the Builder pattern when:
▪ The algorithm for creating a complex object should be
independent of the parts that make up the object and how they
are assembled.
▪ The construction process must allow different representations
for
the object that is constructed.
Applicability of Builder Pattern
▪ A Builder lets you vary the internal representation of the
product it builds. It
also hides the details of how the product is assembled.
▪ Each specific builder is independent of the others and of the
rest of the
program. This improves modularity and makes the addition of
other builders
relatively simple.
▪ Because each builder constructs the final product step-by-step,
depending on
the data, you have more control over each final product that a
Builder
constructs.
▪ A Builder pattern is somewhat like an Abstract Factory pattern
in that
both return classes made up of a number of methods and
objects. The main
difference is that while the Abstract Factory returns a family of
related
classes, the Builder constructs a complex object step by step
depending on the
data presented to it.
Consequences of Builder Pattern
/** "Product" */
class Pizza {
private String dough = ""; //basic element
private String sauce = ""; //basic element
private String topping = ""; //basic element
public void setDough(String dough) {
this.dough = dough; }
public void setSauce(String sauce) {
this.sauce = sauce; }
public void setTopping(String topping) {
this.topping = topping; }
}
Pizza Builder
/** "Abstract Builder" */
abstract class PizzaBuilder {
protected Pizza pizza;
public Pizza getPizza() {
return pizza; }
public void createNewPizzaProduct() {
pizza = new Pizza(); }
public abstract void buildDough();
public abstract void buildSauce();
public abstract void buildTopping();
}
Pizza Builder
/** "ConcreteBuilder" */
class HawaiianPizzaBuilder extends PizzaBuilder {
public void buildDough() {
pizza.setDough("cross"); }
public void buildSauce() {
pizza.setSauce("mild"); }
public void buildTopping() {
pizza.setTopping("ham+pineapple"); }
}
/** "ConcreteBuilder" */
class SpicyPizzaBuilder extends PizzaBuilder {
public void buildDough() {
pizza.setDough("pan baked"); }
public void buildSauce() {
pizza.setSauce("hot"); }
public void buildTopping() {
pizza.setTopping("pepperoni+salami"); }
}
Pizza Builder
/** "Director" */
class Waiter {
private PizzaBuilder pizzaBuilder;
public void setPizzaBuilder(PizzaBuilder pb) {
pizzaBuilder = pb; }
public Pizza getPizza() {
return pizzaBuilder.getPizza(); }
public void constructPizza() {
pizzaBuilder.createNewPizzaProduct();
pizzaBuilder.buildDough();
pizzaBuilder.buildSauce();
pizzaBuilder.buildTopping();
}
}
Pizza Builder
/** A customer ordering a pizza. */
class BuilderExample {
public static void main(String[] args) {
Waiter waiter = new Waiter();
PizzaBuilder hawaiianPizzaBuilder = new
HawaiianPizzaBuilder();
PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
waiter.setPizzaBuilder( hawaiianPizzaBuilder );
waiter.constructPizza();
Pizza pizza = waiter.getPizza();
waiter.setPizzaBuilder(spicyPizzaBuilder);
waiter.constructPizza();
Pizza pizza2 = waiter.getPizza();
}
}
Pizza Builder
Pattern: Prototype
▪ A prototype is a template of any object before the actual
object is
constructed.
▪ The Prototype pattern specifies the kinds of objects to create
using a
prototypical instance, and create new objects by copying this
prototype.
▪ A Protoype pattern is used when creating an instance of a
class is very time-
consuming or complex in some way. Then, rather than creating
more
instances, you make copies of the original instance and modify
them as
appropriate.
Example:
▪ Let’s consider the case of an extensive database where you
need to make a
number of queries to construct an answer. Once you have this
answer as a
table or ResultSet, you might like to manipulate it to produce
other answers
without issuing additional queries.
Definition & Applicability
You can make a copy of any Java object using the clone
method.
Jobj j1 = (Jobj)j0.clone();
The clone method always returns an object of type Object. You
must
cast it to the actual type of the object you are cloning. There are
three other significant restrictions on the clone method:
▪ It is a protected method and can only be called from within the
same class or the module that contains that class.
▪ You can only clone objects which are declared to implement
the Cloneable interface.
▪ Objects that cannot be cloned throw the CloneNotSupported
Exception.
Cloning in Java - I
This suggests packaging the actual clone method inside the
class where it can
access the real clone method:
public class SwimData implements Cloneable {
public Object clone()
{
try{
return super.clone();
}
catch(Exception e) {
System.out.println(e.getMessage());
return null;
}
}
}
Cloning in Java - II
▪This implementation has the advantage of encapsulating the
try-catch block
inside the public clone method.
▪Note that if you declare this public method to have the same
name “clone,” it
must be of type Object, since the internal protected method has
that signature. We
could, however, change the name and do the typecasting within
the method
instead of forcing it onto the user:
public SwimData cloneMe() {
try{
return (SwimData)super.clone();
}
catch(Exception e) {
System.out.println(e.getMessage());
return null;
}
}
Cloning in Java - III
Shallow vs. Deep (Object) Copy in Java
Thanks: https://dzone.com/articles/java-copy-shallow-vs-deep-
in-which-you-will-swim
A reference copy, as the name implies, creates a
copy of a reference variable pointing to an object.
An object copy creates a copy of the object itself.
Say we have a Person object. Person object is in fact composed
of other objects, as you
can see in Example. Person contains a Name object and an
Address object. The Name in
turn, contains a FirstName and a LastName object; the Address
object is composed of
a Street object and a City object.
Shallow Copy
Say we have a Person object. Person object is in fact composed
of other objects, as you
can see in Example. Person contains a Name object and an
Address object. The Name in
turn, contains a FirstName and a LastName object; the Address
object is composed of
a Street object and a City object.
Shallow Copy
Shallow Copy
A shallow copy of an object copies the ‘main’ object, but
doesn’t
copy the inner objects.
…
//Overriding clone() method
protected Object clone() throws CloneNotSupportedException
{
Person p1 = (Person) super.clone();
return p1;
}
…
Deep Copy is a fully independent copy of an object. If we
copied
the Person object, we would copy the entire object structure.
Deep Copy in Java
…
//Overriding clone() method
protected Object clone() throws CloneNotSupportedException
{
Person p1 = (Person) super.clone();
p1.name = (Name) name.clone();
p1.address = (Address) address.clone();
p1.name.firstname = (FirstName) name.firstname.clone();
p1.name.lastname = (LastName) name.lastname.clone();
p1.adress.street = (Street) address.street.clone();
p1.adress.city = (City) address.city.clone();
return p1;
}
…
Shallow vs. Deep
Let’s write a simple program that reads data from a database
and then clones the
resulting object. In our example program, SwimInfo, we just
read these data from
a file, but the original data were derived from a large database
as we discussed
above.
We create a class called Swimmer that holds one name, club
name, sex and time:
class Swimmer {
String name;
int age;
String club;
float time;
boolean female;
public String getName () {return name};
public int getAge () {return age};
public float getTime () {return time};
}
Using the Prototype - I
We create a class called SwimData that maintains a vector of
the Swimmers we read in
from the database.
public class SwimData implements Cloneable {
Vector<Swimmer> swimmers;
public Swimmer getSwimmer(int i) {
return swimmers.get(i);};
public SwimData(String filename) {
String s = "";
swimmers = new Vector();
InputFile f = new InputFile(filename); //open file
s= f.readLine(); //read in and parse each line
while(s != null) {
swimmers.addElement(new Swimmer(s));
s= f.readLine();
}
f.close();
}
Using the Prototype - II
We clone this class and sort the data differently in the new
class. Again, we clone
the data because creating a new class instance would be much
slower, and we
want to keep the data in both forms.
SwimData sdata = new SwimData();
sdata.sortByName(); //sort by name
…
sxdata = (SwimData)sdata.clone();
sxdata.sortByTime(); //re-sort by time
for(int i=0; i< sxdata.size(); i++)
//display sorted values from clone
{
Swimmer sw = sxdata.getSwimmer(i);
System.out.println(sw.getName()+" "+sw.getTime());
}
In the original class, the records
are sorted by name, while in the
cloned class, they are sorted by
time.
Using the Prototype - III
Using the Prototype pattern:
▪ You can add and remove classes at run time by cloning them
as
needed.
▪ You can revise the internal data representation of a class at
run
time based on program conditions.
▪ You can also specify new objects at run time without creating
a
proliferation of classes and inheritance structures.
Consequences of the Prototype Pattern - I
Difficulties:
▪ One difficulty in implementing the Prototype pattern in Java is
that if the classes already exist, you may not be able to change
them to add the required clone or deep Clone methods. The deep
Clone method can be particularly difficult if all of the class
objects contained in a class cannot be declared to implement the
Serializable interface.
▪ Classes that have circular references to other classes cannot
really be cloned.
▪ The idea of having prototype classes to copy implies that you
have sufficient access to the data or methods in these classes to
change them after cloning. This may require adding data access
methods to these prototype classes so that you can modify the
data once you have cloned the class.
Consequences of the Prototype Pattern - II
Design Patterns
EECS 3311
Song Wang
[email protected]
eecs.yorku.ca/~wangsong/
Overview of Last Lectures
▪ Introduction to Design Patterns
▪ Pattern’s Elements
▪ Types of Design Patterns
▪ Java Design Patterns
▪ The Singleton Pattern
▪ The Factory Pattern
▪ The Builder Pattern
▪ The Prototype Pattern
• Only your first Attempt counts
• Average of Quiz 4 is 76.9%
Quiz 4
70
72
74
76
78
80
82
84
86
88
Quiz 1 Quiz 2 Quiz 3 Quiz 4
Average
Average
Acknowledge
• Some of the covered materials are based on SOEN
344 at Concordia, SE463 at UW, SENG321 at UVic,
CSE331 at University of Washington and previous
EECS3311 offerings:
• Nikolaos Tsantalis, Jo Atlee, Marty Stepp, Mike
Godfrey, Jonathan S. Ostroff, M. Ernst, S. Reges, D. Notkin,
R. Mercer, Davor Svetinovic, Jack Jiang, Jackie Wang
Outlines
▪ Java Design Patterns
▪ The Adapter Pattern
▪ The Bridge Pattern
▪ The Composite Pattern
▪ Visitor Design Pattern
▪ Iterator Pattern
▪ State Design Pattern
▪ Event-driven pattern
▪ Observer design patterns
Pattern: Adapter
▪ Structural patterns describe how classes and objects can be
combined to
form larger structures.
▪ The difference between class patterns and object patterns is
that class patterns
describe how inheritance can be used to provide more useful
program
interfaces.
▪ Object patterns, on the other hand, describe how objects can
be composed into
larger structures using object composition, or the inclusion of
objects within
other objects.
▪ The Structural patterns are:
▪ Adapter
▪ Bridge
▪ Composite
▪ Proxy
▪ Flyweight
▪ Facade
▪ Decorator
Structural Patterns
▪ The Adapter pattern can be used to make one class interface
match another to make programming easier.
▪ The Bridge pattern separates an object’s interface from its
implementation, so you can vary them separately.
▪ The Composite pattern is a composition of objects, each of
which
may be either simple or itself a composite object.
▪ The Proxy pattern is frequently a simple object that takes the
place of a more complex object that may be invoked later, for
example when the program runs in a network environment.
Structural Patterns (cont.)
▪ The Flyweight pattern is a pattern for sharing objects, where
each
instance does not contain its own state, but stores it externally.
This allows efficient sharing of objects to save space, when
there
are many instances, but only a few different types.
▪ The Facade pattern is used to make a single class represent an
entire subsystem.
▪ The Decorator pattern, which can be used to add
responsibilities
to objects dynamically.
Structural Patterns (cont.)
▪ Adapters are used to enable objects with different interfaces to
communicate with each other.
▪ The Adapter pattern is used to convert the programming
interface
of one class into that of another. We use adapters whenever we
want unrelated classes to work together in a single program.
▪ Adapters come in two flavors, object adapters and class
adapters.
▪ The concept of an adapter is: we write a class that has the
desired
interface and then make it communicate with the class that has a
different interface.
▪ Adapters in Java can be implemented in two ways: by
inheritance, and by object composition.
Adapter Pattern:
Definition & Applicability
▪ Object adapters use a compositional technique to adapt one
interface to
another.
▪ The adapter inherits the target interface that the client expects
to see, while it
holds an instance of the adaptee.
▪ When the client calls the request() method on its target object
(the adapter), the
request is translated into the corresponding specific request on
the adaptee.
▪ Object adapters enable the client and the adaptee to be
completely decoupled
from each other. Only the adapter knows about both of them.
Adapter Pattern: Object Adapters
/**
* The SquarePeg class.
* This is the Target class.
*/
public class SquarePeg {
public void insert(String str) {
System.out.println("SquarePeg insert():
" + str);}
}
/**
* The RoundPeg class.
* This is the Adaptee class.
*/
public class RoundPeg {
public void insertIntoHole(String msg) {
System.out.println("RoundPeg insertIntoHole(): " + msg);}
}
If a client only understands the SquarePeg interface for
inserting pegs
using the insert() method, how can it insert round pegs, which
are pegs, but
that are inserted differently, using the insertIntoHole() method?
Adapter Pattern:
Example

More Related Content

PPT
01-introductionto Object ooriented Programming in JAVA CS.ppt
PPT
01-introduction OOPS concepts in C++ JAVA
PPT
Ooad ch 2
PDF
Model-based programming and AI-assisted software development
PPTX
Principles of Agile Topic. Basic understanding of Agile principles.
PPTX
Jeet ooad unit-2
PPTX
Expert system (unit 1 &amp; 2)
PPTX
Introduction to Design Patterns in Javascript
01-introductionto Object ooriented Programming in JAVA CS.ppt
01-introduction OOPS concepts in C++ JAVA
Ooad ch 2
Model-based programming and AI-assisted software development
Principles of Agile Topic. Basic understanding of Agile principles.
Jeet ooad unit-2
Expert system (unit 1 &amp; 2)
Introduction to Design Patterns in Javascript

Similar to Design PatternsEECS 3311Song Wang[email protected]ee (20)

PPT
Ooad ch 1_2
PPTX
Design Pattern lecture 4
PPTX
Lecture 5.pptx
PPTX
Analysis
PPTX
Behavioral pattern By:-Priyanka Pradhan
PDF
OBJECT ORIENTED CONCEPTS,UML DIAGRAMS,DFD
PPTX
Object Oriented Programming Concepts Def
PDF
Unit 4- Software Engineering System Model Notes
PPTX
Software engineering rogers pressman chapter 7
PPT
Software Design Patterns
PPT
Software Design Patterns
PPTX
Object_Oriented_Design_Basic Behavioral Modeling.pptx
PDF
Coding data with Boris software
DOCX
Placement management system
PPTX
Object Oriented Analysis and Design UNIT II
PPT
Chapter 4_Introduction to Patterns.ppt
PPT
Chapter 4_Introduction to Patterns.ppt
PPTX
Software Engineering- Understanding Requirements
PPTX
Object Oriented Analysis and Design - OOAD
PPTX
Cultivating Your Design Heuristics
Ooad ch 1_2
Design Pattern lecture 4
Lecture 5.pptx
Analysis
Behavioral pattern By:-Priyanka Pradhan
OBJECT ORIENTED CONCEPTS,UML DIAGRAMS,DFD
Object Oriented Programming Concepts Def
Unit 4- Software Engineering System Model Notes
Software engineering rogers pressman chapter 7
Software Design Patterns
Software Design Patterns
Object_Oriented_Design_Basic Behavioral Modeling.pptx
Coding data with Boris software
Placement management system
Object Oriented Analysis and Design UNIT II
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
Software Engineering- Understanding Requirements
Object Oriented Analysis and Design - OOAD
Cultivating Your Design Heuristics
Ad

More from LinaCovington707 (20)

DOCX
ESSAY #4In contrast to thinking of poor people as deserving of bei.docx
DOCX
Essay # 3 Instructions Representations of War and Genocide .docx
DOCX
Essay 1 What is the role of the millennial servant leader on Capito.docx
DOCX
ESSAY #6Over the course of the quarter, you have learned to apply .docx
DOCX
ErrorsKeyboarding ErrorsCapitlalization ErrorsAbbreviation err.docx
DOCX
Epidemiological ApplicationsDescribe how the concept of multifacto.docx
DOCX
Epidemic, Endemic, and Pandemic Occurrence of Disease(s)One aspect.docx
DOCX
ENVIRONMENTShould the US support initiatives that restrict carbo.docx
DOCX
ePortfolio CompletionResourcesDiscussion Participation Scoring.docx
DOCX
eproduction and Animal BehaviorReproduction Explain why asexually.docx
DOCX
Envisioning LeadershipIdentifying a challenge that evokes your pas.docx
DOCX
EnvironmentOur environment is really important. We need to under.docx
DOCX
Environmental Awareness and Organizational Sustainability  Please .docx
DOCX
EnterobacteriaceaeThe family Enterobacteriaceae contains some or.docx
DOCX
Ensuring your local region is prepared for any emergency is a comp.docx
DOCX
ENG 2480 Major Assignment #3Essay #2 CharacterAnaly.docx
DOCX
English EssayMLA format500 words or moreThis is Caue types of .docx
DOCX
Eng 2480 British Literature after 1790NameApplying Wilde .docx
DOCX
English 1C Critical Thinking Essay (6 - 6 12 pages, MLA 12pt font .docx
DOCX
ENGL 227World FictionEssay #2Write a 2-3 page essay (with work.docx
ESSAY #4In contrast to thinking of poor people as deserving of bei.docx
Essay # 3 Instructions Representations of War and Genocide .docx
Essay 1 What is the role of the millennial servant leader on Capito.docx
ESSAY #6Over the course of the quarter, you have learned to apply .docx
ErrorsKeyboarding ErrorsCapitlalization ErrorsAbbreviation err.docx
Epidemiological ApplicationsDescribe how the concept of multifacto.docx
Epidemic, Endemic, and Pandemic Occurrence of Disease(s)One aspect.docx
ENVIRONMENTShould the US support initiatives that restrict carbo.docx
ePortfolio CompletionResourcesDiscussion Participation Scoring.docx
eproduction and Animal BehaviorReproduction Explain why asexually.docx
Envisioning LeadershipIdentifying a challenge that evokes your pas.docx
EnvironmentOur environment is really important. We need to under.docx
Environmental Awareness and Organizational Sustainability  Please .docx
EnterobacteriaceaeThe family Enterobacteriaceae contains some or.docx
Ensuring your local region is prepared for any emergency is a comp.docx
ENG 2480 Major Assignment #3Essay #2 CharacterAnaly.docx
English EssayMLA format500 words or moreThis is Caue types of .docx
Eng 2480 British Literature after 1790NameApplying Wilde .docx
English 1C Critical Thinking Essay (6 - 6 12 pages, MLA 12pt font .docx
ENGL 227World FictionEssay #2Write a 2-3 page essay (with work.docx
Ad

Recently uploaded (20)

PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
Hazard Identification & Risk Assessment .pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
1_English_Language_Set_2.pdf probationary
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
advance database management system book.pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
Indian roads congress 037 - 2012 Flexible pavement
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
A powerpoint presentation on the Revised K-10 Science Shaping Paper
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Hazard Identification & Risk Assessment .pdf
Empowerment Technology for Senior High School Guide
1_English_Language_Set_2.pdf probationary
What if we spent less time fighting change, and more time building what’s rig...
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
advance database management system book.pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Weekly quiz Compilation Jan -July 25.pdf
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
TNA_Presentation-1-Final(SAVE)) (1).pptx

Design PatternsEECS 3311Song Wang[email protected]ee

  • 1. Design Patterns EECS 3311 Song Wang [email protected] eecs.yorku.ca/~wangsong/ Acknowledge • Some of the covered materials are based on SOEN 344 at Concordia, SE463 at UW, SENG321 at UVic, CSE331 at University of Washington and previous EECS3311 offerings: • Nikolaos Tsantalis, Jo Atlee, Marty Stepp, Mike Godfrey, Jonathan S. Ostroff, M. Ernst, S. Reges, D. Notkin, R. Mercer, Davor Svetinovic, Jack Jiang, Jackie Wang Outlines ▪ Java Design Patterns (Behavioral Patterns) ▪ Visitor Design Pattern ▪ Iterator Pattern ▪ State Design Pattern ▪ Observer design patterns
  • 2. Pattern: Visitor What’s It All About? • Allows for new operations to be defined and used on elements of an object structure without changing the contents of those elements. • The Key is Double Dispatch • choosing the method to invoke based both on receiver and argument types Where Applicable • Rarely Changing Object Structures • Using Unrelated Operations • Many Classes with Differing Interfaces Visitor design pattern • Add an accept(Visitor) method to the "element" hierarchy • Create a "visitor" base class w/ a visit() method for every "element" type
  • 3. • Create a "visitor" derived class for each "operation" to do on "elements" • Client creates "visitor" objects and passes each to accept() calls How it Works • Concrete Object Structure • Assume Rarely Changing • Bank Accounts Add an Inquiry Operation • Check balances and display account summary • Don’t Want to Change Structure • Create a Visitor Structure • Account Visitor Account Visitor Interface
  • 4. Inquiry Visitor Account Structure Change Account Interface Checking Account Savings Account Main Method Pattern: Iterator What’s It All About? • Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  • 5. • Separate traversal code from the data collection itself. Java Collections Figure source: https://techvidvan.com/tutorials/wp- content/uploads/sites/2/2020/03/collection-framework- hierarchy-in-java.jpg Basic Structure • Iterator keeps a Cursor to current location in a collection. • Most collections store their elements in simple lists. However, some of them are based on stacks, trees, graphs and other complex data structures. • But no matter how a collection is structured, it must provide some way of accessing its elements so that other code can use these elements. There should be a way to go through each element of the collection without accessing the same elements over and over. Visualizing iterator pattern at runtime ArrayList ArrayList Person
  • 6. Iterator isDonefirst Iterator for A Binary Tree Pattern: State Model Motivating Problem Consider the reservation panel of an online booking system: Motivating Problem State Transition Diagram State Pattern • State Design Pattern allows an object to change
  • 7. its behavior when the internal state of that object changes. • The state design pattern is generally used in cases where an object depends on its state and its behavior must be changed during run time depending on its internal state. • A typical behavioral design pattern; • Context: Defines an interface to client to interact. It maintains references to concrete state object which may be used to define current state of object. • State: Defines interface for declaring what each concrete state should do. • ConcreteState: Provides implementation for methods defined in State. State Pattern Diagram A Simple Example In this example we will model a mobile state scenario. With respect to alerts, a mobile can be in different states. For example, vibration and silent. Based on this alert state, behavior of the mobile changes when an alert is to be done.
  • 8. Source: https://www.geeksforgeeks.org/state-design-pattern/ The key classes • The State Interface interface MobileAlertState { public void alert(AlertStateContext ctx); } The key classes • Context class AlertStateContext { private MobileAlertState currentState; public AlertStateContext() { currentState = new Vibration(); } public void setState(MobileAlertState state) { currentState = state; }
  • 9. public void alert() { currentState.alert(this); } } The key classes • States class Vibration implements MobileAlertState { @Override public void alert(AlertStateContext ctx) { System.out.println("vibration..."); } } class Silent implements MobileAlertState { @Override public void alert(AlertStateContext ctx) { System.out.println("silent..."); } }
  • 10. The key classes • Demo class StatePattern { public static void main(String[] args) { AlertStateContext stateContext = new AlertStateContext(); stateContext.alert(); stateContext.alert(); stateContext.setState(new Silent()); stateContext.alert(); stateContext.alert(); stateContext.alert(); } } Output: vibration... vibration... silent... silent... silent... Pattern: Observer
  • 11. Observer Pattern • Defines a “one-to-many” dependency between objects so that when one object changes state, all its dependents are notified and updated automatically • a.k.a Dependence mechanism / publish- subscribe / broadcast / change-update Subject & Observer • Subject • the object which will frequently change its state and upon which other objects depend • Observer • the object which depends on a subject and updates according to its subject's state. Observer Pattern - Example a b c 60 y x 5030
  • 12. 30 20 10 z 801010 a b c a b c a = 50% b = 30% c = 20% change notification requests, modifications Observers Subject Observer Pattern - Working A number of Observers “register” to receive notifications of changes to the Subject. Observers are not aware of the presence of each other. When a certain event or “change” in Subject occurs, all Observers are “notified’. Observer Pattern - Key Players
  • 13. • Subject • has a list of observers • Interfaces for attaching/detaching an observer • Observer • An updating interface for objects that gets notified of changes in a subject • ConcreteSubject • Stores “state of interest” to observers • Sends notification when state changes • ConcreteObserver • Implements updating interface Observer Pattern - UML Subject attach (Observer) detach (Observer) Notify () Observer Update() ConcreteObserver
  • 14. Update() observerState ConcreteSubject SetState() GetState() subjectState observers subject For all x in observers{ x.Update(); } observerState= subject.getState(); Observer Pattern - Collaborations :ConcreteSubject :ConcreteObserver-1 :ConcreteObserver-2 GetState() Notify()
  • 15. Update() SetState() GetState() Update() Observer Pattern - Implementation interface Observer { void update (Observable sub, Object arg) // repaint the pi-chart } Java terminology for Subject. public void addObserver(Observer o) {} public void deleteObserver (Observer o) {} public void notifyObservers(Object arg) {} class Observable { … } public boolean hasChanged() {}
  • 16. Observer Pattern - Consequences • Loosely Coupled • Reuse subjects without reusing their observers, and vice versa • Add observers without modifying the subject or other observers • Abstract coupling between subject and observer • Concrete class of none of the observers is known • Support for broadcast communication • Subject doesn’t need to know its receivers • Unexpected updates • Can be blind to changes in the system if the subject is changed (i.e. doesn’t know “what” has changed in the subject) Exercise • Design the system for the flight online booking system; • Draw the class diagram of the online booking system; The idea of 'tone at the top explains the mindset of business executives' driving principles and an ethical atmosphere within the business. International focus has been paid to the link between bribery and an organization's 'tone at the top.' In recent
  • 17. years. Tone at the top is the moral environment generated in the environment by the management of the organization. Any tonal collection that has a trickle-down impact on business workers Repetition It's necessary to repeat. Leaders and staff around the organization must understand and recognize the message regularly. The leadership must speak about it and perform up to the ideals of the organization. Establish a Code of Ethics When management makes ethical values clear and speaks freely, they mean that honesty is appreciated. The Business Code of Ethics must be read and signed annually by both employees and vendors. New staff should still have to read the paper and sign it before they begin their job. This means that each employee respects and meets the same ethical standards (Kang, 2018). Annual Fraud and Ethics Training Practice sessions should warn workers about the business and the legislation prohibiting activities. This helps workers essentially escape circumstances that can lead to bad behavior. New staff may also be expected to attend ethics training at business startup. Regularly scheduled ethics training can help managers detect possible issues and prevent compromises (Gunz & Thorne, 2014). Integrity Employees typically form their job ethic following top management. Ethical judgments by management can require both written and physical communications. Besides that, the management should be modeled upon openness, integrity, and open contact. In the eyes of staff, failure to maintain honesty will create uncertainty. Employees should not behave ethically if the management does not handle people with dignity. Work ethic When senior management is late at work, leaves early, or regularly leaves work, staff may believe this is appropriate to
  • 18. copy. If management orders remain late or arrive early, workers should still work for certain hours. For management to be receptive, hardworking, and committed, these qualities must also be presented. The highest-ranking, hardworking, and agile staff of the business should be the top management. Management should be in a position to collaborate with others, constructive and fruitful. A working tone ensures that ideas from others are to be examined during everyday procedures. Staff must be able to disregard their views and listen to other people's thoughts and viewpoints. This suggests that workers, even though they are distinct from their own, think for their thoughts and viewpoints. References The Tone at the top here means the values and ethics followed by the people in the top of the hierarchy chart. The people at the top level in hierarchy are board of directors, executives and the management at top level. The factors and actions that are performed by the people at top levels largely affects the organizations because these people at the top level will lead by an example. I would like to discussion an example here in this discussion forum which the entire class has worked on a previous case study on Starbucks. The CEO of Starbucks has fired an employee and closed out many stores for time being to give its employees trainings on ethics and cultural values. The reason for these trainings and firing of an employee was because a manager has passed an inappropriate comment on one of its customers and has asked that customer to leave the store as they were sitting ideally without purchasing anything from the store. People at higher management level were involved in this discussion and made sure that this incident was taken for granted and assured customers of Starbucks can sit in the store even if they are not making any purchases. The top-level
  • 19. employees of Starbucks lead by an example and set the right level of attitude. This is a perfect example of tone of the top as the people in higher management has acted in the right way and they have led by an example so that the other employees and managers can follow their footsteps. Today, we all know how Starbucks is strong in their ethical values and one of the organizations that people would love to work because of a good ethical culture. Source: Design Patterns EECS 3311 Song Wang [email protected] eecs.yorku.ca/~wangsong/ Overview of Last Lectures • Control Flow Graphs • If Statements • Loop Statements • Code Coverage • JaCoCo • UML • Use Case Diagram • Activity Diagram • Sequence Diagram • Class Diagram
  • 20. • different relations • Only your first Attempt counts • Average of Quiz 3 is 85.8% Quiz 3 84.4 84.6 84.8 85 85.2 85.4 85.6 85.8 86 86.2 Quiz 1 Quiz 2 Quiz 3 Average Average
  • 21. Acknowledge • Some of the covered materials are based on SE463 at UW, SENG321 at UVic, CSE331 at University of Washington and previous EECS3311 offerings: • Jo Atlee, Dan Berry, Daniela Damian, Marty Stepp, Mike Godfrey, Jonathan S. Ostroff, M. Ernst, S. Reges, D. Notkin, R. Mercer, Davor Svetinovic, Jack Jiang, Jackie Wang Outlines ▪ Introduction to Design Patterns ▪ Pattern’s Elements ▪ Types of Design Patterns ▪ Java Design Patterns ▪ The Singleton Pattern ▪ The Factory Pattern ▪ The Builder Pattern ▪ The Prototype Pattern ▪ The Adapter Pattern ▪ The Bridge Pattern ▪ The Composite Pattern ▪ Visitor Design Pattern ▪ Iterator Pattern ▪ State Design Pattern ▪ Event-driven pattern ▪ Observer design patterns
  • 22. Design patterns • Design pattern: A standard solution to a common software problem in a context. • describes a recurring software structure or idiom • is abstract from any particular programming language • identifies classes and their roles in the solution to a problem • In 1990 a group called the Gang of Four or "GoF" (Gamma, Helm, Johnson, Vlissides) compile a catalog of design patterns • 1995 book Design Patterns: Elements of Reusable Object-Oriented Software is a classic of the field In general, a pattern has four essential elements. ▪ The pattern name ▪ The problem ▪ The solution ▪ The consequences Pattern’s Elements
  • 23. The pattern name is a handle we can use to describe a design problem, its solutions, and consequences in a word or two. ▪ Naming a pattern immediately increases the design vocabulary. It lets us design at a higher level of abstraction. ▪ Having a vocabulary for patterns lets us talk about them. ▪ It makes it easier to think about designs and to communicate them and their trade-offs to others. Pattern’s Elements – The Pattern Name The problem describes when to apply the pattern. ▪ It explains the problem and its context. ▪ It might describe specific design problems such as how to represent algorithms as objects. ▪ It might describe class or object structures that are symptomatic of an inflexible design. ▪ Sometimes the problem will include a list of conditions that must be met before it makes sense to apply the pattern.
  • 24. Pattern’s Elements – The Problem The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations. ▪ The solution doesn't describe a particular concrete design or implementation, because a pattern is like a template that can be applied in many different situations. ▪ Instead, the pattern provides an abstract description of a design problem and how a general arrangement of elements (classes and objects in our case) solves it. Pattern’s Elements – The Solution The consequences are the results and trade-offs of applying the
  • 25. pattern. ▪ The consequences for software often concern space and time trade-offs. ▪ They may address language and implementation issues as well. ▪ Since reuse is often a factor in object-oriented design, the consequences of a pattern include its impact on a system's flexibility, extensibility, or portability. ▪ Listing these consequences explicitly helps you understand and evaluate them. Pattern’s Elements – The Consequences Benefits of using patterns
  • 26. • Patterns give a design common vocabulary for software design: • Allows engineers to abstract a problem and talk about that abstraction in isolation from its implementation. • A culture; domain-specific patterns increase design speed. • Capture expertise and allow it to be communicated: • Promotes design reuse and avoid mistakes. • Makes it easier for other developers to understand a system. • Improve documentation (less is needed): • Improve understandability (patterns are described well, once). Gang of Four (GoF) patterns Erich Gamma, Richard Helm, Ralph Johnson and John Vlisides in their Design Patterns book define 23 design patterns divided into three types: ▪ Creational patterns are ones that create objects for you,
  • 27. rather than having you instantiate objects directly. This gives your program more flexibility in deciding which objects need to be created for a given case. ▪ Structural patterns help you compose groups of objects into larger structures, such as complex user interfaces or accounting data. ▪ Behavioral patterns help you define the communication between objects in your system and how the flow is controlled in a complex program. Gang of Four (GoF) patterns (cont.) • Creational Patterns (abstracting the object-instantiation process) • Singleton Factory • Builder Prototype … • Structural Patterns (how objects/classes can be combined) • Adapter Bridge Composite
  • 28. • Decorator Facade Flyweight • Proxy … • Behavioral Patterns (communication between objects) • Command Interpreter Iterator • Observer State • Visitor Even-driven … Pattern: Singleton A class that has only a single instance Singleton pattern • singleton: An object that is the only object of its type. (one of the most known / popular design patterns) • Ensuring that a class has at most one instance.
  • 29. • Providing a global access point to that instance. • e.g. Provide an accessor method that allows users to see the instance. • Benefits: • Takes responsibility of managing that instance away from the programmer (illegal to construct more instances). • Saves memory. • Avoids bugs arising from multiple instances. Restricting objects • One way to avoid creating objects: use static methods • Examples: Math, System • Is this a good alternative choice? Why or why not? • Disadvantage: Lacks flexibility. • Static methods can't be passed as an argument, nor
  • 30. returned. • Disadvantage: Cannot be extended. • Static methods can't be subclassed and overridden like an object's methods could be. • Make constructor(s) private so that they can not be called from outside by clients. • Declare a single private static instance of the class. • Write a public getInstance() or similar method that allows access to the single instance. • May need to protect / synchronize this method to ensure that it will work in a multi-threaded program. Implementing Singleton Singleton sequence diagram
  • 31. • Class RandomGenerator generates random numbers. public class RandomGenerator { private static final RandomGenerator gen = new RandomGenerator(); public static RandomGenerator getInstance() { return gen; } private RandomGenerator() {} ... } Singleton example
  • 32. • Can wait until client asks for the instance to create it: public class RandomGenerator { private static RandomGenerator gen = null; public static RandomGenerator getInstance() { if (gen == null) { gen = new RandomGenerator(); } return gen; } private RandomGenerator() {} ... }
  • 33. Lazy initialization Pattern: Factory The Factory pattern returns an instance of one of several possible classes depending on the data provided to it. ▪ Here, x is a base class and classes xy and xz are derived from it. ▪ The Factory is a class that decides which of these subclasses to return depending on the arguments you give it. ▪ The getClass() method passes in some value abc, and returns some instance of the class x. Which one it returns doesn't matter
  • 34. to the programmer since they all have the same methods, but different implementations. How does it Work? ▪ Let's consider a simple case where we could use a Factory class. Suppose we have an entry form and we want to allow the user to enter his name either as “firstname lastname” or as “lastname, firstname”(a comma in- between). ▪ Let’s make the assumption that we will always be able to decide the name order by whether there is a comma between the last and first name. class Namer { //a class to take a string apart into two names
  • 35. protected String last; //store last name here protected String first; //store first name here public String getFirst() { return first; //return first name } public String getLast() { return last; //return last name } } The Base Class In the FirstFirst class, we assume that everything before the l ast
  • 36. space is part of the first name. class FirstFirst extends Namer { public FirstFirst(String s) { int i = s.lastIndexOf(" "); //find separating space if (i > 0) { first = s.substring(0, i).trim(); //left = first name last =s.substring(i+1).trim(); //right = last name } else { first = “” // put all in last name last = s; // if no space } } }
  • 37. The First Derived Class In the LastFirst class, we assume that a comma delimits the last name. class LastFirst extends Namer { //split last, first public LastFirst(String s) { int i = s.indexOf(","); //find comma if (i > 0) { last = s.substring(0, i).trim(); //left= last name first = s.substring(i + 1).trim(); //right= first name } else { last = s; // put all in last name
  • 38. first = ""; // if no comma } } } The Second Derived Class The Factory class is relatively simple. We just test for the existence of a comma and then return an instance of one class or the other. class NameFactory { //returns an instance of LastFirst or FirstFirst //depending on whether a comma is found public Namer getNamer(String entry) {
  • 39. int i = entry.indexOf(","); //comma determines name order if (i>0) return new LastFirst(entry); //return one class else return new FirstFirst(entry); //or the other } } Building the Factory NameFactory nfactory = new NameFactory(); String sFirstName, sLastName; ….
  • 40. private void computeName() { //send the text to the factory and get a class back namer = nfactory.getNamer(entryField.getText()); //compute the first and last names using the returned class sFirstName = namer.getFirst(); sLastName = namer.getLast(); } Using the Factory You should consider using a Factory pattern when: ▪ Create objects dynamically ▪ A class can’t anticipate which kind of class of objects it must create.
  • 41. ▪ A class uses its subclasses to specify which objects it creates. ▪ You want to localize the knowledge of which class gets created. There are several similar variations on the factory pattern to recognize: ▪ The base class is abstract and the pattern must return a complete working class. ▪ The base class contains default methods and is only subclassed for cases where the default methods are insufficient. ▪ Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share the same method names but may do something quite different. When to Use a Factory Pattern
  • 42. The Abstract Factory pattern is one level of abstraction higher than the factory pattern. This pattern returns one of several related classes, each of which can return several different objects on request. In other words, the Abstract Factory is a factory object that returns one of several factories. One classic application of the abstract factory is the case where your system needs to support multiple “look-and-feel” user interfaces, such as Windows, Motif or Macintosh: ▪ You tell the factory that you want your program to look like Windows and it
  • 43. returns a GUI factory which returns Windows-like objects. ▪ When you request specific objects such as buttons, check boxes and windows, the GUI factory returns Windows instances of these visual interface components. The Abstract Factory Pattern How does it Work? Suppose you are writing a program to plan the layout of gardens. These could be annual gardens, vegetable gardens or perennial gardens. However, no matter which kind of garden you are planning, you want to ask the same questions: ▪ What are good border plants?
  • 44. ▪ What are good center plants? ▪ What plants do well in partial shade? We want a base Garden class that can answer these questions: public abstract class Garden { public abstract Plant getCenter(); public abstract Plant getBorder(); public abstract Plant getShade(); } A Garden Maker Factory The Plant class simply contains and returns the plant name: public class Plant {
  • 45. String name; public Plant(String pname) { name = pname; //save name } public String getName() { return name; } } The Plant Class A Garden class simply returns one kind of each plant. So, for example, for the vegetable garden we simply write:
  • 46. public class VegieGarden extends Garden { public Plant getShade() { return new Plant("Broccoli"); } public Plant getCenter() { return new Plant("Corn"); } public Plant getBorder() { return new Plant("Peas"); } } A Garden Class
  • 47. We create a series of Garden classes - VegieGarden, PerennialGarden, and AnnualGarden, each of which returns one of several Plant objects. Next, we construct our abstract factory to return an object instantiated from one of these Garden classes and based on the string it is given as an argument: class GardenMaker { //Abstract Factory which returns one of three gardens private Garden gd; public Garden getGarden(String gtype) { gd = new VegieGarden(); //default if(gtype.equals("Perennial"))
  • 48. gd = new PerennialGarden(); if(gtype.equals("Annual")) gd = new AnnualGarden(); return gd; } } A Garden Maker Class – The Abstract Factory ▪ One of the main purposes of the Abstract Factory is that it isolates the concrete classes that are generated. ▪ The actual class names of these classes are hidden in the factory and need not be known at the client level at all.
  • 49. ▪ Because of the isolation of classes, you can change or interchange these product class families freely. ▪ Since you generate only one kind of concrete class, this system keeps you for inadvertently using classes from different families of products. ▪ While all of the classes that the Abstract Factory generates have the same base class, there is nothing to prevent some derived classes from having additional methods that differ from the methods of other classes. Consequences of Abstract Factory
  • 50. Pattern: Builder The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. ▪ Builder - specifies an interface for creating parts of a Product object. ▪ ConcreteBuilder - constructs and assembles parts of the product by implementing the Builder interface. Also, it defines and keeps track of the representation it creates and provides an interface for retrieving the product .
  • 51. ▪ Director - constructs an object using the Builder interface. ▪ Product - represents the complex object under construction. How does it Work? ▪ The client creates the Director object and configures it with the desired Builder object. ▪ Director notifies the builder whenever a part of the product should be built. ▪ Builder handles requests from the director and adds parts to the product. ▪ The client retrieves the product from the builder. The following interaction diagram illustrates how Builder and Director cooperate
  • 52. with a client. How does it Work? Use the Builder pattern when: ▪ The algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled. ▪ The construction process must allow different representations for the object that is constructed. Applicability of Builder Pattern ▪ A Builder lets you vary the internal representation of the product it builds. It
  • 53. also hides the details of how the product is assembled. ▪ Each specific builder is independent of the others and of the rest of the program. This improves modularity and makes the addition of other builders relatively simple. ▪ Because each builder constructs the final product step-by-step, depending on the data, you have more control over each final product that a Builder constructs. ▪ A Builder pattern is somewhat like an Abstract Factory pattern in that both return classes made up of a number of methods and objects. The main
  • 54. difference is that while the Abstract Factory returns a family of related classes, the Builder constructs a complex object step by step depending on the data presented to it. Consequences of Builder Pattern /** "Product" */ class Pizza { private String dough = ""; //basic element private String sauce = ""; //basic element private String topping = ""; //basic element public void setDough(String dough) { this.dough = dough; }
  • 55. public void setSauce(String sauce) { this.sauce = sauce; } public void setTopping(String topping) { this.topping = topping; } } Pizza Builder /** "Abstract Builder" */ abstract class PizzaBuilder { protected Pizza pizza; public Pizza getPizza() { return pizza; }
  • 56. public void createNewPizzaProduct() { pizza = new Pizza(); } public abstract void buildDough(); public abstract void buildSauce(); public abstract void buildTopping(); } Pizza Builder /** "ConcreteBuilder" */ class HawaiianPizzaBuilder extends PizzaBuilder { public void buildDough() { pizza.setDough("cross"); } public void buildSauce() {
  • 57. pizza.setSauce("mild"); } public void buildTopping() { pizza.setTopping("ham+pineapple"); } } /** "ConcreteBuilder" */ class SpicyPizzaBuilder extends PizzaBuilder { public void buildDough() { pizza.setDough("pan baked"); } public void buildSauce() { pizza.setSauce("hot"); } public void buildTopping() { pizza.setTopping("pepperoni+salami"); }
  • 58. } Pizza Builder /** "Director" */ class Waiter { private PizzaBuilder pizzaBuilder; public void setPizzaBuilder(PizzaBuilder pb) { pizzaBuilder = pb; } public Pizza getPizza() { return pizzaBuilder.getPizza(); } public void constructPizza() { pizzaBuilder.createNewPizzaProduct(); pizzaBuilder.buildDough();
  • 59. pizzaBuilder.buildSauce(); pizzaBuilder.buildTopping(); } } Pizza Builder /** A customer ordering a pizza. */ class BuilderExample { public static void main(String[] args) { Waiter waiter = new Waiter(); PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder(); PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
  • 60. waiter.setPizzaBuilder( hawaiianPizzaBuilder ); waiter.constructPizza(); Pizza pizza = waiter.getPizza(); waiter.setPizzaBuilder(spicyPizzaBuilder); waiter.constructPizza(); Pizza pizza2 = waiter.getPizza(); } } Pizza Builder Pattern: Prototype
  • 61. ▪ A prototype is a template of any object before the actual object is constructed. ▪ The Prototype pattern specifies the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. ▪ A Protoype pattern is used when creating an instance of a class is very time- consuming or complex in some way. Then, rather than creating more instances, you make copies of the original instance and modify them as appropriate. Example: ▪ Let’s consider the case of an extensive database where you need to make a number of queries to construct an answer. Once you have this answer as a table or ResultSet, you might like to manipulate it to produce other answers
  • 62. without issuing additional queries. Definition & Applicability You can make a copy of any Java object using the clone method. Jobj j1 = (Jobj)j0.clone(); The clone method always returns an object of type Object. You must cast it to the actual type of the object you are cloning. There are three other significant restrictions on the clone method: ▪ It is a protected method and can only be called from within the same class or the module that contains that class. ▪ You can only clone objects which are declared to implement the Cloneable interface.
  • 63. ▪ Objects that cannot be cloned throw the CloneNotSupported Exception. Cloning in Java - I This suggests packaging the actual clone method inside the class where it can access the real clone method: public class SwimData implements Cloneable { public Object clone() { try{ return super.clone(); }
  • 64. catch(Exception e) { System.out.println(e.getMessage()); return null; } } } Cloning in Java - II ▪This implementation has the advantage of encapsulating the try-catch block inside the public clone method. ▪Note that if you declare this public method to have the same
  • 65. name “clone,” it must be of type Object, since the internal protected method has that signature. We could, however, change the name and do the typecasting within the method instead of forcing it onto the user: public SwimData cloneMe() { try{ return (SwimData)super.clone(); } catch(Exception e) { System.out.println(e.getMessage()); return null; }
  • 66. } Cloning in Java - III Shallow vs. Deep (Object) Copy in Java Thanks: https://dzone.com/articles/java-copy-shallow-vs-deep- in-which-you-will-swim A reference copy, as the name implies, creates a copy of a reference variable pointing to an object. An object copy creates a copy of the object itself. Say we have a Person object. Person object is in fact composed of other objects, as you can see in Example. Person contains a Name object and an Address object. The Name in turn, contains a FirstName and a LastName object; the Address object is composed of
  • 67. a Street object and a City object. Shallow Copy Say we have a Person object. Person object is in fact composed of other objects, as you can see in Example. Person contains a Name object and an Address object. The Name in turn, contains a FirstName and a LastName object; the Address object is composed of a Street object and a City object. Shallow Copy Shallow Copy A shallow copy of an object copies the ‘main’ object, but doesn’t copy the inner objects.
  • 68. … //Overriding clone() method protected Object clone() throws CloneNotSupportedException { Person p1 = (Person) super.clone(); return p1; } … Deep Copy is a fully independent copy of an object. If we copied the Person object, we would copy the entire object structure. Deep Copy in Java
  • 69. … //Overriding clone() method protected Object clone() throws CloneNotSupportedException { Person p1 = (Person) super.clone(); p1.name = (Name) name.clone(); p1.address = (Address) address.clone(); p1.name.firstname = (FirstName) name.firstname.clone(); p1.name.lastname = (LastName) name.lastname.clone(); p1.adress.street = (Street) address.street.clone(); p1.adress.city = (City) address.city.clone(); return p1;
  • 70. } … Shallow vs. Deep Let’s write a simple program that reads data from a database and then clones the resulting object. In our example program, SwimInfo, we just read these data from a file, but the original data were derived from a large database as we discussed above. We create a class called Swimmer that holds one name, club name, sex and time:
  • 71. class Swimmer { String name; int age; String club; float time; boolean female; public String getName () {return name}; public int getAge () {return age}; public float getTime () {return time}; } Using the Prototype - I We create a class called SwimData that maintains a vector of
  • 72. the Swimmers we read in from the database. public class SwimData implements Cloneable { Vector<Swimmer> swimmers; public Swimmer getSwimmer(int i) { return swimmers.get(i);}; public SwimData(String filename) { String s = ""; swimmers = new Vector(); InputFile f = new InputFile(filename); //open file s= f.readLine(); //read in and parse each line while(s != null) { swimmers.addElement(new Swimmer(s));
  • 73. s= f.readLine(); } f.close(); } Using the Prototype - II We clone this class and sort the data differently in the new class. Again, we clone the data because creating a new class instance would be much slower, and we want to keep the data in both forms. SwimData sdata = new SwimData(); sdata.sortByName(); //sort by name
  • 74. … sxdata = (SwimData)sdata.clone(); sxdata.sortByTime(); //re-sort by time for(int i=0; i< sxdata.size(); i++) //display sorted values from clone { Swimmer sw = sxdata.getSwimmer(i); System.out.println(sw.getName()+" "+sw.getTime()); } In the original class, the records are sorted by name, while in the cloned class, they are sorted by time.
  • 75. Using the Prototype - III Using the Prototype pattern: ▪ You can add and remove classes at run time by cloning them as needed. ▪ You can revise the internal data representation of a class at run time based on program conditions. ▪ You can also specify new objects at run time without creating a proliferation of classes and inheritance structures. Consequences of the Prototype Pattern - I
  • 76. Difficulties: ▪ One difficulty in implementing the Prototype pattern in Java is that if the classes already exist, you may not be able to change them to add the required clone or deep Clone methods. The deep Clone method can be particularly difficult if all of the class objects contained in a class cannot be declared to implement the Serializable interface. ▪ Classes that have circular references to other classes cannot really be cloned. ▪ The idea of having prototype classes to copy implies that you have sufficient access to the data or methods in these classes to change them after cloning. This may require adding data access
  • 77. methods to these prototype classes so that you can modify the data once you have cloned the class. Consequences of the Prototype Pattern - II Design Patterns EECS 3311 Song Wang [email protected] eecs.yorku.ca/~wangsong/ Overview of Last Lectures ▪ Introduction to Design Patterns ▪ Pattern’s Elements ▪ Types of Design Patterns
  • 78. ▪ Java Design Patterns ▪ The Singleton Pattern ▪ The Factory Pattern ▪ The Builder Pattern ▪ The Prototype Pattern • Only your first Attempt counts • Average of Quiz 4 is 76.9% Quiz 4 70 72 74 76
  • 79. 78 80 82 84 86 88 Quiz 1 Quiz 2 Quiz 3 Quiz 4 Average Average
  • 80. Acknowledge • Some of the covered materials are based on SOEN 344 at Concordia, SE463 at UW, SENG321 at UVic, CSE331 at University of Washington and previous EECS3311 offerings: • Nikolaos Tsantalis, Jo Atlee, Marty Stepp, Mike Godfrey, Jonathan S. Ostroff, M. Ernst, S. Reges, D. Notkin, R. Mercer, Davor Svetinovic, Jack Jiang, Jackie Wang Outlines ▪ Java Design Patterns ▪ The Adapter Pattern ▪ The Bridge Pattern
  • 81. ▪ The Composite Pattern ▪ Visitor Design Pattern ▪ Iterator Pattern ▪ State Design Pattern ▪ Event-driven pattern ▪ Observer design patterns Pattern: Adapter ▪ Structural patterns describe how classes and objects can be combined to form larger structures. ▪ The difference between class patterns and object patterns is that class patterns
  • 82. describe how inheritance can be used to provide more useful program interfaces. ▪ Object patterns, on the other hand, describe how objects can be composed into larger structures using object composition, or the inclusion of objects within other objects. ▪ The Structural patterns are: ▪ Adapter ▪ Bridge ▪ Composite ▪ Proxy ▪ Flyweight
  • 83. ▪ Facade ▪ Decorator Structural Patterns ▪ The Adapter pattern can be used to make one class interface match another to make programming easier. ▪ The Bridge pattern separates an object’s interface from its implementation, so you can vary them separately. ▪ The Composite pattern is a composition of objects, each of which may be either simple or itself a composite object. ▪ The Proxy pattern is frequently a simple object that takes the place of a more complex object that may be invoked later, for
  • 84. example when the program runs in a network environment. Structural Patterns (cont.) ▪ The Flyweight pattern is a pattern for sharing objects, where each instance does not contain its own state, but stores it externally. This allows efficient sharing of objects to save space, when there are many instances, but only a few different types. ▪ The Facade pattern is used to make a single class represent an entire subsystem. ▪ The Decorator pattern, which can be used to add responsibilities to objects dynamically.
  • 85. Structural Patterns (cont.) ▪ Adapters are used to enable objects with different interfaces to communicate with each other. ▪ The Adapter pattern is used to convert the programming interface of one class into that of another. We use adapters whenever we want unrelated classes to work together in a single program. ▪ Adapters come in two flavors, object adapters and class adapters. ▪ The concept of an adapter is: we write a class that has the desired interface and then make it communicate with the class that has a
  • 86. different interface. ▪ Adapters in Java can be implemented in two ways: by inheritance, and by object composition. Adapter Pattern: Definition & Applicability ▪ Object adapters use a compositional technique to adapt one interface to another. ▪ The adapter inherits the target interface that the client expects to see, while it holds an instance of the adaptee. ▪ When the client calls the request() method on its target object (the adapter), the request is translated into the corresponding specific request on
  • 87. the adaptee. ▪ Object adapters enable the client and the adaptee to be completely decoupled from each other. Only the adapter knows about both of them. Adapter Pattern: Object Adapters /** * The SquarePeg class. * This is the Target class. */ public class SquarePeg { public void insert(String str) { System.out.println("SquarePeg insert():
  • 88. " + str);} } /** * The RoundPeg class. * This is the Adaptee class. */ public class RoundPeg { public void insertIntoHole(String msg) { System.out.println("RoundPeg insertIntoHole(): " + msg);} } If a client only understands the SquarePeg interface for inserting pegs using the insert() method, how can it insert round pegs, which are pegs, but that are inserted differently, using the insertIntoHole() method?