Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are...
Transcript of Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are...
![Page 1: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/1.jpg)
Irvine COP 4814
COP 4814 Florida International University
Kip Irvine
Introduction to Design Patterns
based on: Head-First Design Patterns,
Chapter 1
Updated: 3/21/2016
![Page 2: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/2.jpg)
Overview
• Why use design patterns? • What are design patterns? • Three general types of patterns • Introducing UML diagrams • Introducing the Strategy Pattern • Ducks example from Chapter 1
Irvine COP 4814
![Page 3: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/3.jpg)
Why use design patterns?
• Design patterns help create software solutions that are simple, reusable and maintainable.
• Design patterns help us to describe communication between objects – avoid becoming entangled in each others' data
models and methods • Introduced in the early 1990's by Erich Gamma
– described patterns in a GUI application framework
![Page 4: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/4.jpg)
What are Design Patterns?
• In everyday life, we say that something "is just like" another more familiar thing, with some variations.
• …recurring solutions to design problems you see over and over.
• ..identify and specify abstractions that are above the level of single classes and instances, or of components.
• ..not just about the design of objects—they are about the interactions between objects.
![Page 5: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/5.jpg)
Three General Types
• Creational patterns -- patterns that create objects – e.g. factory, decorator, singleton
• Structural patterns -- compose groups of objects into larger structures – e.g. model-view-controller
• Behavioral patterns -- help to define the communication between objects in a system – e.g. strategy, observer
![Page 6: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/6.jpg)
UML Diagrams
• Unified Modeling Language – a standard in the industry for describing classes, interfaces, and their relationships
• Class name, attributes, operations
Source: UML Basics – Class Diagrams, from IBM DeveloperWorks
![Page 7: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/7.jpg)
Operations
Format for describing an operation
name(parameter list) : type of value returned
Examples: calculate(double hours, double rate) : double
isValid( ) : boolean
getName( ) : string
![Page 8: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/8.jpg)
Visibility of Members + indicates a public member
# indicates a protected member (visible to subclasses only)
− indicates a private member
~ indicates a member with package-level visibility
![Page 9: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/9.jpg)
Inheritance Relationships
Class name in italics is an abstract class
![Page 10: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/10.jpg)
Bidirectional Association
Two classes that are both aware of each other and can call each others' methods have a bidirectional association (solid line).
![Page 11: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/11.jpg)
Uni-Directional Association
Two classes are related, but only one of the classes know that the relationship exists.
![Page 12: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/12.jpg)
Basic Aggregation
A type of association:
One class is part of another class. For example, a Car may contain Wheel objects, an Engine object, and many others. The child class instance can outlive its parent class instance. In the example below, a Wheel can exist even when no Car exists.
![Page 13: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/13.jpg)
Composition Aggregation
Another type of association:
Another type of aggregation in which the child class instance's lifecycle is dependent on the parent child's lifecycle. In this example, the Department instance cannot exist independently of the Company instance.
![Page 14: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/14.jpg)
Interfaces An interface describes a common set of operations for all classes that implement the interface. When a class implements an interface, it agrees to provide concrete versions of all the interface methods.
Below, the Professor and Student classes implement the Person interface.
![Page 15: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/15.jpg)
Strategy Pattern
• Components often contain classes that embody behaviors – buying and selling stocks – aircraft simulation – browser plugins – data encryption – managing network flow
• Should their code be hard-wired into an application? – what if a behavior must be modified at runtime?
Irvine COP 4814
![Page 16: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/16.jpg)
Strategy Pattern
• Rather than hard-wiring behaviors into classes when designing them, why not make them more flexible?
• Think of categories of behaviors – then create types that implement these categories – You can swap out one behavior for another, as long
as their behavior category is the same
Irvine COP 4814
![Page 17: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/17.jpg)
Ducks Example (book)
Imagine an abstract Duck class with two categories of behaviors:
abstract class Duck {
FlyBehavior howToFly;
QuackBehavior howToQuack;
}
Irvine COP 4814
![Page 18: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/18.jpg)
Ducks Example (book)
Define two behavior categories:
interface FlyBehavior {
void fly();
}
interface QuackBehavior {
void quack();
}
Irvine COP 4814
![Page 19: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/19.jpg)
Ducks Example (book)
Create classes that implement the behavior categories:
class FlyWithWings implements FlyBehavior {
void fly() { print(“I can fly with wings!”); }
}
class RealQuackSound implements QuackBehavior {
void quack() { print(“Real quacking sound”); }
}
Irvine COP 4814
![Page 20: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/20.jpg)
Ducks Example (book)
Define a specific type of duck and assign behaviors:
class MallardDuck extends Duck {
MallardDuck() {
howToFly = new FlyWithWings();
howToQuack = new RealQuackSound();
}
}
Irvine COP 4814
![Page 21: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/21.jpg)
Ducks Example (book)
Define another type of duck:
class ModelDuck extends Duck {
ModelDuck() {
howToFly = new FlyNoWay();
howToQuack = new SilentQuack();
}
}
Irvine COP 4814
![Page 22: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/22.jpg)
Ducks Example (book)
What if you could change a duck’s behavior at runtime?
abstract class Duck {
FlyBehavior howToFly;
QuackBehavior howToQuack;
void setFlyBehavior( FlyBehavior howToFly ) {
this.howToFly = howToFly;
}
}
Irvine COP 4814
![Page 23: Introduction to Design Patternsusers.cis.fiu.edu/~irvinek/cop4814/slideshows/Intro to...What are Design Patterns? • In everyday life, we say that something "is just like" another](https://reader033.fdocuments.in/reader033/viewer/2022042805/5f6811315bf17816ec4346cc/html5/thumbnails/23.jpg)
Ducks Example (book)
Now the test program can make the behavior dynamic: class DuckSimulator { public static void main(...) { Duck dd = new ModelDuck(); dd.fly(); // “I cannot fly” dd.setFlyBehavior( new RocketPowered() ); dd.fly(); // “I am flying with a rocket!” } }
Irvine COP 4814