CodeGym/Java Blog/Design Patterns in Java/Design patterns in Java [Part 2]
Author
Edward Izraitel
Software Engineer at Champions Oncology

Design patterns in Java [Part 2]

Published in the Design Patterns in Java group
members
Hello everyone. In my previous article, I briefly described each pattern. In this article, I will try to show, in detail, how to use the patterns.
Design patterns in Java [Part 2] - 1

Creational

Singleton

Description: Restricts the creation of a class to a single instance, and provides access to that single instance. The class's constructor is private. The getInstance() method creates only one instance of the class. Implementation:
class Singleton {
    private static Singleton instance = null;
    private Singleton() {}
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
		}
        return instance;
    }
    public void setUp() {
        System.out.println("setUp");
    }
}

public class SingletonTest { // Test
    public static void main(String[] args){
        Singleton singelton = Singleton.getInstance();
        singelton.setUp();
    }
}

Factory

Description: Used when we have a superclass with multiple subclasses and we need to return a subclass based on input. The class doesn't know what type of object it should create. Objects are created based on the inputs. Implementation:
class Factory {
    public OS getCurrentOS(String inputOS) {
        OS os = null;
        if (inputOS.equals("windows")) {
            os = new windowsOS();
        } else if (inputOS.equals("linux")) {
            os = new linuxOS();
        } else if (inputOS.equals("mac")) {
            os = new macOS();
        }
        return os;
    }
}
interface OS {
    void getOS();
}
class windowsOS implements OS {
    public void getOS () {
        System.out.println("uses Windows");
    }
}
class linuxOS implements OS {
    public void getOS () {
        System.out.println("uses Linux");
    }
}
class macOS implements OS {
    public void getOS () {
        System.out.println("uses macOS");
    }
}

public class FactoryTest { // Test
    public static void main(String[] args){
         String osName = "linux";
        Factory factory = new Factory();
        OS os = factory.getCurrentOS(osName);
        os.getOS();
    }
}

Abstract factory

Description: Lets you select a specific factory implementation from a family of possible factories. Creates a family of related objects. Easy to expand. Implementation:
interface Lada {
    long getLadaPrice();
}
interface Ferrari {
    long getFerrariPrice();
}
interface Porshe {
    long getPorshePrice();
}
interface InteAbsFactory {
    Lada getLada();
    Ferrari getFerrari();
    Porshe getPorshe();
}
class UaLadaImpl implements Lada { // First
    public long getLadaPrice() {
        return 1000;
    }
}
class UaFerrariImpl implements Ferrari {
    public long getFerrariPrice() {
        return 3000;
    }
}
class UaPorsheImpl implements Porshe {
    public long getPorshePrice() {
        return 2000;
    }
}
class UaCarPriceAbsFactory implements InteAbsFactory {
    public Lada getLada() {
        return new UaLadaImpl();
    }
    public Ferrari getFerrari() {
        return new UaFerrariImpl();
    }
    public Porshe getPorshe() {
        return new UaPorsheImpl();
    }
} // First
class RuLadaImpl implements Lada { // Second
    public long getLadaPrice() {
        return 10000;
    }
}
class RuFerrariImpl implements Ferrari {
    public long getFerrariPrice() {
        return 30000;
    }
}
class RuPorsheImpl implements Porshe {
    public long getPorshePrice() {
        return 20000;
    }
}
class RuCarPriceAbsFactory implements InteAbsFactory {
    public Lada getLada() {
        return new RuLadaImpl();
    }
    public Ferrari getFerrari() {
        return new RuFerrariImpl();
    }
    public Porshe getPorshe() {
        return new RuPorsheImpl();
    }
} // Second

public class AbstractFactoryTest { // Test
    public static void main(String[] args) {
        String country = "UA";
        InteAbsFactory ifactory = null;
        if(country.equals("UA")) {
            ifactory = new UaCarPriceAbsFactory();
        } else if(country.equals("RU")) {
            ifactory = new RuCarPriceAbsFactory();
        }

        Lada lada = ifactory.getLada();
        System.out.println(lada.getLadaPrice());
    }
}

