Download presentation
Presentation is loading. Please wait.
Published byBenedict Grant Modified over 8 years ago
1
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative Development Part III Elaboration Iteration I – Basic 3
2
Software Engineering 2 Object-oriented Analysis and Design Chap 19 Designing for Visibility
3
Software Engineering 3 Object-oriented Analysis and Design Visibility Between Objects 1 System operations illustrate messages between objects. m A sender object send a message to a receiver object, the sender must be visible to the receiver. m Sender must have some kind of reference or pointer to the receiver object. m e.g., the getProductDescription message sent from a Register to a ProductCatalog implies that the ProductCatalog instance is visible to the Register instance.
4
Software Engineering 4 Object-oriented Analysis and Design Visibility from the Register to ProductCatalog is required Visibility Between Objects 2
5
Software Engineering 5 Object-oriented Analysis and Design Visibility 1 Visibility Visibility m is the ability of an object to "see" or have a reference to another object. m One instance is within the scope of another. Visibility types m Attribute visibility: B is an attribute of A. m Parameter visibility: B is a parameter of a method of A. m Local visibility: B is a (non-parameter) local object in a method of A. m Global visibility: B is in some way globally visible.
6
Software Engineering 6 Object-oriented Analysis and Design Visibility 2 Attribute visibility B is an attribute of A. m It is a relatively permanent visibility because it persists as long as A and B exist. m A Register instance may have attribute visibility to a ProductCatalog, since it is an attribute of the Register. :Register enterItem (itemID, quantity) :ProductCatalog public void enterItem(itemIDqty) { …. desc=catalog.getProductDes(itemID) …. } class Register {... private ProductCatalog catalog;... } desc=catalog.getProductDes(itemID)
7
Software Engineering 7 Object-oriented Analysis and Design Visibility 3 Parameter Visibility from A to B m when B is passed as a parameter to a method of A. m It is a relatively temporary visibility because it persists only within the scope of the method. m when the makeLineItem message is sent to a Sale instance, a ProductDescription instance is passed as a parameter. 2:makeLineItem(desc,qty) enterItem(id,qty) 1:desc=getProductDesc(id) 2.1:create(desc,qty) :Register:Sale :Product Catalog sl:SalesLineItem makeLineItem(ProductDescription desc,int qty) {... sl=new SalesLineItem(desc,qty);... }
8
Software Engineering 8 Object-oriented Analysis and Design Visibility 4 m transform parameter visibility into attribute visibility. When the Sale creates a new SalesLineItem, it passes the ProductDescription in to its constructor.constructor the parameter is assigned to an attribute, thus establishing attribute visibility. 2:makeLineItem(desc,qty) enterItem(id,qty) 2:desc=getProductDesc(id) 2.1:create(desc,qty) :Register:Sale :Product Catalog sl:SalesLineItem //initializing method(e.g.,a Java constructor) SalesLineItem(ProductDescription desc,int qty) {... description=desc;//parameter to attribute visibility... }
9
Software Engineering 9 Object-oriented Analysis and Design Visibility 5 Local Visibility from A to B exists m when B is declared as a local object within a method of A. m It is a relatively temporary visibility. m Two means by which local visibility is achieved Create a new local instance and assign it to a local variable. Assign the returning object from a method invocation to a local variable (enterItem method of class Register). // there is implicit local visibility to the foo object // returned via the getFoo call anObject.getFoo().doBar(); To transform locally declared visibility into attribute visibility.
10
Software Engineering 10 Object-oriented Analysis and Design Visibility 5
11
Software Engineering 11 Object-oriented Analysis and Design Visibility 6 Global Visibility from A to B exists m when B is global to A. m It is a relatively permanent visibility To achieve global visibility m to assign an instance to a global variable, such as C++.
12
Software Engineering 12 Object-oriented Analysis and Design Chap 20 Mapping Designs to Code
13
Software Engineering 13 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring
14
Software Engineering 14 Object-oriented Analysis and Design Test-Driven Development 1 The test is written first, imagining the code to be tested is written. Advantages include m The unit tests actually get written m Programmer satisfaction leading to more consistent test The psychological aspects of development can't be ignored programming is a human endeavor. m Clarification of detailed interface and behavior That reflection improves or clarifies the detailed design. m Provable, repeatable, automated verification m The confidence to change things When a developer needs to change existing code written by themselves, there is a unit test suite that can be run, providing immediate feedback if the change caused an error.
15
Software Engineering 15 Object-oriented Analysis and Design Test-Driven Development 2 The popular unit testing framework is the xUnit family m For Java, the popular version is JUnit. NUnit for.NET m JUnit is integrated into most of the Java IDEs, such as Eclipse. Example fo Sale class, write a unit testing method in a SaleTest class m Create a Salethe thing to be tested (also known as the fixture). m Add some line items to it with the makeLineItem method (the makeLineItem method is the public method we wish to test). m Ask for the total, and verify that it is the expected value, using the assertTrue method. JUnit will indicate a failure if any assertTrue statement does not evaluate to true. Each testing method follows this pattern: m Create the fixture. m Do something to it (some operation that you want to test). m Evaluate that the results are as expected.
16
Software Engineering 16 Object-oriented Analysis and Design Test-Driven Development 3 To use JUnit, to create a test class that extends the JUnit TestCase class; the test class inherits various unit testing behaviors. m create a separate testing method for each Sale method. m write unit testing methods for each public method of the Sale class. To test method doFoo, it is an idiom to name the testing method testDoFoo. public class SaleTest extends TestCase { // … // test the Sale.makeLineItem method public void testMakeLineItem() { // STEP 1: CREATE THE FIXTURE // -this is the object to test // -it is an idiom to name it 'fixture' // -it is often defined as an instance field rather than // a local variable Sale fixture = new Sale(); // set up supporting objects for the test Money total = new Money( 7.5 ); Money price = new Money( 2.5 );
17
Software Engineering 17 Object-oriented Analysis and Design Test-Driven Development 4 ItemID id = new ItemID( 1 ); ProductDescription desc = new ProductDescription( id, price, "product 1" ); // STEP 2: EXECUTE THE METHOD TO TEST // NOTE: We write this code **imagining** there // is a makeLineItem method. This act of imagination // as we write the test tends to improve or clarify // our understanding of the detailed interface to // to the object. Thus TDD has the side-benefit of // clarifying the detailed object design. // test makeLineItem sale.makeLineItem( desc, 1 ); sale.makeLineItem( desc, 2 ); // STEP 3: EVALUATE THE RESULTS // there could be many assertTrue statements // for a complex evaluation // verify the total is 7.5 assertTrue( sale.getTotal().equals( total )); }
18
Software Engineering 18 Object-oriented Analysis and Design Refactoring 1 Refactoring m is a structured, disciplined method to rewrite or restructure existing code without changing its external behavior, applying small transformation steps combined with re- executing tests each step. The essence of refactoring m applying small behavior preserving transformations (each called a 'refactoring'), one at a time. m After each transformation, the unit tests are re-executed to prove that the refactoring did not cause a regression (failure).
19
Software Engineering 19 Object-oriented Analysis and Design Refactoring 2 The activities and goals refactoring m remove duplicate code m improve clarity m make long methods shorter m remove the use of hard-coded literal constants Some code smells include m duplicated code m big method m class with many instance variables m class with lots of code m strikingly similar subclasses m little or no use of interfaces in the design m high coupling between many objects
20
Software Engineering 20 Object-oriented Analysis and Design In Java, for example, replace using the new operator and constructor call with invoking a helper method that creates the object (hiding the details). Replace Constructor Call with Factory Method Put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose. Introduce Explaining Variable (specialization of Extract Local Variable) Replace a literal constant with a constant variable.Extract Constant Transform a long method into a shorter one by factoring out a portion into a private helper method. Extract Method DescriptionRefactoring Refactoring 3 Refactorings example
21
Software Engineering 21 Object-oriented Analysis and Design Refactoring 4 Before introducing an explaining variable. // good method name, but the logic of the body is not clear boolean isLeapYear( int year ) { return( ( ( year % 400 ) == 0 ) || ( ( ( year % 4 ) == 0 ) && ( ( year % 100 ) != 0 ) ) ); } After introducing an explaining variable. // that's better! boolean isLeapYear( int year ) { boolean isFourthYear = ( ( year % 4 ) == 0 ); boolean isHundrethYear = ( ( year % 100 ) == 0); boolean is4HundrethYear = ( ( year % 400 ) == 0); return(is4HundrethYear||(isFourthYear&& !isHundrethYear)); } IDE Support for Refactoring: Eclipse IDE
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.