Exercise Session 6 Andrei Vancea Todays Exercise Session - - PowerPoint PPT Presentation

exercise session 6
SMART_READER_LITE
LIVE PREVIEW

Exercise Session 6 Andrei Vancea Todays Exercise Session - - PowerPoint PPT Presentation

Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Exercise Session 6 Andrei Vancea Todays Exercise Session Assignment VI Hints Questions Pattern of the Day The Abstract Factory


slide-1
SLIDE 1

Languages in Depth Series: Java Programming

  • Prof. Dr. Bertrand Meyer

Chair of Software Engineering

Exercise Session 6

Andrei Vancea

slide-2
SLIDE 2

2

Languages in Depth series: Java Programming

Today’s Exercise Session

Assignment VI

  • Hints
  • Questions

Pattern of the Day

  • The Abstract Factory

Quizzes

slide-3
SLIDE 3

3

Languages in Depth series: Java Programming

Assignment V

Hints Questions

slide-4
SLIDE 4

4

Languages in Depth series: Java Programming

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

slide-5
SLIDE 5

5

Languages in Depth series: Java Programming

Abstract Factory - Structure

AbstractFactory

  • Declares an interface for operations that create abstract

product objects. ConcreteFactory

  • Implements the operations to create concrete product
  • bjects.
slide-6
SLIDE 6

6

Languages in Depth series: Java Programming

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.

slide-7
SLIDE 7

7

Languages in Depth series: Java Programming

Abstract Factory – Example*

*from http://en.wikipedia.org/wiki/Abstract_factory_pattern

slide-8
SLIDE 8

8

Languages in Depth series: Java Programming

Abstract Factory – Example

slide-9
SLIDE 9

9

Languages in Depth series: Java Programming

Abstract Factory – Consequences

It isolates concrete classes.

  • Clients manipulate instances through their abstract

interfaces.

  • Product class names are isolated in the implementation
  • f 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

slide-10
SLIDE 10

10

Languages in Depth series: Java Programming

What Does It Print?

Exception java.lang.StackOver flowError

slide-11
SLIDE 11

11

Languages in Depth series: Java Programming

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.

slide-12
SLIDE 12

12

Languages in Depth series: Java Programming

Animal Farm

What does the following Java program print?

false

slide-13
SLIDE 13

13

Languages in Depth series: Java Programming

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
  • bjects references, not value

Correction : use “equals” method :

  • System.out.println("Animals are equal: " +

pig.equals(dog));

slide-14
SLIDE 14

14

Languages in Depth series: Java Programming

Indecisive

What does the following Java program print?

false

slide-15
SLIDE 15

15

Languages in Depth series: Java Programming

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.

slide-16
SLIDE 16

16

Languages in Depth series: Java Programming

Questions?

slide-17
SLIDE 17

17

Languages in Depth series: Java Programming

Exercise Session 7

Pattern of the Day

  • Adapter

Quizzes