CSSE 375 Organizing Data – Part 2 Shawn and Steve Continue the same quiz!

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
Lectures 17 and 18 A Refactoring Micro-Example FOR0383 Software Quality Assurance 5/16/20151Dr Andy Brooks Refactoring is really easy using this tool.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Software Engineering Key Refactorings
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Abstraction, Inheritance, and Polymorphism in Java.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
Refactoring. Mathematics: Factor ● fac·tor – One of two or more quantities that divides a given quantity without a remainder, e.g., 2 and 3 are factors.
Refactoring Improving the structure of existing code Refactoring1.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
SWE 316: Software Design and Architecture Objectives Lecture # 20 Improving the existing design: Refactoring SWE 316: Software Design and Architecture.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Refactoring1 Improving the structure of existing code.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Object Oriented Software Development
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
Today’s Agenda  More refactoring patterns Software Testing and Maintenance 1.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Refactoring II Dealing with Polymorphism. Switch in Rental Switches on Movie! class Rental … public double getCharge() { double result = 0; switch (getMovie().getPriceCode()){
Module 3. Smells Between Classes Course: Refactoring.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Refactoring Conditionals Lesson Five: Conditionals.
Object Oriented Programming
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring. Mathematics: Factor ● fac·tor – One of two or more quantities that divides a given quantity without a remainder, e.g., 2 and 3 are factors.
Refactoring1 Improving the structure of existing code.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
Refactoring Constants and Variables Lesson Three: Constants and Variables.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
REFACTORING CHANGE VALUE TO REFERENCE SUBSTITUTE ALGORITHM REPLACE CONDITIONAL WITH POLYMORHPISM.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Module 9. Dealing with Generalization Course: Refactoring.
Catalog of Refactoring (3) Organizing Data. Catalog Self Encapsulate Field Replace Data Value with Object Change Value to Reference Change Reference to.
Catalog of Refactoring (5) Simplifying Conditional Expressions.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Catalog of Refactoring (6) Making Method Calls Simpler.
Principles and examples
Catalog of Refactoring
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
Refactoring Methods: Kevin Murphy.
SWEN-610 Foundations of Software Engineering
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Improving the structure of existing code
CIS 199 Final Review.
Presentation transcript:

CSSE 375 Organizing Data – Part 2 Shawn and Steve Continue the same quiz!

2 Mathematics: Factor fac·tor l One of two or more quantities that divides a given quantity without a remainder, for example: –2 and 5 are factors of 10 –a and b are factors of ab –(a+1) and (a+3) are factors of a 2 + 4a + 3 fac·tor·ing l To determine or indicate explicitly the factors of Reconsidering Q1…

3 Software Engineering: Factoring  fac·tor l Individual items that combined together form a complete software system: –identifiers –contents of function –contents of classes and place in inheritance hierarchy  fac·tor·ing l Determining the items, at design time, that make up or compose a software system Q9

4 Organizing Data  Refactorings that make working with data easier – Lots of them! 1. Self Encapsulate Field 2. Replace Data Value with Object 3. Change Value to Reference 4. Change Reference to Value 5. Replace Array with Object 6. Duplicate Observed Data 7. Change Unidirectional Association to Bidirectional 8. Change Bidirectional Association to Unidirectional 9. Replace Magic Number with Symbolic Constant 10. Encapsulate Field 11. Encapsulate Collection 12. Replace Record with Data Class 13. Replace Type Code with Class 14. Replace Type Code with Subclasses 15. Replace Type Code with State/Strategy 16. Replace Subclass with Fields 1. Self Encapsulate Field 2. Replace Data Value with Object 3. Change Value to Reference 4. Change Reference to Value 5. Replace Array with Object 6. Duplicate Observed Data 7. Change Unidirectional Association to Bidirectional 8. Change Bidirectional Association to Unidirectional 9. Replace Magic Number with Symbolic Constant 10. Encapsulate Field 11. Encapsulate Collection 12. Replace Record with Data Class 13. Replace Type Code with Class 14. Replace Type Code with Subclasses 15. Replace Type Code with State/Strategy 16. Replace Subclass with Fields

5 Replace Array with Object  Situation: You have an array in which certain elements mean different things  Solution: Replace the array with an object that has a field for each element  Similarly, Replace Record with Data Class String[] row = new String[3]; row [0] = "Liverpool"; row [1] = "15"; Performance row = new Performance(); row.setName("Liverpool"); row.setWins("15"); Q10

6 Duplicate Observed Data 6  Situation: You have domain data available only in a GUI control, and domain methods need access.  Solution: Copy the data to a domain object. Set up an observer to synchronize the two pieces of data. Q11

7 Replace Type Code with Class  Situation: A class has a numeric type code that does not affect its behavior  Solution: Replace the number with a new class Q12

8 Replace Type Code with Subclasses  Situation: You have an immutable type code that affects the behavior of a class  Solution: Replace the type code with subclasses Q13

9 Encapsulate Collection  Situation: A method returns a collection  Solution: Make it return a read-only view and provide add/remove methods Q14

10 Replace Subclass with Fields  Situation: You have subclasses that vary only in methods that return constant data  Solution: Change the methods to superclass fields and eliminate the subclasses

11 Replace Type Code with State/Strategy  Situation: You have a type code that affects the behavior of a class, but you cannot use subclassing  Solution: Replace the type code with a state object

12 Mechanics  Self-encapsulate the type code  Create a new class, and name it after the purpose of the type code. This is the state object.  Add subclasses of the state object, one for each type code l It’s easier to add subclasses all at once, rather than one at a time  Create an abstract query in the state object to return the type code. Create overriding queries of each state object subclass to return the correct type code  Compile  Create a field in the old class for the new state object  Adjust the type code query on the original class to delegate to the state object  Adjust the type code setting methods on the original class to assign an instance of the appropriate state object subclass  Compile and test

13 Replace Type Code with State/Strategy class Employee { private int _type; static final int ENGINEER = 0; static final int SALESMAN = 1; static final int MANAGER = 2; Employee (int type) {_type = type;) } int payAmount() { switch (_type) { case ENGINEER: return _monthlySalary; case SALESMAN: return _monthlySalary + _commission; case MANAGER: return _monthlySalary + _bonus; default: throw new Exception("Incorrect Code"); } Q15

14 Employee (int type) { setType (type);} int getType() { return _type;} void setType(int arg) { _type = arg;} int payAmount() { switch (getType()) { case ENGINEER: return _monthlySalary; case SALESMAN: return _monthlySalary + _commission; case MANAGER: return _monthlySalary + _bonus; default: throw new RuntimeException("Incorrect Employee"); } First Self Encapsulate Type code … Self-Encaps

15 abstract class EmployeeType { abstract int getTypeCode(); } class Engineer extends EmployeeType { int getTypeCode () { return Employee.ENGINEER; } class Manager extends EmployeeType { int getTypeCode () { return Employee.MANAGER; } class Salesman extends EmployeeType { int getTypeCode () { return Employee.SALESMAN; } Declare State Class and Subclasses… Subclasses Abstract Class

16 Hook Subclasses into Employee… class Employee... private EmployeeType _type; int getType() { return _type.getTypeCode(); } void setType(int arg) { switch (arg) { case ENGINEER: _type = new Engineer(); break; case SALESMAN: _type = new Salesman(); break; case MANAGER: _type = new Manager(); break; default: throw new Exception("Incorrect Code"); } Employee Type Set via Accessors

17 Create a factory method for employee types… class Employee... void setType(int arg) { _type = EmployeeType.newType(arg); } class EmployeeType... static EmployeeType newType(int code) { switch (code) { case ENGINEER: return new Engineer(); case SALESMAN: return new Salesman(); case MANAGER: return new Manager(); default: throw new Exception("Incorrect Code"); } static final int ENGINEER = 0; static final int SALESMAN = 1; static final int MANAGER = 2; Factory Method For Employee Type

18 Remove the type code definitions … class Employee... int payAmount() { switch (getType()) { case EmployeeType.ENGINEER: return _monthlySalary; case EmployeeType.SALESMAN: return _monthlySalary + _commission; case EmployeeType.MANAGER: return _monthlySalary + _bonus; default: throw new Exception("Incorrect Code"); }  Exercise: Now ready to use Replace Conditional with Polymorphism on payAmount Q16

19 Replace Conditional with Polymorphism on payAmount class Salesman... int payAmount(Employee emp) { return emp.getMonthlySalary() + emp.getCommission(); } class Manager... int payAmount(Employee emp) { return emp.getMonthlySalary() + emp.getBonus(); } … class EmployeeType... abstract int payAmount(Employee emp); Q16 - cntd Declare superclass method abstract