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

Chapter 5 Object-Oriented Design Principles

The pattern separates the persistence mechanism from rest of the application code and puts it together in one class specific to one data source. This centralization enables easier maintenance and easier bug-tracing.

It is quite easy to extend support for other data sources using this pattern. For instance, if you want to provide support for the XML-based repository in the FunPaint application, this can be achieved by defining a new class (say XMLDAO). This new class will implement your CircleDAO interface, such that you do not need to change the way you access the data source. The only thing that needs to be changed is the parameter you pass to DAOFactory to create a DAO. Easy, isn’t it?

Points to Remember

Here are points to remember for the OCPJP 7 exam:

You saw the factory design pattern implementation in the DAO design pattern. You may also employ abstract factory design pattern if you have multiple DAO objects and you have multiple persistence mechanisms.

Note that you declared TransferObject (e.g., CircleTransfer) as serializable. Any idea why you did that? Well, if you are using the transfer object between two JVMs, then the transfer object has to be serializable.

In OOP, a useful and important design principle is “separation of concerns.” This principle states that concerns (or features) should be separated (to attain minimum overlap) in order to overcome the inherent complexity involved with software design. The DAO design pattern

helps you comply with this design principle. If you are not using DAO, then your business logic will be exposed to the concrete implementation details of the persistence mechanisms—an undesirable state of affairs. Use of the DAO design pattern ensures that you separate your core logic from your persistence mechanism.

Question Time!

1.Which of the following statements is true?

A.A class can extend multiple base classes.

B.You can implement only one interface since Java does not support multiple inheritance.

C.You can implement multiple interfaces.

D.You can either extend a class or implement an interface (but not both) at a time.

Answer: C

2.Consider the following interface declaration:

public interface Rotatable { void rotate(float degree);

// insert code

}

143

Chapter 5 Object-Oriented Design Principles

Now, consider following options which could be replaced with “// insert code”:

I.public final float degree = 0;

II. public static final float degree = 0; III. public final static float degree = 0;

IV. public final float degree;

V.public float degree;

VI. float degree = 0;

Choose the correct option:

A.Options I, II, III, and VI will compile without errors.

B.Options I, II, and III will compile without errors.

C.Options I will compile without errors.

D.Options IV, and V will compile without errors.

E.All options will compile without errors.

Answer: A

3.Consider following example:

public interface xyz {

void abc() throws IOException;

}

public interface pqr {

void abc() throws FileNotFoundException;

}

public class Implementation implements xyz, pqr {

// insert code

{ /*implementation*/ }

}

Which of the following statement(s) can you insert in place of “// insert code” comment?

A.public void abc() throws IOException

B.public void abc() throws FileNotFoundException

C.public void abc() throws FileNotFoundException, IOException

D.public void abc() throws IOException, FileNotFoundException

Answer: B

(Since FileNotFoundException is a subtype of IOException, it satisfies both methods).

144

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