Intro to Design Patterns
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 64 of 87.
Design patterns are proven solutions to common problems that developers encounter repeatedly when building software. Rather than reinventing the wheel each time, you can apply these time-tested templates to solve recurring design challenges.
Think of design patterns as blueprints. They don't give you finished code to copy-paste, but they describe how to structure your classes and objects to solve a specific type of problem. The patterns you'll learn leverage the OOP concepts you've already mastered—inheritance, interfaces, polymorphism, and composition.
Design patterns fall into three main categories:
| Category | Purpose | Examples |
|---|---|---|
| Creational | How objects are created | Singleton, Factory, Builder |
| Structural | How objects are composed | Adapter, Decorator, Composite |
| Behavioral | How objects communicate | Observer, Strategy, Command |
Why learn design patterns? They give you a shared vocabulary with other developers—saying "use a Factory here" instantly communicates a complex idea. They also help you write more flexible, maintainable code by applying principles like "program to an interface, not an implementation."
In the upcoming lessons, you'll implement several essential patterns, starting with creational patterns like Singleton and Factory, then moving to behavioral patterns like Observer and Strategy.
Challenge
EasyLet's build a simple pattern classifier that demonstrates your understanding of the three design pattern categories! You'll create a system that categorizes patterns and explains their purpose, organizing your code across multiple files.
You'll create three files:
Pattern.java: Create an interface calledPatternthat defines the contract for all design patterns. Your interface should declare two methods:String getCategory()which returns the pattern's category (Creational, Structural, or Behavioral), andString getPurpose()which returns a description of what that category of patterns does.PatternInfo.java: Create three classes that implement yourPatterninterface, each representing one category of design patterns:CreationalPattern- should return"Creational"for category and"How objects are created"for purpose.StructuralPattern- should return"Structural"for category and"How objects are composed"for purpose.BehavioralPattern- should return"Behavioral"for category and"How objects communicate"for purpose.Main.java: Bring your pattern classifier together! You'll receive one input: a pattern name (String) that will be one of"Singleton","Factory","Adapter","Decorator","Observer", or"Strategy".Based on the input, create the appropriate pattern object:
"Singleton"and"Factory"are Creational patterns"Adapter"and"Decorator"are Structural patterns"Observer"and"Strategy"are Behavioral patterns
Store your pattern object in a variable of type
Pattern(the interface), demonstrating the principle of programming to an interface. Then print the following three lines:Pattern: [input pattern name]Category: [category from getCategory()]Purpose: [purpose from getPurpose()]
You will receive one input: the name of a design pattern (String).
This challenge reinforces the key insight from the lesson: design patterns are organized into categories based on what problem they solve. By using an interface, you're also practicing the principle of "programming to an interface, not an implementation" - a core concept that makes design patterns so powerful!
Cheat sheet
Design patterns are proven solutions to common problems in software development. They are blueprints that describe how to structure classes and objects to solve specific types of problems.
Design patterns fall into three main categories:
| Category | Purpose | Examples |
|---|---|---|
| Creational | How objects are created | Singleton, Factory, Builder |
| Structural | How objects are composed | Adapter, Decorator, Composite |
| Behavioral | How objects communicate | Observer, Strategy, Command |
Design patterns provide a shared vocabulary among developers and help write more flexible, maintainable code by applying principles like "program to an interface, not an implementation."
Try it yourself
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String patternName = scanner.nextLine();
// TODO: Declare a variable of type Pattern (the interface)
Pattern pattern = null;
// TODO: Based on patternName, create the appropriate pattern object:
// - "Singleton" and "Factory" are Creational patterns
// - "Adapter" and "Decorator" are Structural patterns
// - "Observer" and "Strategy" are Behavioral patterns
// TODO: Print the output in the following format:
// Pattern: [input pattern name]
// Category: [category from getCategory()]
// Purpose: [purpose from getPurpose()]
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)11Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternBuilder PatternObserver PatternStrategy Pattern3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System