0% found this document useful (0 votes)
97 views10 pages

Lecture w2 02 PDF

The document discusses design patterns and the strategy pattern. It provides an example of applying the strategy pattern to a duck simulator. The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable so that the algorithm can vary independently of the client using it. The example shows applying different quack and fly behaviors to different types of ducks at runtime.

Uploaded by

Noora Sweis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views10 pages

Lecture w2 02 PDF

The document discusses design patterns and the strategy pattern. It provides an example of applying the strategy pattern to a duck simulator. The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable so that the algorithm can vary independently of the client using it. The example shows applying different quack and fly behaviors to different types of ducks at runtime.

Uploaded by

Noora Sweis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Design Patterns

Recap

• The one constant in software development is change

• Think about your code/product/app to make it flexible,


maintainable, and can cope with changes (extendable)

• Patterns rely on OO concepts & principles


Principles
• encapsulated aspect of the code that can/will vary

• favored Has-A relationship over Is-A relationship

• favor composition over inheritance

• creating systems using composition gives you a lot more


flexibility.

• Not only does it let you encapsulate a family of algorithms


into their own set of classes, but it also lets you change
behavior at runtime
Strategy Pattern

Defines a family of algorithms, encapsulate each one,


and makes them interchangeable. Lets the algorithm
vary independently from the client using it.
Class Diagram
Does that look
familiar to you?
Abstract Class makes use of
encapsulated family of
algorithms

Family of
algorithms

MallardDuck
public MallardDuck() {
quackBehavior = new Quack();
flyBehavior = new FlyWithWings(); Concrete
} class
Test the Code
Call the
public void performQuack() { inherited public class MiniDuckSimulator {
quackBehavior.quack(); } method public static void main(String[] args) {
Duck mallard = new MallardDuck();
mallard.performQuack();
mallard.performFly(); }
}

Delegate to object
concrete quack
behavior
public class Quack implements QuackBehavior
{ public void quack() {
System.out.println(“Quack”);
}
}
We can still do one more
modification to improve the code

• Set the behavior dynamically at runtime

• What will happen if we want to modify the behavior at


runtime?

• Do it by yourself (see next slide)

Tutorial
• Download the code from lms

• Create new java projects, use the classes you downloaded from lms

• Create a simulator class which create an instance from the Mallard Duck class, show its
behavior

• Add new Duck type call it ModelDuck; this type can’t fly and can quack

• Modify the code to show how at runtime we can change the fly behavior of the ModelDuck
which can fly as the rocket

• You need to update the Duck class

• Add new fly behavior: fly as rocket

• In the simulator show how to do the changes

You might also like