Chapter 6

Design Patterns

Software design patterns use one or more objects together to solve a common use case. These use cases often focus on code reuse, extensibility, or generally providing a solid foundation for future development.

Being an object-oriented language, Java makes use of many design patterns throughout the standard library APIs, and it is quite simple to create your own implementation of some common design patterns.

This chapter looks at several common design patterns, examining some of those found in the standard Java APIs, as well as implementing some patterns too.

Hundreds, if not thousands, of software design patterns exist, and you may create some new ones that work for you in your everyday job. The patterns described here are some of the more common and well-known patterns. Interviewers often like asking questions about design patterns, because they provide scope for a lot of discussion, and they have very clear definitions.

Try the examples in this chapter, think of your own scenarios, and see if you can apply the patterns to those, too. Try to justify if a given pattern is good for a given situation, and see if you can make other patterns fit that same situation, too. It won’t always work, but sometimes it is possible for patterns to be interchanged in certain situations.

Investigating Example Patterns

How is the Builder Pattern useful?

When you want to create an object that has many fields, using constructors can become unwieldy and confusing. Imagine ...

Get Java Programming Interviews Exposed now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.