Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2006 Pearson Addison-Wesley. All rights reserved 6.2.1 How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)

Similar presentations


Presentation on theme: "© 2006 Pearson Addison-Wesley. All rights reserved 6.2.1 How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)"— Presentation transcript:

1 © 2006 Pearson Addison-Wesley. All rights reserved 6.2.1 How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2) Discover private attributes. Generally, the need for a supplier class is discovered via an object. Programmers discover objects by looking for data and operations that belong together. 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)

2 © 2006 Pearson Addison-Wesley. All rights reserved 6.2.2 public A public variable, method or class has no scope restrictions (i.e., it is accessible anywhere its class is used. The word scope refers to regions of the program that are permitted to utilize some declared entity. 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.

3 © 2006 Pearson Addison-Wesley. All rights reserved 6.2.3 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() {... } public class MyClass { public int badVar; private int betterVar; public Myclass() { int bestVar;... } public void publicMethod() {... } private void privateMethod() {... }

4 © 2006 Pearson Addison-Wesley. All rights reserved 6.2.4 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)

5 © 2006 Pearson Addison-Wesley. All rights reserved 6.2.5 RULE: Hide as much information (from the client) as possible. Use _______ whenever possible. Use _________ if variable must be retained between method executions. Variables... Avoid _______ variables. Use private when possible. Methods... Use public if the method is designed to be called from outside the class.

6 © 2006 Pearson Addison-Wesley. All rights reserved 6.2.6 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; }... public class Figure { private Color privateColor;... public void setBackground( Color c ) { privateColor = c; } public Color getBackground() { return privateColor; }...

7 © 2006 Pearson Addison-Wesley. All rights reserved 6.2.7 Just like defensive driving... 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. 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


Download ppt "© 2006 Pearson Addison-Wesley. All rights reserved 6.2.1 How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)"

Similar presentations


Ads by Google