Working Classes SCMP Special Topic: Software Development

Slides:



Advertisements
Similar presentations
Software Engineering Class design. Class design – ADT Abstract Data Types:  Why use ADT? Hidden implementation details Changes do not affect whole program.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Object-oriented Programming Concepts
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 Working with Classes Chapter 6. 2 Class definition A class is a collection of data and routines that share a well-defined responsibility or provide.
Abstract Data Types and Encapsulation Concepts
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.
Good Design of Classes Classes let us create Abstract Data Types Should be well aware of benefits – Hide implementation details – Localize changes – Interface.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Best Practices in the Object-Oriented Design Vesko Kolev Telerik Corporation
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Copyright © Jim Fawcett Spring 2017
OOP: Encapsulation &Abstraction
Object-Oriented Programming Concepts
Programming Logic and Design Seventh Edition
Abstract Data Types and Encapsulation Concepts
Object-Oriented Programming: Inheritance
Inheritance and Polymorphism
Module 5: Common Type System
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
11.1 The Concept of Abstraction
About the Presentations
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Object Based Programming
Abstract Data Types and Encapsulation Concepts
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
More Object-Oriented Programming
Java Inheritance.
COP 3330 Object-oriented Programming in C++
Fundaments of Game Design
Abstract Classes and Interfaces
Overview of C++ Polymorphism
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Object-Oriented PHP (1)
Lecture 10 Concepts of Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
11.1 The Concept of Abstraction
SPL – PS4 C++ Advanced OOP.
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Working Classes SCMP 391.00 Special Topic: Software Development Spring 2017 James Skon

Abstract Data Types An abstract data type is a collection of data and operations that work on that data. The idea is to hide the implementation, while providing a service. 3, 50, 15, 10, … 3, 10, 15, 20 Priority Queue

Abstract Data Types (ADTs) You can hide implementation details Changes don't affect the whole program You can make the interface more informative It's easier to improve performance (isolated code) The program is more obviously correct The program becomes more self-documenting You don't have to pass data all over your program (hidden in ADT) You're able to work with real-world entities rather than with low-level implementation structures

ADT Examples Cruise Control Stack Blender Light Set speed Get current settings Resume former speed Deactivate Stack Initialize stack Push item onto stack Pop item from stack Read top of stack Blender Turn on Turn off Set speed Start "Insta-Pulverize" Stop "Insta-Pulverize" Light

