Supplier Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Supplier Classes

Description:

When a client class uses a supplier class as one of its variables, this is known ... you know what you're doing, other coders can subclass your class and incorrectly ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 16
Provided by: mathew
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: Supplier Classes


1
Supplier Classes
  • Chapter 6
  • The Object of Java

2
Clients and Suppliers
  • A client class borrows the facilities (i.e.
    accesses state or methods) from a supplier class.
  • Supplier classes can be used by the client class
    in any of the following ways
  • A variable type
  • A parameter type
  • A return type

3
Composition
  • When a client class uses a supplier class as one
    of its variables, this is known as composition.
  • In UML notation, this is shown by

Supplier class
Client class
4
Client-Supplier Example (1)
  • public class LibraryBook
  • String title
  • List waitingList
  • ...
  • String and List are supplier classes used by the
    client class LibraryBook

5
Client-Supplier Example (2)
LibraryBook

String
List


6
Creating Supplier Classes
  • Use defensive programming protect supplier code
    from undesirable use by the client
  • One way to do this is to use appropriate
    accessibility modifiers on variables and methods
  • Many times, methods are the only public members
    of a supplier class. But in general, use the
    need to know concept to determine visibility.
  • All members of a class are said to be
    encapsulated in that class.

7
Need To Know Example (1)
  • public class LibraryBook
  • private String title
  • private List waitingList
  • ...
  • public Member nextMember()
  • if (!waitingList.isEmpty())
  • return waitingList.get(0)
  • else
  • return null

8
Need To Know Example (2)
  • Clients of LibraryBook do not need to know that
    LibraryBook contains a List.
  • They only really need to know that we can return
    the next library member on the waiting list
    through the nextMember() method call.
  • This is why waitingList is declared with the
    private accessibility modifier.

9
Another Need To Know Example (1)
  • You should declare local variables (i.e.
    variables declared inside the body of methods)
    whenever possible instead of declaring instance
    variables.
  • If a variable only has relevance inside a certain
    method, dont share it with other methods.
  • Even if you know what youre doing, other coders
    can subclass your class and incorrectly use
    instance variables that couldve been declared
    local.
  • This is another example of defensive programming.

10
Another Need To Know Example (2)
  • public class Circle
  • protected float area
  • protected float radius
  • public void setRadius(float radius)
  • this.radius radius
  • public float area()
  • area Math.PI radius radius
  • return area

11
Another Need To Know Example (3)
  • public class Circle
  • // protected float area
  • protected float radius
  • public void setRadius(float radius)
  • this.radius radius
  • public float area()
  • float area Math.PI radius radius
  • return area

12
Information Hiding
  • Information hiding is when you declare variables
    local or private.
  • In this way, clients cannot access those
    variables.
  • Information hiding leads to thin interfaces.
  • In general, an interface to a class is composed
    of the variables and methods that are declared
    public, protected, and default.
  • These members are accessible to clients. By
    declaring some variables local or private, we
    keep the interface thin by reducing the size of
    the contract with clients.

13
Accessor Methods
  • Accessor methods are methods that provide read
    and write access to private variables.
  • Accessor methods are sometimes referred to as
    getter (read) and setter (write) methods.

14
Read-Only Access Example (Getter)
  • public class Person
  • private int age
  • public int getAge()
  • return age

15
Write-Only Access Example (Setter)
  • public class Person
  • private int age
  • public void setAge(int age)
  • this.age age
Write a Comment
User Comments (0)
About PowerShow.com