Object-oriented design and design patterns in Java
You practise designing classes that withstand change, the SOLID principles and the most used design patterns in small but realistic Java projects.
Level 201 · 18 lessons · 15 practice labs
Lessons
Design principles
- Cohesion and coupling: the measure of a good class
- SOLID 1: single responsibility and open-closed
- SOLID 2: substitution, interface segregation, dependency inversion
- Composition over inheritance
- Immutability and value objects
- Closed hierarchies with sealed classes and pattern matching
- Short task: split a class that does everything
Design patterns
- Creational patterns: factory and builder
- Strategy and template method
- Observer and event-driven design
- Decorator and adapter
- Dependency injection by hand
- Short task: a checker that gains new rules without code changes
Layers and contracts
- Layered architecture: interface, service, repository
- Contracts: preconditions, Objects.requireNonNull and validation
- Domain errors and designing an exception hierarchy
- Short task: split a console app into layers
Capstone
- Capstone: a password policy checker with pluggable rules
Practice labs
- Extracting the tax calculation into its own class: Move the tax calculation inside an invoice class to a separate class and show the existing tests still pass.
- Adding a new format without touching existing code: Split an exporter with an if chain per format into an interface and implementations so a new format is added only by writing a new class.
- Splitting a class that does everything: Separate an order class that validates, prices, saves and notifies into responsibilities, invert the dependencies and pass the behaviour tests unchanged.
- Writing an immutable money class: Write an immutable class holding amount and currency whose addition returns a new object and fails for different currencies.
- Replacing inheritance with composition in a stack class: Rewrite a stack class whose invariant can be bypassed because it extends ArrayList so that it holds a list inside.
- Modelling payment types with a sealed hierarchy: Rewrite a payment handler with an instanceof chain using a sealed interface, records and a pattern-matching switch so the compiler catches a missing branch for a new type.
- Replacing a many-parameter constructor with a builder: Replace a seven-parameter report request constructor with a builder that checks required fields.
- Computing shipping cost with a strategy: Separate Kuzeyyıldızı Kargo's fee rules into a strategy interface and write the factory that selects by name at run time.
- Gathering a repeated import flow into a template method: Gather the open, validate, transform and save flow copied across three importers into a template method and show by test that the resource closes on a step failure.
- Writing an adapter for an old printer class: Write an adapter fitting an old printer class with different method names to the application's output interface.
- Announcing stock events to observers: Build a structure that notifies subscribers when stock falls below a threshold where the others keep working if one observer fails and subscriptions can be removed.
- A checker that grows through manual dependency injection: Convert a checker that builds its own dependencies with new to constructor injection, wrap rules with a decorator and show a new rule is added without code changes.
- Checking preconditions in the constructor: Apply preconditions in a booking class constructor with Objects.requireNonNull and range checks, with messages naming the field.
- Designing an exception tree for domain errors: Give a lending module that throws RuntimeException everywhere an exception tree separating not-found, rule violation and infrastructure errors.
- Splitting a console app into layers: Split an app with input reading, business rules and file access in one method into interface, service and repository layers so service tests run with a fake repository.
About this course
You practise designing classes that withstand change, the SOLID principles and the most used design patterns in small but realistic Java projects.
What you will learn
- Design principles
- Design patterns
- Layers and contracts
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.