Java Design Patterns I
Total Page:16
File Type:pdf, Size:1020Kb
Java Design Patterns i Java Design Patterns Java Design Patterns ii Contents 1 Introduction to Design Patterns 1 1.1 Introduction......................................................1 1.2 What are Design Patterns...............................................1 1.3 Why use them.....................................................2 1.4 How to select and use one...............................................2 1.5 Categorization of patterns...............................................3 1.5.1 Creational patterns..............................................3 1.5.2 Structural patterns..............................................3 1.5.3 Behavior patterns...............................................3 2 Adapter Design Pattern 5 2.1 Adapter Pattern....................................................5 2.2 An Adapter to rescue.................................................6 2.3 Solution to the problem................................................7 2.4 Class Adapter..................................................... 11 2.5 When to use Adapter Pattern............................................. 12 2.6 Download the Source Code.............................................. 12 3 Facade Design Pattern 13 3.1 Introduction...................................................... 13 3.2 What is the Facade Pattern.............................................. 13 3.3 Solution to the problem................................................ 14 3.4 Use of the Facade Pattern............................................... 16 3.5 Download the Source Code.............................................. 16 4 Composite Design Pattern 17 4.1 Introduction...................................................... 17 4.2 What is the Composite Pattern............................................ 17 4.3 Example of Composite Pattern............................................ 19 4.4 When to use Composite Pattern............................................ 23 4.5 Download the Source Code.............................................. 23 Java Design Patterns iii 5 Bridge Design Pattern 24 5.1 Introduction...................................................... 24 5.2 What is Bridge Pattern................................................ 25 5.3 Solution to the Problem................................................ 26 5.4 Use of Bridge Pattern................................................. 30 5.5 Download the Source Code.............................................. 30 6 Singleton Design Pattern 31 6.1 Singleton Pattern................................................... 31 6.2 How to create a class using the Singleton Pattern.................................. 31 6.3 When to use Singleton................................................ 35 6.4 Download the Source Code.............................................. 35 7 Observer Design Pattern 36 7.1 Observer Pattern................................................... 36 7.2 What is the Observer Pattern............................................. 36 7.3 Implementing Observer Pattern............................................ 37 7.4 Java’s built-in Observer Pattern............................................ 42 7.5 When to use the Observer Pattern........................................... 44 7.6 Download the Source Code.............................................. 44 8 Mediator Design Pattern 45 8.1 Introduction...................................................... 45 8.2 What is the Mediator Pattern............................................. 46 8.3 Implementing the Mediator Pattern.......................................... 47 8.4 When to use the Mediator Pattern.......................................... 53 8.5 Mediator Pattern in JDK............................................... 54 8.6 Download the Source Code.............................................. 54 9 Proxy Design Pattern 55 9.1 Introduction...................................................... 55 9.2 What is the Proxy Pattern............................................... 55 9.3 Remote Proxy..................................................... 56 9.4 Virtual Proxy..................................................... 58 9.5 Protection Proxy................................................... 62 9.6 When to use the Proxy Pattern............................................ 65 9.7 Other Proxies..................................................... 65 9.8 Proxy Pattern in JDK................................................. 65 9.9 Download the Source Code.............................................. 65 Java Design Patterns iv 10 Chain of Responsibility Design Pattern 66 10.1 Chain of Responsibility Pattern............................................ 66 10.2 What is the Chain of Responsibility Pattern..................................... 66 10.3 Implementing Chain of Responsibility........................................ 67 10.4 When to use the Chain of Responsibility Pattern................................... 73 10.5 Chain of Responsibility in JDK............................................ 73 10.6 Download the Source Code.............................................. 74 11 Flyweight Design Pattern 75 11.1 Flyweight Pattern................................................... 75 11.2 What is the Flyweight Pattern............................................. 76 11.3 Solution to the Problem................................................ 77 11.4 When to use the Flyweight Pattern.......................................... 80 11.5 Flyweight in the JDK................................................. 80 11.6 Download the Source Code.............................................. 81 12 Builder Design Pattern 82 12.1 Builder Pattern.................................................... 82 12.2 What is the Builder Pattern.............................................. 82 12.3 Implementing the Builder Pattern........................................... 84 12.4 Another form of the Builder Pattern......................................... 88 12.5 When to use the Builder Pattern........................................... 93 12.6 Builder Pattern in JDK................................................ 93 12.7 Download the Source Code.............................................. 93 13 Factory Method Design Pattern 94 13.1 Introduction...................................................... 94 13.2 What is the Factory Method Pattern......................................... 94 13.3 Implementing Factory Method Pattern........................................ 95 13.4 When to use the Factory Method Pattern....................................... 98 13.5 Factory Method Pattern in JDK............................................ 98 13.6 Download the Source Code.............................................. 98 14 Abstract Factory Method Design Pattern 99 14.1 Introduction...................................................... 99 14.2 What is the Abstract Factory Design Pattern..................................... 99 14.3 Implementing the Abstract Factory Design Pattern.................................. 100 14.4 When to use the Abstract Factory Design Pattern.................................. 104 14.5 Abstract Factory Pattern in JDK........................................... 104 14.6 Download the Source Code.............................................. 105 Java Design Patterns v 15 Prototype Design Pattern 106 15.1 Introduction...................................................... 106 15.2 What is the Prototype Design Pattern......................................... 106 15.3 Solution to the Problem................................................ 107 15.4 When to use the Prototype Design Pattern...................................... 110 15.5 Prototype Pattern in JDK............................................... 110 15.6 Download the Source Code.............................................. 111 16 Memento Design Pattern 112 16.1 Introduction...................................................... 112 16.2 What is the Memento Design Pattern......................................... 112 16.3 Implementing the Memento Design Pattern..................................... 113 16.4 When to use the Memento Pattern.......................................... 117 16.5 Memento Pattern in JDK............................................... 117 16.6 Download the Source Code.............................................. 117 17 Template Design Pattern 118 17.1 Introduction...................................................... 118 17.2 What is the Template Design Pattern......................................... 118 17.3 Implementing the Template Design Pattern...................................... 119 17.4 Introducing a hook inside the template........................................ 122 17.5 When to use the Template Design Pattern...................................... 124 17.6 Template Pattern in JDK............................................... 124 17.7 Download the Source Code.............................................. 124 18 State Design Pattern 125 18.1 Introduction...................................................... 125 18.2 What is the State Design Pattern........................................... 125 18.3 Implementing the State Design Pattern........................................ 126 18.4 When to use the State Design Pattern......................................... 132 18.5 State Design Pattern in Java.............................................. 132 18.6 Download the Source Code.............................................. 132 19 Strategy Design Pattern 133 19.1 Introduction.....................................................