Java Design Patterns - A Hands-On Experience with Real-World Examples

von: Vaskaran Sarcar

Apress, 2018

ISBN: 9781484240786 , 512 Seiten

2. Auflage

Format: PDF, Online Lesen

Kopierschutz: Wasserzeichen

Mac OSX,Windows PC für alle DRM-fähigen eReader Apple iPad, Android Tablet PC's Online-Lesen für: Mac OSX,Linux,Windows PC

Preis: 56,99 EUR

eBook anfordern eBook anfordern

Mehr zum Inhalt

Java Design Patterns - A Hands-On Experience with Real-World Examples


 

Table of Contents

5

About the Author

19

About the Technical Reviewers

20

Acknowledgments

22

Foreword

23

Introduction

24

Part I: Gang of Four Patterns

31

Chapter 1: Singleton Pattern

32

GoF Definition

32

Concept

32

Real-World Example

32

Computer-World Example

33

Illustration

33

Class Diagram

33

Package Explorer View

34

Discussion

34

Implementation

35

Output

36

Q&A Session

36

Output

40

Eager Initialization

41

Discussion

41

Output

43

Analysis

43

Bill Pugh’s Solution

43

Double-Checked Locking

44

Chapter 2: Prototype Pattern

47

GoF Definition

47

Concept

47

Real-World Example

47

Computer-World Example

48

Illustration

48

Class Diagram

48

Package Explorer View

50

Implementation

51

Output

53

Q&A Session

54

Demonstration

57

Output

59

Chapter 3: Builder Pattern

60

GoF Definition

60

Concept

60

Real-World Example

61

Computer-World Example

61

Illustration

62

Class Diagram

63

Package Explorer View

63

Implementation

65

Output

69

Q&A Session

69

Modified Illustration

73

Modified Package Explorer View

73

Modified Implementation

75

Modified Output

79

Analysis

80

Chapter 4: Factory Method Pattern

82

GoF Definition

82

Concept

82

Real-World Example

83

Computer-World Example

83

Illustration

84

Class Diagram

84

Package Explorer View

85

Implementation

85

Output

88

Modified Implementation

88

Modified Output

90

Analysis

90

Q&A Session

90

Chapter 5: Abstract Factory Pattern

94

GoF Definition

94

Concept

94

Real-World Example

95

Computer-World Example

95

Illustration

95

Class Diagram

97

Package Explorer View

98

Implementation

99

Output

103

Q&A Session

103

Simple Factory Pattern Code Snippet

104

Factory Method Pattern Code Snippet

105

Abstract Factory Pattern Code Snippet

105

Conclusion

106

Modified Illustration

107

Modified Implementation

107

Modified Output

112

Chapter 6: Proxy Pattern

113

GoF Definition

113

Concept

113

Real-World Example

113

Computer-World Example

114

Illustration

114

Class Diagram

114

Package Explorer View

115

Implementation

116

Output

118

Q&A Session

118

Alternate Implementation

119

Output Without Lazy Instantiation

121

Analysis

122

Output with Lazy Instantiation

122

Analysis

122

Modified Package Explorer View

124

Modified Implementation

125

Modified Output

127

Chapter 7: Decorator Pattern

129

GoF Definition

129

Concept

129

Real-World Example

129

Computer-World Example

131

Illustration

132

Class Diagram

132

Package Explorer View

133

Implementation

133

Output

136

Q&A Session

137

Chapter 8: Adapter Pattern

143

GoF Definition

143

Concept

143

Real-World Example

143

Computer-World Example

144

Illustration

145

Class Diagram

146

Package Explorer View

146

Implementation

147

Output

149

Modified Illustration

149

Modified Class Diagram

149

Key Characteristics of the Modified Implementation

150

Modified Package Explorer View

152

Modified Implementation

153

Modified Output

156

Types of Adapters

156

Object Adapters

156

Class Adapters

157

Q&A Session

158

Chapter 9: Facade Pattern

161

GoF Definition

161

Concept

161

Real-World Example

161

Computer-World Example

162

Illustration

162

Class Diagram

163

Package Explorer View

164

Implementation

165

Output

169

Q&A Session

170

Chapter 10: Flyweight Pattern

173

GoF Definition

173

Concept

173

Real-World Example

174

Computer-World Example

174

Illustration

175

Class Diagram

176

Package Explorer View

176

Implementation

177

Output

183

Analysis

185

Q&A Session

185

Chapter 11: Composite Pattern

190

GoF Definition

190

Concept

190

Real-World Example

191

Computer-World Example

191

Illustration

191

Class Diagram

192

Package Explorer View

193

Implementation

194

Output

199

Q&A Session

201

Chapter 12: Bridge Pattern

204

GoF Definition

204

Concept

204

Real-World Example

204

Computer-World Example

205

Illustration

205

Class Diagram

208

Package Explorer View

209

Key Characteristics

210

Implementation

210

Output

214

Q&A Session

215

Chapter 13: Visitor Pattern

217

GoF Definition

217

Concept

217

Real-World Example

218

Computer-World Example

218

Illustration

218

Class Diagram

219

Package Explorer View

220

Implementation

220

Output

222

Modified Illustration

222

Key Characteristic of the Modified Example

223

Step 1

224

Step 2

224

Step 3

225

Step 4

226

Step 5

226

Modified Class Diagram

228

Modified Package Explorer View

228

