Exercise Session 3 Todays Exercise Session Assignment 2 - - PowerPoint PPT Presentation

exercise session 3 today s exercise session
SMART_READER_LITE
LIVE PREVIEW

Exercise Session 3 Todays Exercise Session Assignment 2 - - PowerPoint PPT Presentation

Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Exercise Session 3 Todays Exercise Session Assignment 2 Walkthrough the master solution (your solutions) Questions Pattern of


slide-1
SLIDE 1

Languages in Depth Series: Java Programming

  • Prof. Dr. Bertrand Meyer

Chair of Software Engineering

Exercise Session 3

slide-2
SLIDE 2

2

Languages in Depth series: Java Programming

Today’s Exercise Session

Assignment 2

  • Walkthrough the master solution
  • (your solutions)
  • Questions

Pattern of the Day

  • The Strategy Pattern

Assignment II

  • Hand-out

Quizzes

slide-3
SLIDE 3

3

Languages in Depth series: Java Programming

Assignment 2

  • Review of the master solution
  • (Your solutions)
  • Questions
slide-4
SLIDE 4

4

Languages in Depth series: Java Programming

The Strategy Pattern

A behavioral pattern: It is used to adapt/change behavior of an existing algorithm/object. Intent: Define a family of algorithms, encapsulate each one and make them interchangeable. The Strategy pattern lets the algorithm, embedded in a context object, vary independently from clients that use the context object. Useful if:

  • The behavior of an context object should be dynamically

swapped

  • But the object’s interface (and identity) remains the same
  • All algorithms share a common interface
slide-5
SLIDE 5

5

Languages in Depth series: Java Programming

Motivation

Often, different algorithms exist for the same task. Hard- wiring all such algorithms into classes isn’t desirable because:

  • Clients get more complex if they

would have to include the algorithms themselves

  • The context classes become

inflexible if they have to contain the algorithm

  • Different algorithms will be

appropriate at different times and should be exchangable

Solution: Separate the algorithm implementation (“Strategy”) from the context object that uses it.

slide-6
SLIDE 6

6

Languages in Depth series: Java Programming

Example: Flexible Sorting Framework

// “Context” public public class class SortedList { private private ArrayList<String> list = new ArrayList<String>(); private private SortStrategy sortStrategy; void void setSortStrategy(SortStrategy s) { this this.sortStrategy = s; } public public void void add(String name) { list.add(name); } public public void void sort() { sortStrategy.sort(list); } } // "Strategy" public interface public interface SortStrategy { void void sort(ArrayList<String> list); }

slide-7
SLIDE 7

7

Languages in Depth series: Java Programming

Example: Some Concrete Strategies

public public class class QuickSort implements SortStrategy { public public void sort(ArrayList<String> list) { //Quick-sorting list... } } public public class class ShellSort implements SortStrategy { public public void sort(ArrayList<String> list) { //Shell-sorting list... } } public public class class MergeSort implements SortStrategy { public public void sort(ArrayList<String> list) { //Shell-sorting list... } }

slide-8
SLIDE 8

8

Languages in Depth series: Java Programming

Example: Usage

public public class class SortApp { public public static void main(String[] args) { // Two contexts following different strategies SortedList studentRecords = new SortedList(); studentRecords.add("Samual"); studentRecords.add("Jimmy"); studentRecords.add("Sandra"); studentRecords.setSortStrategy(new QuickSort()); studentRecords.sort(); studentRecords.setSortStrategy(new ShellSort()); studentRecords.sort(); studentRecords.setSortStrategy(new MergeSort()); studentRecords.sort(); } }

slide-9
SLIDE 9

9

Languages in Depth series: Java Programming

Consequences (1)

The Strategy pattern has the following benefits:

  • Hierarchies of strategies define a whole family of

algorithms; inheritance can help to factor out common functionality of the algorithms.

  • Can be a more dynamic and more reusable alternative to

subclassing, as long as the algorithm interface doesn’t change.

  • Strategies can provide a variety of implementations to a

client which can choose the most appropriate one, depending on individual time and space trade-offs.

slide-10
SLIDE 10

10

Languages in Depth series: Java Programming

Consequences (2)

The Strategy pattern has the following drawbacks:

