Design Patterns Practice Sols

Which of the following applies to which pattern:

I have noticed that I have a lot of construction code in one of my classes, and I have noticed that I am using an if-statement to check the configuration for the construction. Which pattern is right for me:

  • Adapter

  • Composite

  • Factory

  • Observer

  • Singleton

  • State


I have noticed that I have a lot of code in one class that checks the status of another object, watching it for updates, and then handling the updates. Which pattern is right for me:

  • Adapter

  • Composite

  • Factory

  • Observer

  • Singleton

  • State


I am writing a client that needs to be able to switch between algorithms. Which pattern is right for me:

  • Adapter

  • Composite

  • Factory

  • Observer

  • Singleton

  • State


Consider the following code:

class Line {

public void draw(int x1, int y1, int x2, int y2) {

System.out.println("Line from point A(" + x1 + ";" + y1 + "), to point B(" + x2 + ";" + y2 + ")");

}

}


class Rectangle {

public void draw(int x, int y, int width, int height) {

System.out.println("Rectangle with coordinate left-down point (" + x + ";" + y + "), width: " + width

+ ", height: " + height);

}

}


public class PatternDemo {

public static void main(String[] args) {

Object[] shapes = {new Line(), new Rectangle()};

int x1 = 10, y1 = 20;

int x2 = 30, y2 = 60;

int width = 40, height = 40;

for (Object shape : shapes) {

if (shape.getClass().getSimpleName().equals("Line")) {

((Line)shape).draw(x1, y1, x2, y2);

} else if (shape.getClass().getSimpleName().equals("Rectangle")) {

((Rectangle)shape).draw(x2, y2, width, height);

}

}

}

}


Which pattern do you think might be helpful to solve the if-type code smell:

  • Adapter

  • Composite

  • Factory

  • Observer

  • Singleton

  • Strategy


What new abstractions and concretizations would you introduce to fix the problem and apply the pattern?

Make a new interface (call it “NewInterface”)

Make one new class (NewClassOne) to implement NewInterface

Make two new classes (NewClassOne and NewClassTwo) to implement NewInterface


How many methods would be specified by you in the NewInterface

  • 1

  • 2

  • 3

  • 4


Which classes would implement NewInterface

  • NewClassOne

  • NewClassTwo (if it exists)

  • PatternDemo

  • Rectangle

  • Line


Which class would have an association with Rectangle, if any?

  • NewInterface

  • NewClassOne

  • NewClassTwo (if it exists)

  • PatternDemo

  • Line


Which class would have an association with Line, if any?

  • NewInterface

  • NewClassOne

  • NewClassTwo (if it exists) (or the reverse -- NewClass1 relates to line, NC2 relates to Rectangle)

  • PatternDemo

  • Rectangle


Which class would have an association with NewClassOne and NewClassTwo (if it exists)

  • NewClassOne

  • NewClassTwo (if it exists)

  • PatternDemo (actually -- PatternDemo would have an association to NewInterface)

  • Rectangle

  • Line


Which lines of code in the client will need to change:

Object[] shapes = {new Line(), new Rectangle()};

int x1 = 10, y1 = 20;

int x2 = 30, y2 = 60;

int width = 40, height = 40;

for (Object shape : shapes) { //type needs to change

if (shape.getClass().getSimpleName().equals("Line")) {

((Line)shape).draw(x1, y1, x2, y2);

} else if (shape.getClass().getSimpleName().equals("Rectangle"))

((Rectangle)shape).draw(x2, y2, width, height);



Entire solution to Long Question 1 available here: https://sourcemaking.com/design_patterns/adapter/java/1