Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need Help with Design Pattern. The following code will not compile (there are external dependencies), but you do not have to build and run it.

Need Help with Design Pattern.

The following code will not compile (there are external dependencies), but you do not have to build and run it. Instead search for instances of various Go4 Design Patterns (point out the java file or code block indicating the pattern, explain why this code describes the pattern).

Identify one instance of a Behavioral pattern in any of the following code:

interface Order { void execute(); }

class DomesticEngineer implements Order { public void execute() { System.out.println("take out the trash"); } }

class Politician implements Order { public void execute() { System.out.println("take money from the rich, take votes from the poor"); } }

class Programmer implements Order { public void execute() { System.out.println("sell the bugs, charge extra for the fixes"); } }

public class OrderDemo { public static List produceRequests() { List queue = new ArrayList<>(); queue.add(new DomesticEngineer()); queue.add(new Politician()); queue.add(new Programmer()); return queue; }

public static void workOffRequests(List queue) { for (Object order : queue) { ((Order)order).execute(); } }

public static void main( String[] args ) { List queue = produceRequests(); workOffRequests(queue); } }

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// 1. Subsystem class PointCartesian { private double x, y; public PointCartesian(double x, double y ) { this.x = x; this.y = y; }

public void move( int x, int y ) { this.x += x; this.y += y; }

public String toString() { return "(" + x + "," + y + ")"; }

public double getX() { return x; }

public double getY() { return y; } }

// 1. Subsystem class PointPolar { private double radius, angle;

public PointPolar(double radius, double angle) { this.radius = radius; this.angle = angle; }

public void rotate(int angle) { this.angle += angle % 360; }

public String toString() { return "[" + radius + "@" + angle + "]"; } }

// 1. Desired interface: move(), rotate() class Point { // 2. Design a "wrapper" class private PointCartesian pointCartesian;

public Point(double x, double y) { pointCartesian = new PointCartesian(x, y); }

public String toString() { return pointCartesian.toString(); }

// 4. Wrapper maps public void move(int x, int y) { pointCartesian.move(x, y); }

public void rotate(int angle, Point o) { double x = pointCartesian.getX() - o.pointCartesian.getX(); double y = pointCartesian.getY() - o.pointCartesian.getY(); PointPolar pointPolar = new PointPolar(Math.sqrt(x * x + y * y),Math.atan2(y, x) * 180 / Math.PI); // 4. Wrapper maps pointPolar.rotate(angle); System.out.println(" PointPolar is " + pointPolar); String str = pointPolar.toString(); int i = str.indexOf('@'); double r = Double.parseDouble(str.substring(1, i)); double a = Double.parseDouble(str.substring(i + 1, str.length() - 1)); pointCartesian = new PointCartesian(r*Math.cos(a*Math.PI/180) + o.pointCartesian.getX(), r*Math.sin(a * Math.PI / 180) + o.pointCartesian.getY()); } }

class Line { private Point o, e; public Line(Point ori, Point end) { o = ori; e = end; }

public void move(int x, int y) { o.move(x, y); e.move(x, y); }

public void rotate(int angle) { e.rotate(angle, o); }

public String toString() { return "origin is " + o + ", end is " + e; } }

public class PointDemo { public static void main(String[] args) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("a.txt"))); while(bis.available()>0) { char c = (char)bis.read(); System.out.println("Char: "+c);; } Line lineA = new Line(new Point(2, 4), new Point(5, 7)); lineA.move(-2, -4); System.out.println( "after move: " + lineA ); lineA.rotate(45); System.out.println( "after rotate: " + lineA ); Line lineB = new Line( new Point(2, 1), new Point(2.866, 1.5)); lineB.rotate(30); System.out.println("30 degrees to 60 degrees: " + lineB); } }

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

class IntegerBox { private final List list = new ArrayList<>();

public void add(int in) { list.add(in); }

public List getData() { return list; } }

public class TraversalDemo { public static void main(String[] args) { IntegerBox box = new IntegerBox(); for (int i = 9; i > 0; --i) { box.add(i); } Collection integerList = box.getData(); for (Object anIntegerList : integerList) { System.out.print(anIntegerList + " "); } System.out.println(); integerList.clear(); integerList = box.getData(); System.out.println("size of data is: " + integerList.size()); } }

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class University { private static University instance = null; protected University() { // Exists only to defeat instantiation. } public static University getInstance() { if(instance == null) { instance = new University(); } return instance; } }

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class User { private final String firstName; // required private final String lastName; // required private final int age; // optional private final String phone; // optional private final String address; // optional

private User(UserCreator creater) { this.firstName = creater.firstName; this.lastName = creater.lastName; this.age = creater.age; this.phone = creater.phone; this.address = creater.address; }

public String getFirstName() { return firstName; }

public String getLastName() { return lastName; }

public int getAge() { return age; }

public String getPhone() { return phone; }

public String getAddress() { return address; }

public static class UserCreator { private final String firstName; private final String lastName; private int age; private String phone; private String address;

public UserCreator(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }

public UserBuilder age(int age) { this.age = age; return this; }

public UserBuilder phone(String phone) { this.phone = phone; return this; }

public UserBuilder address(String address) { this.address = address; return this; }

public User create() { return new User(this); }

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions

Question

Define and explain the nature of nonassociative learning.

Answered: 1 week ago