Reusability 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 1
Objectives Identify the benefits of building applications using frameworks. Describe how a framework differs from a design pattern. Use design patterns. Identify the four elements that define a pattern Use refactoring to improve code maintenance and reusability. List the benefits of refactoring Identify situations in which refactoring might cause problems Write a program that uses the following components: Component Object Model (COM), ActiveX control, .NET component, JavaBean, Web service Describe how you would use a COM object. Describe how you would use an ActiveX control. Describe how you would use a .NET component. Describe how you would use a JavaBean Describe how you woul use a Web service 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 2
Patterns A pattern name The problem the pattern solves Conditions that must be met for the pattern to be applicable The solution to the problem brought by the pattern Elements involved and their roles, responsibilities, relationships, and collaborations Not a concrete design or implementation The consequences of applying the pattern Time and space Language and implementation Effects on flexibility, extensibility, and portability 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 3
Observer Pattern 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 4
Frameworks Provide abstract classes and base classes Provide default implementations Apply multiple patterns Can be based on: White box (classes and inheritance) Black box (object composition) 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 5
MVC Framework 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 6
Java 2 Platform 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 7
Java, the Language 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 8
Java.awt.image Package 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 9
Interfaces versus Classes 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 10
JavaBeans Events Properties Introspection Customization Persistence 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 11
JAR Files May include: A set of class files A set of serialize objects Optional help files (HTML) Optional localization information Optional icons held in .icon files in GIF format Other resources needed by the bean 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 12
Basic Java Services Reflection Object serialization Java native interface Java AWT and JFC/Swing 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 13
COM Component with a Single Interface 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 14
COM Component with Multiple Interfaces 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 15
Depiction of a COM Object 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 16
COM Object Containment 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 17
COM Object Aggregation 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 18
ActiveX Controls COM objects, but with additional features Self-registering Handle events Expose design-time properties Support persistence 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 19
.NET Common Type Systems 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 20
Side-by-Side Interface Versioning 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 21
COM Interoperability with .NET COM Callable Wrapper COM Application .NET component Runtime Callable Wrapper .NET Application COM component 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 22
.NET Assemblies Shared assemblies Stored in the Global Assembly Cache Must be strong-named Publisher policy Application policy Assemblies can also be private to an application All assemblies require a manifest 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 23
Refactoring – Key Points Build a good set of tests before you start Make a small change, then test 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 24
Class Diagram of Starting Program 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 25
Interactions for the Statement Method 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 26
Refactoring First Example public String statement() … switch (each.getMovie().getPriceCode()){ case Movie.REGULAR: thisAmount+=2 if(each.getaysRented() > 2) thisAmount+= (each.getdaysRented -2) * 1.5; break; case Movie.NEW_RELEASE: thisAmount+= each.getDaysRented()*3 } Extract out into its own method Pass each as a variable Return thisAmount 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 27
Rename Variables private double amountFor(Rental aRental) { double result = 0; switch (aRental.getMovie().getPriceCode()){ case Movie.REGULAR: result +=2 if(aRental.getaysRented() > 2) result += (aRental.getDaysRented -2) * 1.5; break; case Movie.NEW_RELEASE: result += aRental.getDaysRented()*3 case Movie.CHILDRENS: result += 1.5; if (aRental.getDaysRented() > 3) result += (aRental.getDaysRented()-3) * 1.5; } return result; 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 28
Move the Method to the Rental Class double getCharge() { double result = 0; switch (aRental.getMovie().getPriceCode()){ case Movie.REGULAR: result +=2 if(aRental.getaysRented() > 2) result += (aRental.getDaysRented -2) * 1.5; break; case Movie.NEW_RELEASE: result += aRental.getDaysRented()*3 case Movie.CHILDRENS: result += 1.5; if (aRental.getDaysRented() > 3) result += (aRental.getDaysRented()-3) * 1.5; } return result; In Customer, delegate to the new method and test If it works, find calls to the old method and change them 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 29
Class Diagram After the Move 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 30
Replace the Conditional Logic with Polymorphism Move the getCharge method to Movie Pass daysRented 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 31
Using Inheritance on Movie 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 32
Using the State Pattern on Movie 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 33
Interactions with the State Pattern 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 34
Class Diagram after the State Pattern 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 35
Why Refactor Improve software design Make software easier to understand Find bugs Develop code more quickly 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 36
When to Refactor If you do the same thing over and over When you add functionality When you need to fix a bug During code review 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 37
Potential Problems Databases Changing interfaces Design changes that are hard to refactor 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 38
Summary In this unit, students learned: Using patterns Using frameworks Using Java 2 Platform Using JavaBeans Using COM Using ActiveX Using .NET components Using Web Services Refactoring 11/29/2018© 2006 ITT Educational Services Inc. Structured Programming: Unit 10 Slide 39