Builder

Description: Used to create a complex object using simple objects. It gradually creates a large object from a small, simple object. Allows you to change the internal representation of the end product. Implementation:
class Car {
    public void buildBase() {
        print("Building the base");
    }
    public void buildWheels() {
        print("Installing wheels");
    }
    public void buildEngine(Engine engine) {
        print("Installing engine: " + engine.getEngineType());
    }
    private void print(String msg){
        System.out.println(msg);
    }
}
interface Engine {
    String getEngineType();
}
class EngineOne implements Engine {
    public String getEngineType() {
        return "First engine";
    }
}
class EngineTwo implements Engine {
    public String getEngineType() {
        return "Second engine";
    }
}
abstract class Builder {
    protected Car car;
    public abstract Car buildCar();
}
class OneBuilderImpl extends Builder {
    public OneBuilderImpl(){
        car = new Car();
    }
    public Car buildCar() {
        car.buildBase();
        car.buildWheels();
        Engine engine = new EngineOne();
        car.buildEngine(engine);
        return car;
    }
}
class TwoBuilderImpl extends Builder {
    public TwoBuilderImpl(){
        car = new Car();
    }
    public Car buildCar() {
        car.buildBase();
        car.buildWheels();
        Engine engine = new EngineOne();
        car.buildEngine(engine);
        car.buildWheels();
        engine = new EngineTwo();
        car.buildEngine(engine);
        return car;
    }
}
class Build {
    private Builder builder;
    public Build(int i){
        if(i == 1) {
            builder = new OneBuilderImpl();
        } else if(i == 2) {
            builder = new TwoBuilderImpl();
        }
    }
    public Car buildCar(){
        return builder.buildCar();
    }
}

public class BuilderTest { // Test
    public static void main(String[] args) {
        Build build = new Build(1);
        build.buildCar();
    }
}

Prototype

Description: Helps improve performance when creating duplicate objects; instead of creating a new object it creates and returns a clone of an existing object. Clones an existing object. Implementation:
interface Copyable {
    Copyable copy();
}
class ComplicatedObject implements Copyable {
    private Type type;
    public enum Type {
        ONE, TWO
    }
    public ComplicatedObject copy() {
        ComplicatedObject complicatedObject = new ComplicatedObject();
        return complicatedObject;
    }
    public void setType(Type type) {
        this.type = type;
    }
}

public class PrototypeTest { // Test
    public static void main(String[] args) {
        ComplicatedObject prototype = new ComplicatedObject();
        ComplicatedObject clone = prototype.copy();
        clone.setType(ComplicatedObject.Type.ONE);
    }
}

Structural

Adapter

Description: We can use the adapter pattern to combine two incompatible interfaces. It acts as a converter between two incompatible objects. Implementation:
class PBank {
	private int balance;
	public PBank() { balance = 100; }
	public void getBalance() {
		System.out.println("PBank balance = " + balance);
	}
}
class ABank {
	private int balance;
	public ABank() { balance = 200; }
	public void getBalance() {
		System.out.println("ABank balance = " + balance);
	}
}
class PBankAdapter extends PBank {
	private ABank abank;
	public PBankAdapter(ABank abank) {
		this.abank = abank;
	}
	public void getBalance() {
		abank.getBalance();
	}
}

public class AdapterTest { // Test
	public static void main(String[] args) {
		PBank pbank = new PBank();
		pbank.getBalance();
		PBankAdapter abank = new PBankAdapter(new ABank());
		abank.getBalance();
	}
}

Composite

