Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 5 Review & Announcement

Similar presentations


Presentation on theme: "Week 5 Review & Announcement"— Presentation transcript:

1 Week 5 Review & Announcement

2 Midterm Date: 27 Feb, 8:00-11:00 and 12:00-15:00
Programming Part: 12: :00 Bring your computer to both exams.

3 Lab 4 Correct! Everyone implemented java.util.Comparable
java.util.Comparator

4 Access Control __________ void setId(long id) { this.id = id; }
What access modifier should you use? _______________ any code can call this method _______________ only code in this class can call it _______________ only code in classes in same package _______________ only code in this class or a subclass __________ void setId(long id) { this.id = id; }

5 3 Fundamental Characteristics of OOP
_______________ an object contains both data (state) and methods that operate on the data. It can control access to both the data and methods. _______________ Many classes can implement the same method (in different ways) and we can invoke the method (behavior) without knowing the type of object that will perform the method. _______________ we can define a hierarchy of classes so that a class automatically has all the methods of its parent classes.

6 Mutable vs. Immutable 1. What does it mean for an object to me mutable? 2. Name a class in Java API that is mutable. How would you demonstrate that it is mutable? 3. Name a class in Java API that is immutable.

7 Mutable & Encapsulation
Encapsulation: an object should encapsulate and control access to its state. Example: if your friend asks for money you might give it to him, but you probably wouldn't give him your wallet! public Money giveMeMoneyPlease( double amount) { return wallet.withdraw(amount); } public Wallet wallet; // not this

8 Breaking Encapsulation
Can you write some code to change a person's birthday? public class Person { private String name; private final Date birthday; public Person(String name, Date bday) { this.name = name; this.birthday = bday; } /** get the person's birthday */ public Date getBirthday() { return birthday; //NO setBirthday() method

9 Example Date bday = new Date(100,Calendar.MARCH,1);
Person pee = new Person("Peewee", bday); // Hack #1: Person copied a reference to // our date! bday.setMonth(Calendar.JULY); // now pee's birthday is July 1. // Hack #2: get a reference to birthday // and change it. Date date = pee.getBirthday(); date.setYear(1); // now pee's birthday is 1 July 1901

10 Why Does this Work? java.util.Date is mutable, and
Person getBirthday() is returning a reference to his private Date object. Once we have the reference, we can change the object it refers to.

11 What's the Solution? 1. Use an immutable date, such as java.time.LocalDate (classes in java.time are newer and better) or 2. Make a copy of external reference (in constructor) and return a copy (in getBirthday). But this is expensive (extra objects).

12 Breaking Encapsulation, Part 2
Can you change the Person's name from outside the class? public class Person { private String name; private final Date birthday; public Person(String name, Date bday) { this.name = name; this.birthday = bday; } /** get the person's name */ public String getName() { return name;

13 This Week's Lab Abstract superclass for money to avoid duplicate code
also make it easier to implement new money Factory method to create money Separate Factory for Thai money, Malay money, Indian money, etc. the application only needs one factory object (can be shared). How to ensure Money Factory is only created once?

14 Lessons from Lab

15 A Class has a Single Purpose
Purse manage money in a purse Coin, Banknote represent a unit of currency ConsoleDialog user interface to use purse Main purpose: create objects, connect them ("wire together"), and start the application.

16 High Cohesion All the methods of a class should be related to the same purpose or group of responsibilities.

17 Design using Layers User Interface User interface layer.
Domain layer (model). Purse, Valuable, ... Java Foundation General services.

18 Higher Layers call Lower Ones
User Interface Purse, Valuable, ... Java Foundation

19 Depend on Abstractions
The Purse depends on the concept (abstraction) of money, not on a concrete form of money (coin,...).

20 Separate Creation from Usage
The ConsoleDialog (user interface) uses a Purse, but it does not create a Purse. The Application class (Main class) gives a Purse to the UI. This is called Dependency Injection. // Main class is responsible for creating // objects. public static void main(String[] args) { Purse purse = new Purse(CAPACITY); ConsoleDialog ui = new ConsoleDialog( purse ); ui.run(); }


Download ppt "Week 5 Review & Announcement"

Similar presentations


Ads by Google