Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”

Similar presentations


Presentation on theme: "Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”"— Presentation transcript:

1 Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”

2 Aalborg Media Lab 28-Jun-15 Object Oriented Design Chapter 6 –Relationships among classes –The static modifier –Formal object interfaces

3 Aalborg Media Lab 28-Jun-153 6.1 Program Development Activities The creation of software involves four basic activities: –establishing the requirements –creating a design –implementing the code –testing the implementation

4 Aalborg Media Lab 28-Jun-154 Requirements Software requirements specify the tasks a program must accomplish (what to do, not how to do it) They often include a description of the user interface An initial set of requirements often are provided, but usually must be critiqued, modified, and expanded Often it is difficult to establish detailed, unambiguous, complete requirements Careful attention to the requirements can save significant time and expense in the overall project

5 Aalborg Media Lab 28-Jun-155 Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal An algorithm may be expressed in pseudo code, which is code-like, but does not necessarily follow any specific syntax In object-oriented development, the design establishes the classes, objects, methods, and data that are required

6 Aalborg Media Lab 28-Jun-156 Implementation Implementation is the process of translating a design into source code Most novice programmers think that writing code is the heart of software development, but actually it should be the least creative step Almost all important decisions are made during requirements and design stages Implementation should focus on coding details, including style guidelines and documentation

7 Aalborg Media Lab 28-Jun-157 Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process of discovering the causes of problems and fixing them Programmers often think erroneously that there is "only one more bug" to fix Tests should consider design details as well as overall requirements

8 Aalborg Media Lab 28-Jun-15 6.2Identifying Objects/Classes Determining the classes contributing to the program is the fundamental part of OOP. Objects are generally nouns in the problem specification Similar objects  class Then specify class behavior and features

9 Aalborg Media Lab 28-Jun-159 6.3The static Modifier For example, the methods of the Math class are static: Math.sqrt (25) To write a static method, we apply the static modifier to the method definition The static modifier can be applied to variables as well It associates a variable or method with the class rather than with an object

10 Aalborg Media Lab 28-Jun-1510 Static Variables Static variables are also called class variables Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created when the class in which it is declared is loaded All objects created from the class share static variables Changing the value of a static variable in one object changes it for all others

11 Aalborg Media Lab 28-Jun-1511 Static Methods public static int triple (int num) { int result; result = num * 3; return result; } class Helper Because it is static, the method can be invoked as: value = Helper.triple (5);

12 Aalborg Media Lab 28-Jun-1512 Static Methods The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static; it is invoked by the system without creating an object Static methods cannot reference instance variables, because instance variables don't exist until an object exists However, a static method can reference static variables or local variables

13 Aalborg Media Lab 28-Jun-15 6.4Class Relationships “uses-a” relationship (one object uses methods of another) “has-a” relationship (one object contain another) “is-a” relationship (inheritance)

14 Aalborg Media Lab 28-Jun-15 UML Diagrams UML diagrams show relationships among classes and objects A UML class diagram consists of one or more classes, each with sections for the class name, attributes, and methods Lines between classes represent associations Associations can show multiplicity

15 Aalborg Media Lab 28-Jun-15 UML Class Diagrams A UML class diagram for the Rolling Dices program: RolingDices main (args : String[]) : void Die faceValue : int roll() : void getFaceValue() : boolean toString() : String 12

16 Aalborg Media Lab 28-Jun-15 UML Diagrams A UML object diagram consists of one or more instantiated objects. It is a snapshot of the objects during an executing program, showing data values die1 : Die faceValue = 5 die2 : Die Value = 1

17 Aalborg Media Lab 28-Jun-15 Use Relationship A general association, as we've seen in UML diagrams, is sometimes referred to as a use relationship A general association indicates that one object (or class) uses or refers to another object (or class) in some way We could even annotate an association line in a UML diagram to indicate the nature of the relationship Author Book writes

18 Aalborg Media Lab 28-Jun-15 Aggregation An aggregate object is an object that contains references to other objects For example, an Account object contains a reference to a String object (the owner's name) An aggregate object represents a has-a relationship A bank account has a name Likewise, a student may have one or more addresses

19 Aalborg Media Lab 28-Jun-15 Aggregation in UML An aggregation association is shown in a UML class diagram using an open diamond at the aggregate end StudentBody + main (args : String[]) : void + toString() : String 12 Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address + toString() : String - streetAddress : String - city : String - state : String - zipCode : long Address

20 Aalborg Media Lab 28-Jun-15 6.5Interfaces A Java interface is a collection of abstract methods and constants An abstract method is a method header without a method body An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off An interface is used to establish, as a formal contract, a set of methods that a class will implement

21 Aalborg Media Lab 28-Jun-15 Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } interface is a reserved word None of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

22 Aalborg Media Lab 28-Jun-15 Interfaces An interface cannot be instantiated Methods in an interface have public visibility by default A class formally implements an interface by –stating so in the class header –providing implementations for each abstract method in the interface If a class asserts that it implements an interface, it must define all methods in the interface

