2. Java 7: Design Patterns Flashcards
1
Q
What are the Java 7 design patterns you need to know?
A
- Singleton pattern
- Composition pattern
- DAO pattern
- Factory pattern
2
Q
What are two ways to make the Sinlgeton pattern thread-safe?
A
- Make the whole getInstance method synchronized (performance impact because it will always enter a synchronized method)
- Add a synchronized block to the getInstance methodm (less performance impact) f.e:
MySingleton getInstance() { if(instance == null) { synchronized(MySingleton.class) { if(instance == null) { instance = new MySingleton(); } } } return instance; }
3
Q
How can be tested if TypeB needs inheritance?
A
Does TypeB want to expose the complete interface of TypeA so that TypeB van be used where TypeA is expected?
4
Q
How can be tested if TypeB needs composition?
A
Does TypeB only want some part of the behaviour of TypeA exposed?
5
Q
What is a DAO?
A
Data Access Object. An object that provides a abstract interface to some type of database or persistence layer.