Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Exercise Session 3
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 Languages in Depth series: Java Programming 2
Assignment 2 • Review of the master solution • (Your solutions) • Questions Languages in Depth series: Java Programming 3
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 Languages in Depth series: Java Programming 4
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. Languages in Depth series: Java Programming 5
Example: Flexible Sorting Framework // “Context” // "Strategy" public public class class SortedList { public interface public interface SortStrategy { private private ArrayList<String> list = void void sort(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); } } Languages in Depth series: Java Programming 6
Example: Some Concrete Strategies public public class class QuickSort implements SortStrategy { public public void sort(ArrayList<String> list) { //Quick-sorting list... } } public class public 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... } } Languages in Depth series: Java Programming 7
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(); } } Languages in Depth series: Java Programming 8
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. Languages in Depth series: Java Programming 9
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 overhead. Languages in Depth series: Java Programming 10
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. Languages in Depth series: Java Programming 11
Assignment 3: A GUI for the Calculator Languages in Depth series: Java Programming 12
Quiz: Syntax File my/products/Products.java: import import java.util.Date; package package my.products; private private class class Product { Try to find all 6 int multiply(int a, int b) { mistakes in this return(a * b) } example class. abstract Date lastSell(); public static static void main(String args[ ]) { int x = 10, y = 20; System. out .println(multiply(x, y)); } } Languages in Depth series: Java Programming 13
Quiz: Syntax (Solution) 1. The 1. The filename must be equal to the class name filename must be equal to the class name File my/products/Products.java: import java.util.Date; import 2. The package declaration must come 2. The package declaration must come first package my.products; package private private class class Product { 3. Top-level classes can not be private 3. Top-level classes can not be private int multiply(int a, int b) { return(a * b) 4. Semi-colon is missing 4. Semi-colon is missing } abstract Date lastSell(); 5. No abstract methods in non-abstract classes 5. No abstract methods in non-abstract classes public static static void main(String args[ ]) { int x = 10, y = 20; System. out .println(multiply(x, y)); } 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 Languages in Depth series: Java Programming 14
Quiz: Generics and Overloading import import java.util.*; class class Foo { Do these two public public Integer foo(List list) { classes return return null null; compile? } public public String foo(List list) { Explain why or return return null; why not. } } class class Bar { public public Integer bar(List<Integer> integers) { return return null; } public public String bar(List<String> strings) { return return null; } } Languages in Depth series: Java Programming 15
Quiz: Generics and Overloading import import java.util.*; Foo does not compile. Both foo- class class Foo { methods have the same signature public public Integer foo(List list) { and are therefore ambiguous. return return null null; Bar does compile. Overload } resolution happens at compile public public String foo(List list) { time. At that time, the compiler return return null; has access to the generic type } } information to select a most specific method. class Bar { class public public Integer bar(List<Integer> integers) { return return null; } public public String bar(List<String> strings) { return return null; } } Languages in Depth series: Java Programming 16
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) Languages in Depth series: Java Programming 17
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. Languages in Depth series: Java Programming 18
Questions Languages in Depth series: Java Programming 19
Recommend
More recommend