Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Exercise Session 6 Andrei Vancea
Today’s Exercise Session Assignment VI Hints Questions Pattern of the Day The Abstract Factory Quizzes Languages in Depth series: Java Programming 2
Assignment V Hints Questions Languages in Depth series: Java Programming 3
Abstract Factory Creational Pattern Concerned with object creation Provide an interface for creating families of related or dependent objects without specifying their concrete classes. AKA : Kit Languages in Depth series: Java Programming 4
Abstract Factory - Structure AbstractFactory Declares an interface for operations that create abstract product objects. ConcreteFactory Implements the operations to create concrete product objects. Languages in Depth series: Java Programming 5
Abstract Factory - structure AbstractProduct Declares an interface for a type of product object. ConcreteProduct Defines a product object to be created by the corresponding concrete factory. Implements the AbstractProduct interface. Client uses only interfaces declared by AbstractFactory and AbstractProduct classes. Languages in Depth series: Java Programming 6
Abstract Factory – Example * Languages in Depth series: Java Programming *from http://en.wikipedia.org/wiki/Abstract_factory_pattern 7
Abstract Factory – Example Languages in Depth series: Java Programming 8
Abstract Factory – Consequences It isolates concrete classes. Clients manipulate instances through their abstract interfaces. Product class names are isolated in the implementation of the concrete factory. It makes exchanging product families easy. Easy to change the concrete factory an application uses. Because an abstract factory creates a complete family of products, the whole product family changes at once (Ex : change GUI from Windows to Mac). Supporting new kinds of products is difficult AbstractFactory interface fixes the set of products. A new product requires changing the AbstractFactory and all of its subclasses Languages in Depth series: Java Programming 9
What Does It Print? Exception java.lang.StackOver flowError Languages in Depth series: Java Programming 10
What Does It Print? Instance variable initializers run before the body of the constructor The initializer for the variable internalInstance invokes the constructor recursively. These recursive invocations cause a StackOverflowError before the constructor body ever gets a chance to execute. Because StackOverflowError is a subtype of Error rather than Exception , the catch clause in main doesn't catch it. Languages in Depth series: Java Programming 11
Animal Farm What does the following Java program print? false Languages in Depth series: Java Programming 12
Animal Farm The “+” operator has higher priority than “==“. The expression could be written : System.out.println(("Animals are equal: " + pig) == dog); Solution : uses parenthesizes. The expresssion becomes System.out.println("Animals are equal: " + (pig == dog)); It prints “Animal are equal: false”, because == tests objects references, not value Correction : use “equals” method : System.out.println("Animals are equal: " + pig.equals(dog)); Languages in Depth series: Java Programming 13
Indecisive What does the following Java program print? false Languages in Depth series: Java Programming 14
Indecisive In a try-finally statement, the finally block is always executed when control leaves the try block. When both the try block and the finally block complete abruptly, the reason for the abrupt completion in the try block is discarded. The program tries to return true but finally it returns false . Languages in Depth series: Java Programming 15
Questions? Languages in Depth series: Java Programming 16
Exercise Session 7 Pattern of the Day Adapter Quizzes Languages in Depth series: Java Programming 17
Recommend
More recommend