160 likes | 189 Views
CSE 1030: Aggregation and Composition. Mark Shtern. Summary. Aggregation Constructors Accessor/Mutators Collections. Collections. Copy Contractor Alias this.setBooks ( library.books ) Shallow Copy this.setBooks (new ArrayList ( library.getBooks ())) Deep Copy
E N D
CSE 1030: Aggregation and Composition Mark Shtern
Summary • Aggregation • Constructors • Accessor/Mutators • Collections
Collections • Copy Contractor • Alias this.setBooks(library.books) • Shallow Copy this.setBooks(new ArrayList(library.getBooks())) • Deep Copy ArrayList copy= new ArrayList(); for (Book book:books) copy.add(new Book(book)); this.setBooks(copy);
What is Composition • Object Composition is a way to combine simple object into more complex ones • Composite objects are usually expressed by means of references from one object to another • Exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'
Is it composition? Carburetor carburetor = new Carburetor(); car = new Car(carburetor); Answer: NO
Is it composition? Carburetor carburetor = new Carburetor(); car = new Car(carburetor); Answer:Yes
Is it possible? Carburetor carburetor = new Carburetor(); car = new Car(carburetor); Carburetor carburetor1 = carburetor; carburetor = car.getCarburetor(); Answer: NO
Is composition correctly implemented? Carburetor carburetor = new Carburetor(); car = new Car(carburetor); carburetor = car.getCarburetor(); Answer: When carburetor is immutable object then yes, otherwise no
Is composition correctly implemented? Carburetor carburetor = new Carburetor(); car = new Car(carburetor); carburetor = car.getCarburetor(); Answer:Yes
UML Diagram public Car { private Carburetor carburetor; Car () { carburetor = new Carburetor(); } }
Constructor Carburetor carburetor = new Carburetor(); car = new Car(carburetor); public Car(Carburetor carburetor) { this.setCarburator(carburetor); }
Method section • Accessor public Carburetor getCarburetor() { return new Carburetor (this.carburator); }
Method section • Mutator public boolean setCarburetor(Carburetor c) { // validation section // returns false if input is invalid this.carburator = new Carburetor(c); return true; }
Ex601 • Develop an directory application which stores personal information about students, faculty and staff. In addition to the information commonly found in telephone books, your directory contains the following data • Student Major • Staff and Faculty Room • Faculty Office hours • Staff Title