Modified Implementation

230

Modified Output

236

Q&A Session

237

Chapter 14: Observer Pattern

240

GoF Definition

240

Concept

240

Real-World Example

243

Computer-World Example

243

Illustration

244

Class Diagram

245

Package Explorer View

245

Implementation

247

Output

250

Analysis

250

Q&A Session

250

Chapter 15: Strategy (Policy) Pattern

255

GoF Definition

255

Concept

255

Real-World Example

255

Computer world Example

256

Illustration

256

Class Diagram

257

Package Explorer View

257

Implementation

259

Output

262

Q&A Session

262

Chapter 16: Template Method Pattern

272

GoF Definition

272

Concept

272

Real-World Example

272

Computer-World Example

273

Illustration

273

Class Diagram

273

Package Explorer View

274

Implementation

275

Output

277

Q&A Session

277

Modified Implementation

278

Modified Output

281

Chapter 17: Command Pattern

283

GoF Definition

283

Concept

283

Real-World Example

283

Computer-World Example

284

Illustration

284

Class Diagram

285

Package Explorer View

286

Implementation

287

Output

290

Q&A Session

290

Modified Class Diagram

291

Modified Package Explorer View

292

Modified Implementation

294

Modified Output

300

Chapter 18: Iterator Pattern

304

GoF Definition

304

Concept

304

Real-World Example

305

Computer-World Example

306

Illustration

306

Class Diagram

307

Package Explorer View

309

First Implementation

310

Output

312

Key Characteristics of the Second Implementation

313

Second Implementation

313

Output

315

Q&A Session

316

Third Implementation

318

Output

321

Chapter 19: Memento Pattern

322

GoF Definition

322

Concept

322

Real-World Example

322

Computer-World Example

323

Illustration

323

Class Diagram

324

Package Explorer View

325

Implementation

325

Output

328

Q&A Session

329

Modified Caretaker Class

330

Modified Output

331

Analysis

332

Shallow Copy vs. Deep Copy in Java

340

Key Characteristics of the Following Program

340

Implementation

341

Output

344

Analysis

344

Modified Output

345

Analysis

345

Chapter 20: State Pattern

347

GoF Definition

347

Concept

347

Real-World Example

348

Computer-World Example

348

Illustration

348

Key Characteristics

350

Class Diagram

350

Package Explorer View

352

Implementation

353

Output

357

Q&A Session

358

Modified Package Explorer View

361

Modified Implementation

363

Modified Output

368

Chapter 21: Mediator Pattern

371

GoF Definition

371

Concept

371

Real-World Example

371

Computer-World Example

372

Illustration

373

Class Diagram

374

Package Explorer View

375

Implementation

377

Output

381

Analysis

381

Modified Illustration

381

Modified Class Diagram

383

Modified Package Explorer View

384

Modified Implementation

385

Modified Output

390

Analysis

391

Q&A Session

391

Chapter 22: Chain-of-Responsibility Pattern

395

GoF Definition

395

Concept

395

Real-World Example

396

Computer-World Example

396

Illustration

397

Class Diagram

398

Package Explorer View

399

Implementation

400

Output

403

Q&A Session

404

Chapter 23: Interpreter Pattern

406

GoF Definition

406

Concept

406

Real-World Example

408

Computer-World Example

408

Illustration

408

Class Diagram

410

Package Explorer View

411

Implementation

412

Output

416

Analysis

417

Modified Illustration

417

Modified Class Diagram

417

Modified Package Explorer View

417

Modified Implementation

418

Modified Output

423

Analysis

423

Q&A Session

424

Part II: Additional Design Patterns

425

Chapter 24: Simple Factory Pattern

426

Intent

426

Concept

426

Real-World Example

426

Computer-World example

427

Illustration

428

Class Diagram

428

Package Explorer View

429

Implementation

430

Output

432

Case1. User input:0

433

Case2. User input:1

433

Case3. An unwanted user input:2

433

Q&A Session

434

Chapter 25: Null Object Pattern

436

Concept

436

A Faulty Program

437

Output with Valid Inputs

439

Analysis with an Unwanted Input

439

Encountered Exception

440

Immediate Remedy

440

Analysis

440

Real-World Example

441

Computer-World Example

441

Illustration

441

Class Diagram

442

Package Explorer View

443

Implementation

444

Output

447

Analysis

448

Q&A Session

448

Chapter 26: MVC Pattern

452

Concept

452

Key Points to Remember

453

Variation 1

454

Variation 2

454

Variation 3

455

Real-World Example

455

Computer-World Example

456

Illustration

457

Class Diagram

457

Package Explorer View

459

Implementation

459

Output

467

Q&A Session

468

Modified Output

470

Part III: Final Discussions on Design Patterns

473

Chapter 27: Criticisms of Design Patterns

474

Q&A Session

476

Chapter 28: AntiPatterns: Avoid the Common Mistakes

480

What Is an Antipattern?

480

Brief History of Antipatterns

481

Examples of Antipatterns

482

Types of Antipatterns

484

Q&A Session

484

Chapter 29: FAQs

488

Appendix A: A Brief Overview of GoF Design Patterns

494

Key Points

495

A. Creational Patterns

496

B. Structural Patterns

496

C. Behavioral Patterns

497

Q&A Session

499

Appendix B: Winning Notes and the Road Ahead

502

Appendix C: Bibliography

503

Index

505