Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 5 Object-Oriented Design Principles

4.Consider the following three classes: University, Department, and CSE_Department (CSE stands for Computer Science and Engineering). The University and Department classes are related with relation R1, and the Department and CSE_Department classes are related with relation R2. Which combination of these relations is appropriate?

A.R1: inheritance, R2: inheritance

B.R1: composition, R2: inheritance

C.R1: inheritance, R2: composition

D.R1: composition, R2: composition

Answer: B

(A university has many departments, so they share a has-a relationship between them, a composition relationship. CSE_Department is a department, so these two share a is-a relationship between them, an inheritance relationship.)

5.You need to model a file system where there could be subfolders and files in a folder. What is the most appropriate design choice in this case to represent the relationship between Folder and File classes?

A.Use composition to model the relationship of “a Folder object consists of File objects.”

B.Use composition to model the relationship of “a Folder object consists of File objects or Folder objects.”

C.Use inheritance to define a superclass (say FolderItem) and make Folder and File classes subclasses to this class. Use composition to model the relationship “a Folder object consists of FolderItem objects.”

D.Use inheritance between Folder and File classes to model the relationship “a

Folder is of type File.”

Answer: C

(In fact, this arrangement is referred to as a composite design pattern)

Summary

Interfaces

An interface is a set of abstract methods that defines a protocol.

An interface cannot be instantiated; however, an interface can extend another interface.

All methods declared in an interface are implicitly considered to be abstract.

Abstract class and interface are quite similar concepts. However, you should be careful to use the appropriate construct based on the context.

145

Chapter 5 Object-Oriented Design Principles

Object Composition

Inheritance implies is-a, interface implies is-like-a, and composition implies has-a relationships.

Favor composition over inheritance whenever feasible.

Program to an interface, not to an implementation.

Design Patterns

Design patterns are reusable solutions of frequently recurring design problems.

The observer design pattern improves loose coupling between subject and observers.

The singleton design pattern ensures that only one instance of the class is created.

Making sure that an intended singleton implementation is indeed singleton is a non-trivial task, especially in a multi-threaded environment.

The factory design pattern “manufactures” the required type of product on demand.

You should consider using the abstract factory design pattern when you have a family of objects to be created.

A DAO design pattern essentially separates your core business logic from your persistence logic.

146

Chapter 6

Generics and Collections

Create a generic class

Use the diamond syntax to create a collection

Analyze the interoperability of collections that use raw type and generic types

Use wrapper classes and autoboxing

Exam Topics

Create and use a List, a Set and a Deque

Create and use a Map

Use java.util.Comparator and java.lang.Comparable

Sort and search arrays and lists

Every non-trivial Java application makes use of data structures and algorithms. The Java collections framework provides a large set of readily usable general-purpose data structures and algorithms. These data structures and algorithms can be used with any suitable data type in a type-safe manner; this is achieved through the use of a language feature known as generics.

Since data structures and algorithms are implemented using generics, these two complementary topics are combined together as a single topic in the 1Z0-804 exam syllabus. In this chapter, we first introduce generics, which are useful for defining reusable data structures and algorithms. We discuss how generics offer more type safety than Object type containers. We also explore other aspects of generics such as wildcard parameters in generics and subtyping in generics.

147

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]