Good Class Interfaces class Employee { public: // public constructors and destructors Employee(); Employee( FullName name, String address, String workPhone, String homePhone, TaxId taxIdNumber, JobClassification jobClass ); virtual ~Employee(); // public routines FullName GetName() const; String GetAddress() const; String GetWorkPhone() const; String GetHomePhone() const; TaxId GetTaxIdNumber() const; JobClassification GetJobClassification() const; ... private: };

contains a collection of miscellaneous functions Poor Class Interfaces class Program { public: ... // public routines void InitializeCommandStack(); void PushCommand( Command command ); Command PopCommand(); void ShutdownCommandStack(); void InitializeReportFormatting(); void FormatReport( Report report ); void PrintReport( Report report ); void InitializeGlobalData(); void ShutdownGlobalData(); private: }; contains a collection of miscellaneous functions

Consistent level of abstraction class EmployeeCenus: public ListContainer { public: ... // public routines void AddEmployee( Employee employee ); void RemoveEmployee( Employee employee ); Employee NextItemInList(); Employee FirstItem(); Employee LastItem(); private: }; The abstraction of these routines is at the "employee" level. The abstraction of theseroutines is at the "list" level.

Consistent Levels of Abstraction class EmployeeCenus: public ListContainer { public: ... // public routines void AddEmployee( Employee employee ); void RemoveEmployee( Employee employee ); Employee NextEmployee(); Employee FirstEmployee(); Employee LastEmployee(); private: ListContainer m_EmployeeList; }; The abstraction of all these routines is now at the "employee" level. That the class uses the ListContainer library is now hidden.

Good Class Design Be sure you understand what abstraction the class is implementing Provide services in pairs with their opposites Move unrelated information to another class Make interfaces programmatic rather than semantic when possible Programmatic concepts can be enforced by compiler.

Beware of class erosion Over time a class might be modified and extended. Changes may not fit original class design. May class for refactoring

Not really about specifically employees. Beware of class erosion lass Employee { public: ... // public routines FullName GetName() const; Address GetAddress() const; PhoneNumber GetWorkPhone() const; bool IsJobClassificationValid( JobClassification jobClass ); bool IsZipCodeValid( Address address ); bool IsPhoneNumberValid( PhoneNumber phoneNumber ); SqlQuery GetQueryToCreateNewEmployee() const; SqlQuery GetQueryToModifyEmployee() const; SqlQuery GetQueryToRetrieveEmployee() const; private: }; Not really about specifically employees.

Good Class Design Minimize accessibility of classes and members hiding more is generally better than hiding less.

Good Class Design Consider Make private Access Methods Don't expose member data in public Consider float GetX(); float GetY(); float GetZ(); void SetX( float x ); void SetY( float y ); void SetZ( float z ); float x; float y; float z; Make private Access Methods

Containment – “has a” relationship Containment is the simple idea that a class contains a primitive data element or object. A “has a” relationship. Employee “has a” name, address, phone #, tax ID. Be critical of classes that contain more than about seven data members 7  2 rule House Kitchen BR1 BR2 Bath2 contains

Inheritance - "is a" Relationships Inheritance is the idea that one class is a specialization of another class. Kenyon Person On Campus Alumni Parent Donor Faculty Staff Student Full-time Pat-time

Inheritance - "is a" Relationships When you use inheritance, you have to make several decisions: For each member routine, will the routine be visible to derived classes? Will it have a default implementation? Will the default implementation be overridable? For each data member (including variables, named constants, enumerations, and so on), will the data member be visible to derived classes?

Inheritance If the derived class isn't going to adhere completely to the same interface contract defined by the base class, inheritance is not the right implementation technique.

Inheritance - "is a" Relationships With Inheritance, routines and variables are inherited from the parent class. Classes (and routines) may be “abstract”, that is they are a generalized concept, and are never realized as an instance. animal dog cat horse yak

Inheritance – routine types abstract overridable routine - derived class inherits the routine's interface but not its implementation. overridable routine - derived class inherits the routine's interface and a default implementation and it is allowed to override the default implementation. non-overridable routine - class inherits the routine's interface and its default implementation and it is not allowed to override

Inheritance – general rules Move common interfaces, data, and behavior as high as possible in the inheritance tree Be suspicious of classes of which there is only one instance Be suspicious of base classes of which there is only one derived class Be suspicious of classes that override a routine and do nothing inside the derived routine Avoid deep inheritance trees (7  2 rule)

Inheritance - polymorphism polymorphism is a major benefit of inheritance A routine must be declared virtual in the base class to be overridable. switch ( shape.type ) { case Shape_Circle: shape.DrawCircle(); break; case Shape_Square: shape.DrawSquare(); ... } shape.Draw();

Member Functions and Data Keep the number of routines in a class as small as possible higher numbers of routines per class are associated with higher fault rates Disallow implicitly generated member functions and operators you don't want If you don’t want assignment, disallow it.

Member Functions and Data Minimize the extent to which a class collaborates with other classes: Minimize: Number of kinds of objects instantiated Number of different direct routine calls on instantiated objects Number of routine calls on objects returned by other instantiated objects Why? Minimize coupling!

Constructors Initialize all member data in all constructors (when possible) defensive programming practice Prefer deep copies to shallow copies until proven otherwise. What are deep copies and shallow copies? What are the advantages and disadvantages of each?

Reasons to create a class Model real-world objects Model abstract objects Reduce complexity Isolate complexity Hide implementation details Limit effects of changes Hide global data

Reasons to create a class Streamline parameter passing Make central points of control Facilitate reusable code Plan for a family of programs Package related operations Accomplish a specific refactoring

Classes to Avoid God classes – “There is one God!” Classes that can do anything Classes with data but no behavior Maybe this is just a data member struct in another class. Classes with behavior but not data Avoid classes named after verbs Probably should be a routine in another class.

Language-Specific Issues Behavior of overridden constructors and destructors in an inheritance tree Behavior of constructors and destructors under exception-handling conditions Importance of default constructors (constructors with no arguments) Time at which a destructor or finalizer is called Wisdom of overriding the language's built-in operators, including assignment and equality How memory is handled as objects are created and destroyed or as they are declared and go out of scope

CHECKLIST: Class Quality Abstract Data Types Have you thought of the classes in your program as abstract data types and evaluated their interfaces from that point of view?

CHECKLIST: Class Quality Abstraction Does the class have a central purpose? Is the class well named, and does its name describe its central purpose? Does the class's interface present a consistent abstraction? Does the class's interface make obvious how you should use the class? Is the class's interface abstract enough that you don't have to think about how its services are implemented? Can you treat the class as a black box? Are the class's services complete enough that other classes don't have to meddle with its internal data? Has unrelated information been moved out of the class? Have you thought about subdividing the class into component classes, and have you subdivided it as much as you can? Are you preserving the integrity of the class's interface as you modify the class?

CHECKLIST: Class Quality Encapsulation Does the class minimize accessibility to its members? Does the class avoid exposing member data? Does the class hide its implementation details from other classes as much as the programming language permits? Does the class avoid making assumptions about its users, including its derived classes? Is the class independent of other classes? Is it loosely coupled?

CHECKLIST: Class Quality Inheritance Is inheritance used only to model "is a" relationships—that is, do derived classes adhere to the Liskov Substitution Principle? Does the class documentation describe the inheritance strategy? Do derived classes avoid "overriding" non- overridable routines? Are common interfaces, data, and behavior as high as possible in the inheritance tree? Are inheritance trees fairly shallow? Are all data members in the base class private rather than protected?

CHECKLIST: Class Quality Other Implementation Issues Does the class contain about seven data members or fewer? Does the class minimize direct and indirect routine calls to other classes? Does the class collaborate with other classes only to the extent absolutely necessary? Is all member data initialized in the constructor? Is the class designed to be used as deep copies rather than shallow copies unless there's a measured reason to create shallow copies?

CHECKLIST: Class Quality Language-Specific Issues Have you investigated the language-specific issues for classes in your specific programming language?