Description: Groups several objects into a tree structure using one class. Allows you to work with multiple classes through a single object. Implementation:
import java.util.ArrayList;
import java.util.List;
interface Car {
    void draw(String color);
}
class SportsCar implements Car {
    public void draw(String color) {
        System.out.println("SportsCar color: " + color);
    }
}
class UnknownCar implements Car {
    public void draw(String color) {
        System.out.println("UnknownCar color: " + color);
    }
}
class Drawing implements Car {
    private List<Car> cars = new ArrayList<Car>();
    public void draw(String color) {
        for(Car car : cars) {
            car.draw(color);
        }
    }
    public void add(Car s){
        this.cars.add(s);
    }
    public void clear(){
		System.out.println();
        this.cars.clear();
    }
}

public class CompositeTest { // Test
    public static void main(String[] args) {
        Car sportsCar = new SportsCar();
        Car unknownCar = new UnknownCar();
        Drawing drawing = new Drawing();
        drawing.add(sportsCar);
        drawing.add(unknownCar);
        drawing.draw("green");
        drawing.clear();
        drawing.add(sportsCar);
        drawing.add(unknownCar);
        drawing.draw("white");
    }
}

Proxy

Description: Represents objects that can control other objects by intercepting their method calls. You can intercept the original object's method call. Implementation:
interface Image {
    void display();
}
class RealImage implements Image {
    private String file;
    public RealImage(String file){
        this.file = file;
        load(file);
    }
    private void load(String file){
        System.out.println("Loading " + file);
    }
    public void display() {
        System.out.println("Displaying " + file);
    }
}
class ProxyImage implements Image {
    private String file;
    private RealImage image;
    public ProxyImage(String file){
        this.file = file;
    }
    public void display() {
        if(image == null){
            image = new RealImage(file);
        }
        image.display();
    }
}

public class ProxyTest { // Test
    public static void main(String[] args) {
        Image image = new ProxyImage("test.jpg");
        image.display();
        image.display();
    }
}

Flyweight

Description: Reuses objects instead of creating a large number of similar objects. Saves memory. Implementation:
class Flyweight {
    private int row;
    public Flyweight(int row) {
        this.row = row;
        System.out.println("ctor: " + this.row);
    }
    void report(int col) {
        System.out.print(" " + row + col);
    }
}

class Factory {
    private Flyweight[] pool;
    public Factory(int maxRows) {
        pool = new Flyweight[maxRows];
    }
    public Flyweight getFlyweight(int row) {
        if (pool[row] == null) {
            pool[row] = new Flyweight(row);
        }
        return pool[row];
    }
}

public class FlyweightTest { // Test
    public static void main(String[] args) {
        int rows = 5;
        Factory theFactory = new Factory(rows);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < rows; j++) {
                theFactory.getFlyweight(i).report(j);
            }
            System.out.println();
        }
    }
}

Facade

Description: Hides a complex system of classes by adding all of the calls to one object. Places the method calls of several complex objects into a single object. Implementation:
interface Car {
    void start();
    void stop();
}
class Key implements Car {
    public void start() {
        System.out.println("Insert keys");
    }
    public void stop() {
        System.out.println("Remove keys");
    }
}
class Engine implements Car {
    public void start() {
        System.out.println("Start engine");
    }
    public void stop() {
        System.out.println ("Stop engine");
    }
}
class Facade {
    private Key key;
    private Engine engine;
    public Facade() {
        key = new Key();
        engine = new Engine();
    }
    public void startCar() {
        key.start();
        engine.start();
    }
    public void stoptCar() {
        key.stop();
        engine.stop();
    }
}

public class FacadeTest { // Test
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.startCar();
        System.out.println();
        facade.stoptCar();
    }
}

Bridge

