DESIGN
PATTERN
ADVANCED I
CLOVERLAB. INC
JIEYI WU
2018/10/31
OUTLINE
• ISSUES OF SINGLETON PATTERN
• PRACTICING DESIGN A SYSTEM
3
Issues of
Single Pattern
For what there’re so many
ways to implement?
• Normal Singleton
• Thread-safe Singleton (Synchroized)
• Thread-safe Singleton (Double Click Lock)
• Private inner static Singleton
• Initialized First Singleton
• Enum Singleton
Memory
Memory
Leak
Resource
Costing
Reflection Serialization Cloneable
Real issues for Singleton
That’s why there’re so many ways
4
Issue of Reflection
5
Output:-
instance1.hashCode():- 366712642
instance2.hashCode():- 1829164700
6
Solution for
Reflection
As enums don’t have any constructor so it is
not possible for Reflection to utilize it.
Issue of Serialization
7
Output:-
instance1.hashCode():- 1550089733
instance2.hashCode():- 865113938
8
Solution for
Serialization
Issue of Cloneable
9
Output:-
instance1.hashCode():- 366712642
instance2.hashCode():- 1829164700
10
Overcome for
Cloneable
PRACTICING
DESIGN A SYSTEM!
LET’S DIRTY OUR HAND!
11
Design data structures and algorithms for in-memory file system
12
• Composite Pattern
• Folder
• Files
• Name
• Created Date
• Create
• Write
• Modify
• Delete
Abstract Class
13
• All Common Properties
• Name
• Created Date
Concrete Class
14
• Keep all Files & Directories
§ Three type of Cars( ! , " , #)
§ Spaces
Bus needs 3 spaces
Car needs 1 space
Auto-bike needs 1 space
Design a parking lot
Using object-oriented principles
15
16
Q & A
THANK YOU
17
Jieyi Wu

Design pattern advanced i