1 / 22

Object-Oriented Design

Object-Oriented Design. Object : An encapsulation of data with the methods that operate on that data. Compare: function-oriented vs object-oriented . get-name(student-number) student.getName(). Budd’s Example: Ordering Flowers.

doria
Download Presentation

Object-Oriented Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Design Object: An encapsulation of data with the methods that operate on that data. Compare: function-oriented vs object-oriented. get-name(student-number) student.getName() CSE 341 -- S. Tanimoto Java Objects

  2. Budd’s Example: Ordering Flowers Budd orders flowers for Sally by sending a message to Flora. Budd and Flora are members of a community of agents. Sally Budd Gardener Flower arranger Flora Delivery person Grower Wholesaler Sally’s florist CSE 341 -- S. Tanimoto Java Objects

  3. Characteristics of an Object Encapulates data and methods. Acts like an agent. Has “responsibility” delegated to it by the designer Is an instance of a class. Its public members (data and/or methods) are accessible outside of its class. Its private members are accessible only within methods of its class, permitting information hiding) Goes into action when “sent” a “message” (i.e., when one of its member functions is called or “invoked”). CSE 341 -- S. Tanimoto Java Objects

  4. Designing a System Identify the component parts of the system Associate data and functionality with each part Identify the communication links among the components CSE 341 -- S. Tanimoto Java Objects

  5. Designing an Object Identify its responsibility. (“What” not “how”) Identify the data it needs. Separate out those services it needs and can delegate to other objects. CSE 341 -- S. Tanimoto Java Objects

  6. Implementing an Object Name its class. Create prototypes for its public methods (to be compatible with the calls from other objects). Design the internal data structures and create private variables for them. Implement the public and any private methods making calls to other objects as needed. CSE 341 -- S. Tanimoto Java Objects

  7. Example: A Program for Displaying Random Line Segments and Their Intersections Specs: 1. The program should randomly create a set of 10 line segments. 2. The segments should be displayed on a canvas. 3. Each intersection should be shown with a bold dot. CSE 341 -- S. Tanimoto Java Objects

  8. Sample Desired Result CSE 341 -- S. Tanimoto Java Objects

  9. Our Key Objects: Line Segments Responsibilities: 1. Display themselves. 2. Find their intersections with others. 3. Create themselves. Delegated services: 1. Create random point 2. Get a list of all the other line segments CSE 341 -- S. Tanimoto Java Objects

  10. Other Objects Points: 1. Create point, create random point. Set of line segments: 1. Create 10 line segments 2. Make a list of all the line segments other than a given one. 3. Display the set. CSE 341 -- S. Tanimoto Java Objects

  11. Data Needed by LineSegment Pointsp1, p2 (endpoints of the segment) Integer id We declare that it is the responsibility of the higher-numbered line segment to determine any intersection between it and another segment. Vector intersections This is a list of intersections for which it is responsible. CSE 341 -- S. Tanimoto Java Objects

  12. LineSegment.java import java.awt.*; import java.util.*; public class LineSegment { LSPoint p1, p2; int id; LineSegmentCollection collection; Vector intersections; public LSPoint getP1() { return p1; } public LSPoint getP2() { return p2; } public LineSegment(int anID, LineSegmentCollection theCollection){...} public void paint(Graphics g) {...} private void computeIntersections() {...} private LSPoint segIntersection(LineSegmentls){...} private void plotIntersections(Graphics g) {...} } CSE 341 -- S. Tanimoto Java Objects

  13. In LineSegment.java -- Constructor public LineSegment(int anID, LineSegmentCollection theCollection){ id = anID; collection = theCollection; p1 = new LSPoint(); p2 = new LSPoint(); computeIntersections(); } CSE 341 -- S. Tanimoto Java Objects

  14. In LineSegment.java -- paint public void paint(Graphics g) { g.drawLine(p1.getX(), p1.getY(), p2.getX(), p2.getY()); plotIntersections(g); } CSE 341 -- S. Tanimoto Java Objects

  15. LineSegment: computeIntersections private void computeIntersections() { intersections = new Vector(); for (Enumeration segments = (collection.otherSegments(this)).elements(); segments.hasMoreElements();) { LineSegment aLineSeg = (LineSegment) segments.nextElement(); LSPoint p = segIntersection(aLineSeg); if (p != null) intersections.addElement(p); } } CSE 341 -- S. Tanimoto Java Objects

  16. LineSegment: segIntersection private LSPoint segIntersection(LineSegment ls) { LSPoint p3 = ls.getP1(); LSPoint p4 = ls.getP2(); int x1 = p1.getX(); int y1 = p1.getY(); int x2 = p2.getX(); int y2 = p2.getY(); int x3 = p3.getX(); int y3 = p3.getY(); int x4 = p4.getX(); int y4 = p4.getY(); double d1 = (x1 * y2) - (x2 * y1); double d2 = (x3 * y4) - (x4 * y3); double d3 = ((x1 - x2) * (y3 - y4)) - ((x3 - x4) * (y1 - y2)); int x = (int) (((d1 * (x3 - x4)) - (d2 * (x1 - x2))) / d3); int y = (int) (((d1 * (y3 - y4)) - (d2 * (y1 - y2))) / d3); CSE 341 -- S. Tanimoto Java Objects

  17. LineSegment: segIntersection (cont) int minx1 = Math.min(x1, x2); if (x < minx1) return null; int maxx1 = Math.max(x1, x2); if (x > maxx1) return null; int miny1 = Math.min(y1, y2); if (y < miny1) return null; int maxy1 = Math.max(y1, y2); if (y > maxy1) return null; int minx2 = Math.min(x3, x4); if (x < minx2) return null; int maxx2 = Math.max(x3, x4); if (x > maxx2) return null; int miny2 = Math.min(y3, y4); if (y < miny2) return null; int maxy2 = Math.max(y3, y4); if (y > maxy2) return null; return new LSPoint(x, y); } CSE 341 -- S. Tanimoto Java Objects

  18. LineSegment: plotIntersections private void plotIntersections(Graphics g) { for (Enumeration intPoints = intersections.elements(); intPoints.hasMoreElements();) { LSPoint p = (LSPoint) intPoints.nextElement(); int x = p.getX(); int y = p.getY(); int radius = 5; g.fillOval(x-radius, y-radius, 2*radius, 2*radius); } } CSE 341 -- S. Tanimoto Java Objects

  19. The Class LSPoint import java.util.Random; public class LSPoint { int x, y; static Random rg = new Random(); public LSPoint() { x = Math.abs(rg.nextInt()) % 200; y = Math.abs(rg.nextInt()) % 200; } public LSPoint(int xVal, int yVal) { x = xVal; y = yVal; } public int getX() { return x; } public int getY() { return y; } } CSE 341 -- S. Tanimoto Java Objects

  20. The Class LineSegmentCollection import java.applet.*; import java.awt.*; import java.util.*; public class LineSegmentCollection extends Applet { Vector theSegments; public void init() { theSegments = new Vector(); for (int i = 0; i < 10; i++) { theSegments.addElement(new LineSegment(i, this)); } } CSE 341 -- S. Tanimoto Java Objects

  21. LineSegmentCollection (cont) public void paint(Graphics g) { setBackground(Color.red); g.setColor(Color.black); for (Enumeration theEnum = theSegments.elements(); theEnum.hasMoreElements();) { ((LineSegment)theEnum.nextElement()).paint(g); } } public Vector otherSegments(LineSegment oneSegment) { Vector copyOfList = (Vector)(theSegments.clone()); copyOfList.removeElement((Object)oneSegment); return copyOfList; } } CSE 341 -- S. Tanimoto Java Objects

  22. Summary of Object-Oriented Design Express the system as a collection of interacting agents (“objects”) Identify data and functionality for each kind of object Identify communication links among types of objects Specify the interfaces for each type of object Implement object behavior in classes and methods CSE 341 -- S. Tanimoto Java Objects

More Related