Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type....

13
Slide 1 Design Patterns

Transcript of Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type....

Page 1: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 1

Design Patterns

Page 2: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 2

Video Rental System

• Rental of each movie is determined by the type.• There are currently in the system three types of movies• New releases have a rental fee of $3.00• Children’s movies rent for $1.00• All other movies rent for $2.00

nameaddressphoneNumbercreditCard#emailAddressuserNamepassword

Member

copyID

VideoCopy

barCodetitletype……..

VideoDescription

rentalStorerentalDatedueDatereturnDatereturnTime

Rental

1 *

1

*

* 1

*

rentsrequest

Page 3: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 3

Video Rental System

nameaddressphoneNumbercreditCard#emailAddressuserNamepassword

Member

copyID

VideoCopy

barCodetitletype……..

VideoDescription

rentalStorerentalDatedueDatereturnDatereturnTime

Rental

1 *

1

*

* 1

*

rentsrequest

• In design you realized you realized you needed a method to make the statement to for the rentals…(you might have placed it in MEMBER or the use case class RENTVIDEO

• While other code would be present, there would be a method that calculates the cose of the movie depending on the days and the price of the movie

• There would be a switch or case statement to select the price depending on the type

Page 4: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 4

Video Rental System

public Void makeStatement () { int price = 0, total = 0, int days Iterator videos = rents.iterator(); …..while (videos.hasNext()) { Rental rents = (Rental) videos.next(); int type = rents.getMovie().getType(); switch (type) { case Movie.REGULAR: price = 200; break; case Movie.NEWRELEASE: price = 300; break; case Movie.CHILDREN: price = 100; break; days = computeDays(rentalDate); cost = days * price; }……..

Page 5: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 5

Video Rental System

public Void makeStatement () { int price = 0, total = 0 Iterator videos = rents.iterator(); …..while (videos.hasNext()) { Rental rents = (Rental) videos.next(); int type = rents.getMovie().getType(); switch (type) { case Movie.REGULAR: price = 200; break; case Movie.NEWRELEASE: price = 300; break; case Movie.CHILDREN: price = 100; break; days = computeDays(rentalDate); cost = days * price; …..

Make this a method called computeCost

Page 6: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 6

Video Rental System

public Void makeStatement () { int price = 0, total = 0 Iterator videos = rents.iterator(); …..while (videos.hasNext()) { Rental rents = (Rental) videos.next(); int type = rents.getMovie().getType(); switch (type) { case Movie.REGULAR: price = 200; break; case Movie.NEWRELEASE: price = 300; break; case Movie.CHILDREN: price = 100; break; days = computeDays(rentalDate); cost = days * price; …..

Now it is a method that returns the cost of the

rental and should probably be placed in the

RENTAL CLASS

rentalStorerentalDatedueDatereturnDatereturnTimecomputeDayscomputeCost

Rental

Page 7: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 7

Video Rental System

public Void makeStatement () { int price = 0, total = 0 Iterator videos = rents.iterator(); …..while (videos.hasNext()) { Rental rents = (Rental) videos.next(); int type = rents.getMovie().getType(); switch (type) { case Movie.REGULAR: price = 200; break; case Movie.NEWRELEASE: price = 300; break; case Movie.CHILDREN: price = 100; break; days = computeDays(rentalDate); cost = days * price; …..

BUT: getCost uses type from from the MovieDescription to decide the price. We could pass the days to MovieDescription and have it select the price since it has the type.

barCodetitleType

getCost (days)……..

VideoDescription

Page 8: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 8

Video Rental System

public Void makeStatement () { int price = 0, total = 0 Iterator videos = rents.iterator(); …..while (videos.hasNext()) { Rental rents = (Rental) videos.next(); int type = rents.getMovie().getType(); switch (type) { case Movie.REGULAR: price = 200; break; case Movie.NEWRELEASE: price = 300; break; case Movie.CHILDREN: price = 100; break; days = computeDays(rentalDate); cost = days * price; …..

Still: getCost currently are not exploiting dynamic binding to make the case decisions

==> sub-classing of three different sub-types: Regular, NewRelease, and Children would allow this

VideoDescription

Regular Children NewRelease

Page 9: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 9

Video Rental System

barCodetitletype……..

VideoDescription

Regular Children NewRelease

getCost getCost getCost

NOTE: even more flexibility can be achieved if the class model is developed further to a framework by applying a construct like the Strategy Pattern.

Page 10: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 10

Video Rental System

barCodetitletype……..

VideoDescription

Regular Children NewRelease

getCost getCost getCost

So we add a class responsible for the pricing strategy……

9. step -> additional flexibility by adding a class Price responsible for the pricing strategy acting as framework hot-spot

Price

getCost

Page 11: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 11

Video Rental System

abstract class Price { public abstract int getCost (int days);}class Regular extends Price { public int getCost (int days) { return days * 200; }}class NewRelease extends Price { public int getCost (int days) { return days * 300; }}}class Children extends Price { public int getCost (int days) { return days * 100;}} …..

Page 12: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 12

Video Rental System

class MovieDescription { private String title; ……. private Price pricing;

…..

public Price getPrice () { return price; } …. public int getCharge (int days) { return pricing.getCost(days); }}

Page 13: Slide 1 Design Patterns. Slide 2 Video Rental System Rental of each movie is determined by the type. There are currently in the system three types of.

Slide 13

Video Rental System

class Rental { private MovieDescription movie; private int days;

….

public Movie getMovie () { return movie; } public int computeDays () { …..return days; } public int getCost () { return movie.getCost(days); }}