23 Aalborg Media Lab 28-Jun-15 Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition

24 Aalborg Media Lab 28-Jun-15 Interfaces A class that implements an interface can implement other methods as well In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all its constants

25 Aalborg Media Lab 28-Jun-15 Interfaces A class can implement multiple interfaces, these are listed in the implements clause The class must implement all methods in all interfaces listed in the header class ManyThings implements interface1, interface2 { // all methods of both interfaces }

26 Aalborg Media Lab 28-Jun-15 Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains an abstract method called compareTo, which is used to compare two objects The String class implements Comparable, giving us the ability to put strings in lexicographic order The Iterator interface contains methods that allow the user to move easily through a collection of objects

27 Aalborg Media Lab 28-Jun-15 Why use interfaces? When the user presses a button, the Java run-time system sends an actionPerformed() message to any object that is listening for the button’s events. If we want our listener to listen, it must listen for that particular message. When the user adjusts a slider, the Java run-time system sends an adjustmentValueChanged() message to any object that is listening for the slider’s events. If we want our listener to listen, it must listen for that particular message.

28 Aalborg Media Lab 28-Jun-15 More generally, why do we use interfaces? To allow our objects to work inside an existing framework. To allow programmers to create objects that fulfill responsibilities — without committing to how their objects do so! Sometimes called a protocol.

29 Aalborg Media Lab 28-Jun-15 6.7 Method Design A method should be relatively small, so that it can be understood as a single entity A potentially large method should be decomposed into several smaller methods as needed for clarity A service method of an object may call one or more support methods to accomplish its goal Support methods could call other support methods if appropriate

30 Aalborg Media Lab 28-Jun-1530 6.8 Overloading Methods Method overloading is the process of using the same method name for multiple methods The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters The compiler determines which version of the method is being invoked by analyzing the parameters The return type of the method is not part of the signature

31 Aalborg Media Lab 28-Jun-15 Overloading Methods float tryMe (int x) { return x +.375; } Version 1 float tryMe (int x, float y) { return x*y; } Version 2 result = tryMe (25, 4.32) Invocation

32 Aalborg Media Lab 28-Jun-1532 Overloaded Methods The println method is overloaded: println(String s),println(int i) … The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total);

33 Aalborg Media Lab 28-Jun-1533 Overloading Methods Constructors can be overloaded Overloaded constructors provide multiple ways to initialize a new object

34 Aalborg Media Lab 28-Jun-15 Testing Testing is no guarantee for the absence of errors Reviews –Presenting designs to each other in project-group Defect Testing –Test cases (how does the user normally use the program?) –White-Box vs. Black-Box Testing Do it, but with a concept!!!

35 Aalborg Media Lab 28-Jun-15 GUI Design The GUI designer should: –Know the users and their needs –Prevent user errors whenever possible –Optimize user abilities and make information readily available –Be consistent with placement of components and color schemes

36 Aalborg Media Lab 28-Jun-15 Layout Managers A layout manager is an object that determines the manner in which components are arranged in a container There are several predefined layout managers defined in the Java standard class library: Defined in the AWT Defined in Swing Flow Layout Border Layout Card Layout Grid Layout GridBag Layout Box Layout Overlay Layout

37 Aalborg Media Lab 28-Jun-15 Layout Managers Every container has a default layout manager, but we can explicitly set the layout manager as well Each layout manager has its own particular rules governing how the components will be arranged Some layout managers pay attention to a component's preferred size or alignment, while others do not A layout manager attempts to adjust the layout as components are added and as containers are resized

38 Aalborg Media Lab 28-Jun-15 Layout Managers We can use the setLayout method of a container to change its layout manager JPanel panel = new JPanel(); panel.setLayout (new BorderLayout()); The following example uses a tabbed pane, a container which permits one of several panes to be selected

39 Aalborg Media Lab 28-Jun-15 Flow Layout Flow layout puts as many components as possible on a row, and then moves to the next row Rows are created as needed to accommodate all of the components Components are displayed in the order they are added to the container Each row of components is centered horizontally in the window by default, but could also be aligned left or right The horizontal and vertical gaps between the components can be explicitly set also

40 Aalborg Media Lab 28-Jun-15 Demo – Flow Layout

41 Aalborg Media Lab 28-Jun-15 Border Layout A border layout defines five areas to which components can be added North South CenterEastWest

42 Aalborg Media Lab 28-Jun-15 Border Layout Each area displays one component (which could be another container such as a JPanel ) Each of the four outer areas enlarges as needed to accommodate the component added to it If nothing is added to the outer areas, they take up no space and other areas expand to fill the void The center area expands to fill space as needed

43 Aalborg Media Lab 28-Jun-15 LayoutDemo - Border

44 Aalborg Media Lab 28-Jun-15 Containment Hierarchies

45 Aalborg Media Lab 28-Jun-15 Exercises Identify Objects including behavior and features 6.3, 6.14


Download ppt "Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”"

Similar presentations


Ads by Google