Composition  recall that an object of type X that is composed of an object of type Y means  X has-a Y object and  X owns the Y object  in other words.

Slides:



Advertisements
Similar presentations
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Composition (Part 2) 1. Class Invariants  class invariant  some property of the state of the object that is established by a constructor and maintained.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Utilities (Part 2) Implementing static features 1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Goals for Today 1  Multiton  maps  static factory methods.
Mixing Static and Non-static
1  lecture slides online
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Puzzle 1  what does the following program print? public class Puzzle01 { public static void main(String[] args) { System.out.print("C" + "S" + "E"); System.out.println('1'
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Composition 1.  recall that an object of type X that is composed of an object of type Y means  X has-a Y object and  X owns the Y object  in other.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Written by: Dr. JJ Shepherd
Objects with Functions and Arrays. Objects can be Passed Class defines type – Can use as type of function or parameter.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Written by: Dr. JJ Shepherd
Classes and Objects Introduced
Aggregation and Composition
Information hiding.
The hashCode method.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Non-static classes.
Computer Science II Exam 1 Review.
Defining Classes II.
Classes and Objects, State and Behavior
slides created by Ethan Apter
Object initialization: constructors
Building Java Programs
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Building Java Programs
Building Java Programs
slides created by Ethan Apter
Basics of OOP A class is the blueprint of an object.
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Special instance methods: toString
Building Java Programs
Building Java Programs
CSE 1030: Aggregation and Composition
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Composition.
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
slides created by Ethan Apter and Marty Stepp
CMPE212 – Reminders Assignment 2 due next Friday.
Software Specifications
Building Java Programs
Presentation transcript:

Composition  recall that an object of type X that is composed of an object of type Y means  X has-a Y object and  X owns the Y object  in other words 1 the X object, and only the X object, is responsible for its Y object

Composition & the Default Constructor  if a default constructor is defined it must create a suitable Y object public X() { // create a suitable Y; for example // 1. this.y = new Y(); // 2. this.y = new Y( /* suitable arguments */ ); } 2 the X object, and only the X object, is responsible for its Y object

Test Your Knowledge 1. A Book has a title and a number of pages. Implement a reasonable default constructor. 2. A Triangle has three points name p1, p2, and p3, represented by Vector2d objects. By default, the points have coordinates (0, 0), (1, 0), and (0, 1). Implement the default constructor. 3

Composition & Copy Constructor  if a copy constructor is defined it must create a new Y that is a deep copy of the other X object’s Y object public X(X other) { // create a new Y that is a copy of other.y; for example // 1. this.y = new Y(other.y); // 2. this.y = new Y(other.getY()); } 4 the X object, and only the X object, is responsible for its Y object

Composition & Copy Constructor  what happens if the X copy constructor does not make a deep copy of the other X object’s Y object? // don’t do this public X(X other) { this.y = other.y; }  every X object created with the copy constructor ends up sharing its Y object  if one X modifies its Y object, all X objects will end up with a modified Y object  this is called a privacy leak 5

Test Your Knowledge 1. Suppose Y is an immutable type. Does the X copy constructor need to create a new Y ? Why or why not? 2. Implement the Book copy constructor. 6

7 3. Suppose you have a Triangle copy constructor and main method like so: public Triangle(Triangle t) { this.p1 = t.p1; this.p2 = t.p2; this.p3 = t.p3; } public static void main(String[] args) { Triangle t1 = new Triangle(); Triangle t2 = new Triangle(t1); t1.p1.set( -100, -100 ); System.out.println( t2.p1 ); } What does the program print? How many Vector2d objects are there in memory? How many Vector2d objects should be in memory? Implement a better copy constructor.

Composition & Other Constructors  a constructor that has a Y parameter must first deep copy and then validate the Y object public X(Y y) { // create a copy of y this.y = new Y(y); // validate this.checkY(this.y); // throws an exception if // this.y is invalid } 8 the X object, and only the X object, is responsible for its Y object

Composition & Other Constructors  it may be possible to fix a bad Y object in some cases public X(Y y) { // create a copy of y this.y = new Y(y); // validate; checkY returns false for invalid Y if(this.checkY(this.y) == false) { this.fixY(this.y); } 9

Composition and Other Constructors  why is the deep copy required?  if the constructor does this // don’t do this for composition public X(Y y) { this.y = y; } then the client and the X object will share the same Y object  this is called a privacy leak 10 the X object, and only the X object, is responsible for its Y object

Test Your Knowledge 1. Suppose Y is an immutable type. Does the X constructor need to copy the other X object’s Y object? Why or why not? 2. Implement the following Triangle constructor: /** * Create a Triangle from 3 points p1 The first point. p2 The second point. p3 The third point. IllegalArgumentException if the 3 points are * not unique */ 11 Triangle has a class invariant: the 3 points of a Triangle are unique

Test Your Knowledge 3. Implement the following Triangle constructor; make sure you maintain the class invariant: /** * Create a Triangle from 3 points p1 The first point. p2 The second point. p3 The third point. */ 12 Triangle has a class invariant: the 3 points of a Triangle are unique

Composition and Accessors  never return a reference to an attribute; always return a deep copy public Y getY() { return new Y(this.y); } 13 the X object, and only the X object, is responsible for its Y object

Composition and Accessors  why is the deep copy required?  if the accessor does this // don’t do this for composition public Y getY() { return this.y; } then the client and the X object will share the same Y object  this is called a privacy leak 14 the X object, and only the X object, is responsible for its Y object

Test Your Knowledge 1. Suppose Y is an immutable type. Does the X accessor need to copy it’s Y object before returning it? Why or why not? 2. Implement the following 3 Triangle accessors: /** * Get the first/second/third point of the triangle. The first/second/third point of the triangle */ 15

Test Your Knowledge 3. Given your Triangle accessors from question 2, can you write an improved Triangle copy constructor that does not make copies of the point attributes? 16

Composition and Mutators  if X has a method that sets its Y object to a client- provided Y object then the method must make a deep copy of the client-provided Y object and validate it public void setY(Y y) { Y copyY = new Y(y); this.checkY(copyY); // throws an exception if copyY // is invalid this.y = copyY; } 17 the X object, and only the X object, is responsible for its Y object

Composition and Mutators  it may be possible to fix a bad Y object in some cases public void setY(Y y) { Y copyY = new Y(y); if(this.checkY(copyY) == false) { this.fixY(copyY); } this.y = copyY; } 18

Composition and Mutators  why is the deep copy required?  if the mutator does this // don’t do this for composition public void setY(Y y) { this.y = y; } then the client and the X object will share the same Y object  this is called a privacy leak 19 the X object, and only the X object, is responsible for its Y object

Test Your Knowledge 1. Suppose Y is an immutable type. Does the X mutator need to copy the Y object? Why or why not? Does it need to the validate the Y object? 2. Implement the following 3 Triangle mutators: /** * Set the first/second/third point of the triangle. p The desired first/second/third point of * the triangle. true if the point could be set; * false otherwise */ 20 Triangle has a class invariant: the 3 points of a Triangle are unique