Description: Separates the implementation from the abstraction, making it possible to change each independently of the other. Makes specific classes independent of classes implementing an interface. Implementation:
interface Engine {
    void setEngine();
}
abstract class Car {
    protected Engine engine;
    public Car(Engine engine){
        this.engine = engine;
    }
    abstract public void setEngine();
}
class SportsCar extends Car {
    public SportsCar(Engine engine) {
        super(engine);
    }
    public void setEngine() {
        System.out.print("SportsCar engine: ");
        engine.setEngine();
    }
}
class UnknownCar extends Car {
    public UnknownCar(Engine engine) {
        super(engine);
    }
    public void setEngine() {
        System.out.print("UnknownCar engine: ");
        engine.setEngine();
    }
}
class PerformanceEngine implements Engine {
    public void setEngine(){
        System.out.println("sport");
    }
}
class UnknownEngine implements Engine {
    public void setEngine(){
        System.out.println("unknown");
    }
}
public class BridgeTest { // Test
    public static void main(String[] args) {
        Car sportsCar = new SportsCar(new PerformanceEngine());
        sportsCar.setEngine();
        System.out.println();
        Car unknownCar = new UnknownCar(new UnknownEngine());
        unknownCar.setEngine();
    }
}

Decorator

Description: Adds new functionality to an existing object without tying into its structure. Implementation:
interface Car {
    void draw();
}
class SportsCar implements Car {
    public void draw() {
        System.out.println("SportsCar");
    }
}
class UnknownCar implements Car {
    public void draw() {
        System.out.println("UnknownCar");
    }
}
abstract class CarDecorator implements Car {
    protected Car decorated;
    public CarDecorator(Car decorated){
        this.decorated = decorated;
    }
    public void draw(){
        decorated.draw();
    }
}
class BlueCarDecorator extends CarDecorator {
    public BlueCarDecorator(Car decorated) {
        super(decorated);
    }
    public void draw() {
        decorated.draw();
        setColor();
    }
    private void setColor(){
        System.out.println("Color: red");
    }
}

public class DecoratorTest { // Test
    public static void main(String[] args) {
        Car sportsCar = new SportsCar();
        Car blueUnknownCar = new BlueCarDecorator(new UnknownCar());
        sportsCar.draw();
        System.out.println();
        blueUnknownCar.draw();
    }
}

Behavioral

Template method

Description: Lets you define a basic algorithm and allows descendants to override some steps of the algorithm without changing its overall structure. Implementation:
abstract class Car {
    abstract void startEngine();
    abstract void stopEngine();

    public final void start(){
        startEngine();
        stopEngine();
    }
}
class CarOne extends Car {
    public void startEngine() {
        System.out.println("Start engine.");
    }
    public void stopEngine() {
        System.out.println("Stop engine.");
    }
}
class CarTwo extends Car {
    public void startEngine() {
        System.out.println("Start engine.");
    }
    public void stopEngine() {
        System.out.println("Stop engine.");
    }
}

public class TemplateTest { // Test
    public static void main(String[] args) {
        Car car1 = new CarOne();
        car1.start();
        System.out.println();
        Car car2 = new CarTwo();
        car2.start();
    }
}

Mediator

Description: Provides an intermediary class that handles all communication between different classes. Implementation:
class Mediator {
    public static void sendMessage(User user, String msg){
        System.out.println(user.getName() + ": " + msg);
    }
}
class User {
    private String name;
    public User(String name){
        this.name  = name;
    }
    public String getName() {
        return name;
    }
    public void sendMessage(String msg){
        Mediator.sendMessage(this, msg);
    }
}

public class MediatorTest { // Test
    public static void main(String[] args) {
        User user1 = new User("user1");
        User user2 = new User("user2");
        user1.sendMessage("message1");
        user2.sendMessage("message2");
    }
}

Chain of responsibility

Description: Makes it possible to avoid strict dependence between the sender and receiver of a request; moreover, the request can be processed by several objects. Implementation:
interface Payment {
    void setNext(Payment payment);
    void pay();
}
class VisaPayment implements Payment {
    private Payment payment;
    public void setNext(Payment payment) {
        this.payment = payment;
    }
    public void pay() {
        System.out.println("Visa Payment");
    }
}
class PayPalPayment implements Payment {
    private Payment payment;
    public void setNext(Payment payment) {
        this.payment = payment;
    }
    public void pay() {
        System.out.println("PayPal Payment");
    }
}

