Download presentation
Presentation is loading. Please wait.
1
How to Design Supplier Classes
Lecture 6.2 How to Design Supplier Classes
2
Clients and Suppliers How to Design a Supplier
Generally, the need for a supplier class is discovered via an object. Programmers discover objects by looking for data and operations that belong together. How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2) Discover private attributes. 3) Implement public methods. (This step often suggests the need for other private members.) 4) Double-check the scope of all members (local is better than private; private is better than public) © 2006 Pearson Addison-Wesley. All rights reserved
3
Scope public private local
The word scope refers to regions of the program that are permitted to utilize some declared entity. public A public variable, method or class has no scope restrictions (i.e., it is accessible anywhere its class is used. private A private variable or method can be referenced only within its own class. local A local variable or formal parameter can be referenced only within its own method. © 2006 Pearson Addison-Wesley. All rights reserved
4
How to Choose Scope RULE: Hide as much information (from the client) as possible. public class MyClass { public int badVar; private int betterVar; public Myclass() { int bestVar; ... } public void publicMethod() { private void privateMethod() { betterVar scope privateMethod scope bestVar scope © 2006 Pearson Addison-Wesley. All rights reserved
5
Lifetime Definition The lifetime of an object is the portion of execution time between the object’s instantiation and the time it is orphaned (or the program terminates). The lifetime of primitive data follows its scope. Local variable lifetime is the run time of a single method execution. Formal parameter lifetime is the run time of a single method execution. Instance variable lifetime is the lifetime of the containing object. The lifetime of reference data depends upon code execution. A new expression begins a lifetime. When a variable is assigned null, any object previously bound to that variable may end its lifetime. (When would this not end the object’s lifetime? What other instructions might orphan an object? (i.e., end its lifetime) © 2006 Pearson Addison-Wesley. All rights reserved
6
How to Choose Scope (revisited)
RULE: Hide as much information (from the client) as possible. Variables ... Use local whenever possible. Use private if variable must be retained between method executions. Avoid public variables. Methods ... Use private when possible . Use public if the method is designed to be called from outside the class. © 2006 Pearson Addison-Wesley. All rights reserved
7
Instance Variable Access
RULE: All supplier class instance variables should be declared private. Write access can be provided to clients by a public set method. Write access Read access can be provided to clients by a public non-void method to return the variable’s value. Read access Example public class Figure { private Color privateColor; ... public void setBackground( Color c ) { privateColor = c; } public Color getBackground() { return privateColor; provides write access provides read access © 2006 Pearson Addison-Wesley. All rights reserved
8
Defensive Programming
Defensive programming occurs when the programmer guards against possible failures. Encapsulating and hiding data through proper use of scope is an important aspect of defensive programming. Just like defensive driving... local variables... are like Grandma driving a Humvee private variables without read or write access... are like Dale Earnhardt, Jr. in a Volvo private variables with read-only access... are like your parents in their family car private variables with write access... are like giving the keys to a first-time driver public instance variables... are like 150 mph in a Ford Pinto on a curvy country road © 2006 Pearson Addison-Wesley. All rights reserved
9
Exercise: The Traffic Light
Consider a program that needs to create several traffic lights and control their lighting sequence. Assume that all will be placed upon and JFrame. Consider what classes you will need, what class members and what scope for each member. © 2006 Pearson Addison-Wesley. All rights reserved
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.