Posted on by & filed under Design Patterns, java, learning, Learning & Development, programming, Programming & Development, Tech.

Welcome to day five of our seven-day Design Patterns Series. Today, we’ll be learning the Facade and Template Method patterns. Open a free 10-day Safari trial account to access the series materials.

Facade and Template Method Patterns

Today, we’ll begin by looking at the Facade pattern. Sometimes, you’ll have a complex, powerful subsystem with lots of objects, but you’ll find that many of your clients have relatively simple requests — they don’t need to be exposed to the full complexity of that subsystem. For those clients, you can provide a Facade, a simplified interface that makes the subsystem easier to use.

Then, we’ll be looking at the Template Method pattern. Template Methods are used in superclasses: the superclass defines the “skeleton” of an algorithm, while subclasses “fill in the details” of how to perform certain steps.

Facade

Dive in Head First

  1. Start by reading the rest of Chapter 7 from Head First Design Patterns:

    Chapter 7. The Adapter and Facade Patterns: Being Adaptive
    Start from the section titled And now for something different…

  2. Now, skim the original description from Design Patterns: Elements of Reusable Object-Oriented Software:.

    Object Structural: Facade

Test Your Knowledge

  1. What is the difference between an Adapter and a Facade?
    Answer

  2. True or False: A Facade encapsulates a subsystem, isolating its components from use by other clients.

Template Method

Dive in Head First

  1. Start by reading chapter 8 from Head First Design Patterns:

    Chapter 8. The Template Method Pattern: Encapsulating Algorithms

  2. Now, skim the original description from Design Patterns: Elements of Reusable Object-Oriented Software.

    Class Behavioral: Template Method

Test Your Knowledge

  1. True or False: Template Methods are used to implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.

  2. True or False: Template methods can be used when common behavior among subclasses should be factored and localized in a common class to avoid code duplication.

  3. How can you decide whether to use abstract methods or hooks when creating a template method?
    Answer

Extend Your Knowledge

For more information on the Facade pattern, take a look at these other resources on Safari:

For more information on the Template Method pattern, check out these resources:

Tags: design patterns, development, gang of four, java, Learning, programming,

Comments are closed.