Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts
Prototype Design Pattern

Prototype Design Pattern

Name: Prototype Design Pattern

Type: Creational Design Pattern

Purpose:
  • Improving performance by cloning objects.
  • Minimize complexity in object creation.

Sample Problem and Solution:
Consider a problem where you need a 64x64 grid Board class to represent a chess board. A possible design is provided here.
public class Cell {
    private String color;

    public Cell(String color) {
        this.color = color;

        // Make it time consuming task.
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }

    public String getColor() {
        return color;
    }

    @Override
    public String toString() {
        return color.substring(0, 1);
    }
}
In this code, Thread.sleep is used to make the object creation as a time consuming operation.
public class Board {
    public static final int NO_OF_ROWS = 8;
    public static final int NO_OF_COLUMNS = 8;

    private final Cell[][] board;

    public Board() {
        this.board = new Cell[NO_OF_ROWS][NO_OF_COLUMNS];

        for (int row = NO_OF_ROWS - 1; row >= 0; row--) {
            for (int col = NO_OF_COLUMNS - 1; col >= 0; col--) {
                if ((row + col) % 2 == 0) {
                    board[row][col] = new Cell("WHITE");
                } else {
                    board[row][col] = new Cell("BLACK");
                }
            }
        }
    }

    public void print() {
        for (int row = 0; row < NO_OF_ROWS; row++) {
            for (int col = 0; col < NO_OF_COLUMNS; col++) {
                System.out.print(board[row][col] + " ");
            }
            System.out.println();
        }
    }
}
public class Main {

    public static void main(String[] args) {
        // Get the start time
        long startTime = System.currentTimeMillis();

        Board chessBoard = new Board();

        // Get the end time
        long endTime = System.currentTimeMillis();

        System.out.println("Time taken to create a board: " + (endTime - startTime) + " millis");

        // Print the board
        chessBoard.print();
    }

}

Read More
Bridge Design Pattern

Bridge Design Pattern

Name: Bridge Design Pattern

Type: Structural Design Pattern

Purpose: Decouple abstraction from implementation
                (In other words, using composite over inheritance)

Sample Problem and Solution:
Assume that you are required to create some Shapes with two different patterns. Some sample shapes are SolidColorCircle, GradientCircle, SolidColorSquare, and GradientSquare. A possible design is provided below.
public interface Shape {
    public void draw();
}
public abstract class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public void draw() {
        System.out.println("Create a circle with radius: " + radius);
    }
}
public class SolidColorCircle extends Circle {
    private String color;

    public SolidColorCircle(double radius, String color) {
        super(radius);
        this.color = color;
    }

    @Override
    public void draw() {
        super.draw();
        System.out.println("Fill with: " + color);
    }
}
public class GradientCircle extends Circle {
    private String colorX;
    private String colorY;

    public SolidColorCircle(double radius, String colorX, String colorY) {
        super(radius);
        this.colorX = colorX;
        this.colorY = colorY;
    }

    @Override
    public void draw() {
        super.draw();
        System.out.println("Fill with: " + colorX + " & " + colorY);
    }
}
Square, SolidColorSquare, and GradientSquare are provided in GitHub.
Read More
Factory Design Pattern

Factory Design Pattern

Name: Factory Design Pattern

Type: Creational Design Pattern

Purpose:
  • Hide object creation logic
  • Decouple classes from their clients

Sample Problem and Solution:
Consider a situation where you are creating a library which provides some 2D Shapes. Other developers will use your library to create some shapes with random measurements. A naive way of designing the Shape classes is provided here.
package com.javahelps.shapes;

public interface Shape {
    public void draw();
}
package com.javahelps.shapes;

public class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public void draw() {
        System.out.println("Circle");
    }
}
package com.javahelps.shapes;

public class Square implements Shape {
    private int width;

    public Square(int width) {
        this.width = width;
    }

    @Override
    public void draw() {
        System.out.println("Square");
    }
}
package com.javahelps.shapes;

public class Rectangle implements Shape {
    private int width;
    private int height;

    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public void draw() {
        System.out.println("Rectangle");
    }
}
Read More

Contact Form

Name

Email *

Message *