public class ChainofResponsibilityTest { // Test
    public static void main(String[] args) {
        Payment visaPayment = new VisaPayment();
        Payment payPalPayment = new PayPalPayment();
        visaPayment.setNext(payPalPayment);
        visaPayment.pay();
    }
}

Observer

Description: Allows one object to observe what happens in other objects. Implementation:
import java.util.ArrayList;
import java.util.List;
interface Observer {
    void event(List<String> strings);
}
class University {
    private List<Observer> observers = new ArrayList<Observer>();
    private List<String> students = new ArrayList<String>();
    public void addStudent(String name) {
        students.add(name);
        notifyObservers();
    }
    public void removeStudent(String name) {
        students.remove(name);
        notifyObservers();
    }
    public void addObserver(Observer observer){
        observers.add(observer);
    }
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }
    public void notifyObservers(){
        for (Observer observer : observers) {
            observer.event(students);
        }
    }
}
class Director implements Observer {
    public void event(List<String> strings) {
        System.out.println("The list of students has changed: " + strings);
    }
}

public class ObserverTest { // Test
    public static void main(String[] args) {
        University university = new University();
        Director director = new Director();
        university.addStudent("Oscar");
        university.addObserver(director);
        university.addStudent("Anna");
        university.removeStudent("Oscar");
    }
}

Strategy

Description: Defines a set of algorithms and allows them to interact. Allows for strategies (algorithms) to be changed at run time. Implementation:
interface Strategy {
    void download(String file);
}
class WindowsDownloadStrategy implements Strategy {
    public void download(String file) {
        System.out.println("windows download: " + file);
    }
}
class LinuxDownloadStrategy implements Strategy {
    public void download(String file) {
        System.out.println("linux download: " + file);
    }
}
class Context {
    private Strategy strategy;
    public Context(Strategy strategy){
        this.strategy = strategy;
    }
    public void download(String file){
        strategy.download(file);
    }
}

public class StrategyTest { // Test
    public static void main(String[] args) {
        Context context = new Context(new WindowsDownloadStrategy());
        context.download("file.txt");
        context = new Context(new LinuxDownloadStrategy());
        context.download("file.txt");
    }
}

Command

Description: Allows you to encapsulate various operations in individual objects. Implementation:
interface Command {
    void execute();
}
class Car {
    public void startEngine() {
        System.out.println("Start engine");
    }
    public void stopEngine() {
        System.out.println ("Stop engine");
    }
}
class StartCar implements Command {
    Car car;
    public StartCar(Car car) {
        this.car = car;
    }
    public void execute() {
        car.startEngine();
    }
}
class StopCar implements Command {
    Car car;
    public StopCar(Car car) {
        this.car = car;
    }
    public void execute() {
        car.stopEngine();
    }
}
class CarInvoker {
    public Command command;
    public CarInvoker(Command command){
        this.command = command;
    }
    public void execute(){
        this.command.execute();
    }
}

public class CommandTest { // Test
    public static void main(String[] args) {
        Car car = new Car();
        StartCar startCar = new StartCar(car);
        StopCar stopCar = new StopCar(car);
        CarInvoker carInvoker = new CarInvoker(startCar);
        carInvoker.execute();
    }
}

State

Description: Allows an object to change its behavior depending on its state. Implementation:
interface State {
    void doAction();
}
class StartPlay implements State {
    public void doAction() {
        System.out.println("start play");
    }
}
class StopPlay implements State {
    public void doAction() {
        System.out.println("stop play");
    }
}
class PlayContext implements State {
    private State state;
    public void setState(State state){
        this.state = state;
    }
    public void doAction() {
        this.state.doAction();
    }
}

public class StateTest { // Test
    public static void main(String[] args) {
        PlayContext playContext = new PlayContext();
        State startPlay = new StartPlay();
        State stopPlay = new StopPlay();
        playContext.setState(startPlay);
        playContext.doAction();
        playContext.setState(stopPlay);
        playContext.doAction();
    }
}

Visitor

Description: Used to simplify operations on groups of related objects. Implementation:
interface Visitor {
    void visit(SportsCar sportsCar);
    void visit(Engine engine);
    void visit(Wheel wheel);
}
interface Car {
    void accept(Visitor visitor);
}
class Engine implements Car {
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
class Wheel implements Car {
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
class SportsCar implements Car {
    Car[] cars;
    public SportsCar(){
        cars = new Car[]{new Engine(), new Wheel()};
    }
    public void accept(Visitor visitor) {
        for (int i = 0; i < cars.length; i++) {
            cars[i].accept(visitor);
        }
        visitor.visit(this);
    }
}
class CarVisitor implements Visitor {
    public void visit(SportsCar computer) {
        print("car");
    }
    public void visit(Engine engine) {
        print("engine");
    }
    public void visit(Wheel wheel) {
        print("wheel");
    }
    private void print(String string) {
        System.out.println(string);
    }
}

public class VisitorTest { // Test
    public static void main(String[] args) {
        Car computer = new SportsCar();
        computer.accept(new CarVisitor());
    }
}

Interpreter

Description: Allows you to define the grammar of a simple language in the problem domain. Implementation:
interface Expression {
    String interpret(Context context);
}
class Context {
     public String getLowerCase(String s){
        return s.toLowerCase();
    }
    public String getUpperCase(String s){
        return s.toUpperCase();
    }
}
 class LowerExpression implements Expression {
    private String s;
     public LowerExpression(String s) {
        this.s = s;
    }
    public String interpret(Context context) {
        return context.getLowerCase(s);
    }
}
class UpperExpression implements Expression {
    private String s;
    public UpperExpression(String s) {
        this.s = s;
    }
    public String interpret(Context context) {
        return context.getUpperCase(s);
    }
}

public class InterpreterTest { // Test
    public static void main(String[] args) {
        String str = "TesT";
        Context context = new Context();
         Expression lowerExpression = new LowerExpression(str);
         str = lowerExpression.interpret(context);
        System.out.println(str);
        Expression upperExpression = new UpperExpression(str);
        str = upperExpression.interpret(context);
        System.out.println(str);
    }
}

Iterator

Description: An iterator sequentially accesses elements of a collection without knowing its underlying form. Implementation:
interface Iterator {
    boolean hasNext();
    Object next();
}
class Numbers {
    public int num[] = {1 , 2, 3};
    public Iterator getIterator() {
        return new NumbersIterator();
    }
    private class NumbersIterator implements Iterator {
        int ind;
        public boolean hasNext() {
            if(ind < num.length) return true;
            return false;
        }
        public Object next() {
            if(this.hasNext()) return num[ind++];
            return null;
        }
    }
}

public class IteratorTest { // Test
    public static void main(String[] args) {
        Numbers numbers = new Numbers();
        Iterator iterator = numbers.getIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

Memento

Description: Allows you to save the current state of an object; this state can be restored later. Does not violate encapsulation. Implementation:
import java.util.ArrayList;
import java.util.List;
class Memento {
    private String name;
    private int age;
    public Memento(String name, int age){
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
}
class User {
    private String name;
    private int age;
    public User(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println(String.format("create: name = %s, age = %s", name, age));
    }
    public Memento save(){
        System.out.println(String.format("save: name = %s, age = %s", name, age));
        return new Memento(name, age);
    }
    public void restore(Memento memento){
        name = memento.getName();
        age = memento.getAge();
        System.out.println(String.format("restore: name = %s, age = %s", name, age));
    }
}
class SaveUser {
    private List<Memento> list = new ArrayList<Memento>();
    public void add(Memento memento){
        list.add(memento);
    }
    public Memento get(int ind){
        return list.get(ind);
    }
}

public class MementoTest { // Test
    public static void main(String[] args) {
        SaveUser saveUser = new SaveUser();
        User user1 = new User("Peter", 17);
        User user2 = new User("Ian", 19);
        saveUser.add(user1.save());
        user1.restore(saveUser.get(0));
    }
}
Good luck programming!
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet