How to Design Supplier Classes

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Programming Languages and Paradigms
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
C++ Classes & Data Abstraction
C++ Lecture 6 Object Life-times Creating objects using new Using pointers to objects Aggregation (Containment –UML speak) Other C++ class features.
Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Software Testing and Quality Assurance
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Review of C++ Programming Part II Sheng-Fang Huang.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Inheritance in the Java programming language J. W. Rider.
© 2006 Pearson Addison-Wesley. All rights reserved How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)
ECE122 Feb. 22, Any question on Vehicle sample code?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Lecture 4.1 More About Methods - Using a Prototyping Approach.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Structures and Classes
Prototyping with Methods
Topic: Classes and Objects
Visibility, Accessibility, and Information Hiding
Examples of Classes & Objects
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 3: Using Methods, Classes, and Objects
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 4: Writing Classes
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Prototyping with Methods
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Encapsulation & Visibility Modifiers
Defining Classes and Methods
Chapter 4: Writing classes
Object-Oriented Programming
Classes & Objects: Examples
Introduction to Classes and Objects
Chapter 4 Writing Classes.
Object-Oriented Programming
Defining Classes and Methods
Object-Oriented Programming
NAME 436.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Information Hiding and Encapsulation Section 4.2
(4 – 2) Introduction to Classes in C++
Presentation transcript:

How to Design Supplier Classes Lecture 6.2 How to Design Supplier Classes

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

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

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

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

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

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

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

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