Lesson Introduction

Hello! Today, we'll venture into the realm of design patterns. Specifically, we'll tackle exercises that apply a single design pattern to problem-solving. Mastering these patterns is a surefire way to extend your coding skills.

Our goal today is to fortify your understanding of when and how to apply specific Object-Oriented Programming (OOP) design patterns. These patterns include Encapsulation, Abstraction, Polymorphism, and Composition.

We'll dissect four real-life scenarios and distinguish which pattern is applicable and why.

Let's get underway!

Real-life Example 1: Database Management System (Encapsulation)

The Encapsulation pattern proves beneficial for the development of a Database Management System (DBMS). Each DBMS table represents a class, the fields represent private data members, and the functions operating on this data serve as methods.

Encapsulation ensures that data members are accessed through methods that promote data integrity and prevent inadvertent anomalies. Here's a mini-code snippet to support this concept:

class Employees {
    private Map<Integer, String> employees = new HashMap<>(); // private data member

    public void addEmployee(int eid, String name) { // method to operate on private data
        employees.put(eid, name);
    }

    public void updateEmployee(int eid, String newName) { // method to operate on private data
        if (employees.containsKey(eid)) {
            employees.put(eid, newName);
        }
    }

    public String getEmployee(int eid) { // getter method for private data
        return employees.get(eid);
    }
}

public class Main {
    public static void main(String[] args) {
        Employees employees = new Employees();
        employees.addEmployee(1, "John");
        employees.addEmployee(2, "Mark");

        employees.updateEmployee(2, "Jake");

        System.out.println(employees.getEmployee(1)); // Outputs: John
        System.out.println(employees.getEmployee(2)); // Outputs: Jake
    }
}

In this context, Encapsulation restricts direct access to employee data, presenting a protective layer via designated methods.

Real-life Example 2: Graphic User Interface (GUI) Development (Polymorphism)

When transitioning to GUI development, consider the creation of controls like buttons or checkboxes. Despite belonging to the same class, each responds differently when clicked. This situation illustrates Polymorphism, which allows us to handle different objects uniformly via a common interface.

Check out this illustrative example:

class Control {
    public void click() {
        // method that can be overridden
    }
}

class Button extends Control {
    @Override
    public void click() {
        System.out.println("Button Clicked!"); // overridden method
    }
}

class CheckBox extends Control {
    @Override
    public void click() {
        System.out.println("CheckBox Clicked!"); // overridden method
    }
}

public class Main {
    public static void main(String[] args) {
        Control b = new Button();
        Control c = new CheckBox();

        // Click Controls
        b.click(); // Outputs: Button Clicked!
        c.click(); // Outputs: CheckBox Clicked!
    }
}

Despite sharing the common click interface, different controls display unique responses. This characteristic demonstrates Polymorphism.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal