Prévia do material em texto
Projeto de Software / P3 Composição e Herança Márcio Ribeiro marcio@ic.ufal.br twitter.com/marciomribeiro Composi'on 3 ShoppingCart… § … has a list of products ShoppingCart! - customerID : int" - productList : ArrayList" + addProduct() : void" + removeProduct() : void" + getContents() : String" + getCustomerID() : int" + getItemCount() : int" + getTotalPrice() : double" Product! - name : String" - price : double" + getName() : String" + getPrice() : double" n 4 Composi'on § “has-‐a” rela'onship § Code reuse § Example: § ArrayList § List of our shopping cart products Now, let’s improve our E-‐commerce applica'on! 6 E-‐commerce products 7 TV § Implement the following TV class TV! - brand : String" - price : double" - inches : int" + getBrand() : String" + getPrice() : double" + getInches(): int" 8 Refrigerator § Now, implement this one: Refrigerator! - brand : String" - price : double" - size : int" + getBrand() : String" + getPrice() : double" + getSize(): int" 9 Stove § Finally, this one: Stove! - brand : String" - price : double" - burners : int" + getBrand() : String" + getPrice() : double" + getBurners(): int" 10 Keep in mind that… § SoLware changes all the 'me! § “80% of the life'me cost of a piece of soLware goes to maintenance” § Change requests! § You need to be prepared § Avoid error-‐prone tasks § Improve produc'vity § Improve 'me-‐to-‐market From http://www.oracle.com/technetwork/java/codeconv-‐138413.html 11 Change request § CR: we need to debit the IPI tax for all products 12 Change request scaUered throughout the classes… TV! - brand : String" - price : double" - inches : int" + getBrand() : String" + getPrice() : double" + getInches(): int" Refrigerator! - brand : String" - price : double" - size : int" + getBrand() : String" + getPrice() : double" + getSize(): int" Stove! - brand : String" - price : double" - burners : int" + getBrand() : String" + getPrice() : double" + getBurners(): int" What is going on? Error-prone Low productivity Time consuming 14 What are the commonali'es?! TV! - brand : String" - price : double" - inches : int" + getBrand() : String" + getPrice() : double" + getInches(): int" Refrigerator! - brand : String" - price : double" - size : int" + getBrand() : String" + getPrice() : double" + getSize(): int" Stove! - brand : String" - price : double" - burners : int" + getBrand() : String" + getPrice() : double" + getBurners(): int" What is going on? Cloned code: no reuse 16 In our e-‐commerce applica'on… § What are TV, Refrigerator, and Stove?! PRODUCTS! No'ce that this is common in real life! 18 Generaliza'on versus Specializa'on Sp ec ia liz at io n G eneralization Vehicle Car Race car Ophthalmologist Person Doctor 19 Commonali'es § They all have: § Brand § Price This is general for all products Move the common… Product! - brand : String" - price : double" + getBrand() : String" + getPrice() : double" So, move the common attributes and methods to the Product class… Common aUributes in the Product class… Product! - brand : String" - price : double" + getBrand() : String" + getPrice() : double" TV! - inches : int" + getInches(): int" Refrigerator! - size : int" + getSize(): int" Stove! - burners : int" + getBurners(): int" … and keep the variabilities in each specialized class Inheritance 23 Inheritance § Mechanism to § Organize informa'on § Achieve categoriza'on § Group and modeling similari'es among classes § Create a taxonomy of objects § Hierarchy tree § “is-‐a” rela'onship § TV is a product § Refrigerator is a product § Stove is a product 24 Superclass package br.ufal.ic.ecommerce.product; public class Product { private String brand; private double price; public Product(String brand, double price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public double getPrice() { return price; } } 25 Subclass package br.ufal.ic.ecommerce.product; public class TV extends Product { private int inches; public TV(String brand, double price, int inches) { super(brand, price); this.inches = inches; } public int getInches() { return inches; } } 26 Back to the change request § CR: we need to debit the IPI tax for all products Common aUributes in the Product class… Product! - brand : String" - price : double" + getBrand() : String" + getPrice() : double" TV! - inches : int" + getInches(): int" Refrigerator! - size : int" + getSize(): int" Stove! - burners : int" + getBurners(): int" Now, only one class needs changing! 28 Reasons to use inheritance § BeUer modularity § Separa'on of concerns § Categoriza'on § Clear representa'on of concepts