  • Clients might have to be aware of the different strategies

in order to select the appropriate one; clients might be exposed to implementation issues.

  • Additional communication overhead between strategy

and context: additional method calls and a “one-size-fits- all” shared algorithm interface.

  • Strategies increase the number of objects. You might

use shared stateless (!) strategy objects to reduce the

  • verhead.
slide-11
SLIDE 11

11

Languages in Depth series: Java Programming

Comparison to the Template Method Pattern

The strategy pattern and the template method pattern are in some sense very similar; both allow to change the behavior of an algorithm. However:

  • The Template Method pattern uses inheritance, but the

Strategy pattern uses delegation.

  • The basic steps of the algorithm in the Template Method

pattern are fixed; callbacks and hook methods are used to adapt the behavior.

  • With the Strategy pattern, the whole algorithm is

replaced.

slide-12
SLIDE 12

12

Languages in Depth series: Java Programming

Assignment 3: A GUI for the Calculator

slide-13
SLIDE 13

13

Languages in Depth series: Java Programming

Quiz: Syntax

import import java.util.Date; package package my.products; private private class class Product { int multiply(int a, int b) { return(a * b) } abstract Date lastSell(); public static static void main(String args[ ]) { int x = 10, y = 20; System.out.println(multiply(x, y)); } }

Try to find all 6 mistakes in this example class.

File my/products/Products.java:

slide-14
SLIDE 14

14

Languages in Depth series: Java Programming

Quiz: Syntax (Solution)

import import java.util.Date; package package my.products; private private class class Product { int multiply(int a, int b) { return(a * b) } abstract Date lastSell(); public static static void main(String args[ ]) { int x = 10, y = 20; System.out.println(multiply(x, y)); } }

File my/products/Products.java:

1.

  • 1. The

The filename must be equal to the class name filename must be equal to the class name

  • 2. The
  • 2. The package declaration must come

package declaration must come first

  • 3. Top-level classes can not be private
  • 3. Top-level classes can not be private
  • 4. Semi-colon is missing
  • 4. Semi-colon is missing
  • 5. No abstract methods in non-abstract classes
  • 5. No abstract methods in non-abstract classes

6.

  • 6. A non-static method can not be

A non-static method can not be invoked in a static context invoked in a static context

slide-15
SLIDE 15

15

Languages in Depth series: Java Programming

Quiz: Generics and Overloading

import import java.util.*; class class Foo { public public Integer foo(List list) { return return null null; } public public String foo(List list) { return return null; } } class class Bar { public public Integer bar(List<Integer> integers) { return return null; } public public String bar(List<String> strings) { return return null; } }

Do these two classes compile? Explain why or why not.

slide-16
SLIDE 16

16

Languages in Depth series: Java Programming

Quiz: Generics and Overloading

import import java.util.*; class class Foo { public public Integer foo(List list) { return return null null; } public public String foo(List list) { return return null; } } class class Bar { public public Integer bar(List<Integer> integers) { return return null; } public public String bar(List<String> strings) { return return null; } }

Foo does not compile. Both foo- methods have the same signature and are therefore ambiguous. Bar does compile. Overload resolution happens at compile

  • time. At that time, the compiler

has access to the generic type information to select a most specific method.

slide-17
SLIDE 17

17

Languages in Depth series: Java Programming

Quiz: Generics and Overloading

import import java.util.*; class class Bar { public public Integer bar(List<Integer> integers) { return return null; } public public Integer bar(List<String> strings) { return return null; } }

Why does this class not compile? (Note the changed return types)

slide-18
SLIDE 18

18

Languages in Depth series: Java Programming

Quiz: Generics and Overloading

import import java.util.*; class class Bar { public public Integer bar(List<Integer> integers) { return return null; } public public Integer bar(List<String> strings) { return return null; } }

During compilation, generic type erasure is performed: generic type parameters are replaced by their leftmost bound or ‘Object’ if no bound was specified. Two methods with the same erasure would have identical signatures in the byte code (the return type is included) and the virtual machine could not distinguish between

  • them. This is prohibited and the compiler issues an error

method if two method have the same erasure.

slide-19
SLIDE 19

19

Languages in Depth series: Java Programming

Questions