0% found this document useful (0 votes)
18 views4 pages

Design Patterns Practice

We thank you for your time spent taking this survey. Your response has been recorded.

Uploaded by

Jenny Liu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Design Patterns Practice

We thank you for your time spent taking this survey. Your response has been recorded.

Uploaded by

Jenny Liu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

We thank you for your time spent taking this survey.

Your response has been recorded.

Below is a summary of your responses Download PDF

Welcome to the Design Patterns study form!

Tell us your CSID so you can get credit

Name Jenny Liu

CSID j7h2b

CWL liujenn

Student Numbere 95901757

What's your goal in submitting this form:

just doing it for credit, not really looking at the questions right now (by selecting this, you will get credit, but you will not
see the questions -- come back later for real practice!)
actually practicing

looking at the questions, but not entering anything for real right now

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) {
[Link]("Line from point A(" + x1 + ";" + y1 + "), to point B(" + x2 +
";" + y2 + ")");
}
}

class Rectangle {
public void draw(int x, int y, int width, int height) {
[Link]("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 ([Link]().getSimpleName().equals("Line")) {
((Line)shape).draw(x1, y1, x2, y2);
} else if ([Link]().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
State

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

2
3

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) {


if ([Link]().getSimpleName().equals("Line")) {

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


} else if ([Link]().getSimpleName().equals("Rectangle"))

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

Powered by Qualtrics A

You might also like