Good Design of Classes Classes let us create Abstract Data Types Should be well aware of benefits – Hide implementation details – Localize changes – Interface.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Software Engineering Class design. Class design – ADT Abstract Data Types:  Why use ADT? Hidden implementation details Changes do not affect whole program.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
OBJECT ORIENTED PROGRAMMING M Taimoor Khan
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
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.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
OOP Languages: Java vs C++
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Object-Oriented Design. A Historical Perspective Programs  Procedures to manipulate data Earlier: procedural  Think actions/procedures first, data second.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Peyman Dodangeh Sharif University of Technology Fall 2014.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
Object-Oriented Principles Applications to Programming.
Object Oriented Programming
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
GoodOO Programming Practice in Java © Allan C. Milne v
ISBN Object-Oriented Programming Chapter Chapter
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
ITEC0724 Modern Related Technology on Mobile Devices Lecture Notes #2 1.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance ndex.html ndex.htmland “Java.
STATICS /. When static?? When a method’s behaviors has no dependency on state of an object i.e.., when the method will never be instance specific. E.g..,
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Best Practices in the Object-Oriented Design Vesko Kolev Telerik Corporation
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Working Classes SCMP Special Topic: Software Development
Copyright © Jim Fawcett Spring 2017
OOP: Encapsulation &Abstraction
Creating and Using Classes
Lecture 22 Inheritance Richard Gesick.
More Object-Oriented Programming
Java Programming, Second Edition
Fundaments of Game Design
Object-Oriented Programming
CIS 199 Final Review.
Java Programming Language
Lecture 10 Concepts of Programming Languages
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Good Design of Classes Classes let us create Abstract Data Types Should be well aware of benefits – Hide implementation details – Localize changes – Interface can be more informative – More self-documenting – Don’t have to pass data all over your program – Work with real-world entities rather than low-level implementation structures By now you should know all about building classes

Anything wrong with this class? class Program { public void InitializeStack(); public void PushCommand(Command c); public Command PopCommand(); public ShutdownCommandStack(); public void InitializeReportFormatting(); public void FormatReport(Report r); public void PrintReport(Report r); public void InitializeGlobalData(); public void ShutdownGlobalData(); … }

Anything wrong with this class? class EmployeeCensus { public void AddEmployee(Employee e); public void RemoveEmployee(Employee e); public Employee NextItemInList(); public Employee FirstItem(); public Employee LastItem(); … } Be sure you understand what abstraction the class is implementing

Designing Classes Provide services in pairs with their opposites – E.g. Get/Set, Add/Remove Move unrelated information to another class Beware of erosion of the interface’s abstraction under modification class Employee { public string GetName(); public Address GetAddress(); … public boolean isJobActive(JobClass j); … public SqlQuery GetQueryToCreateEmployee(); … }

Designing Classes Don’t expose member data in public Don’t assume the class’s users know how to correctly use the class – // Initialize x, y and z to 1.0 because … Avoid friend classes – Probably an indicator of classes that are too tightly coupled anyway Favor read-time convenience to write-time convenience

Class Containment Containment = Data Members Variables should be “Has A” relationship Implement “Has A” via inheritance as a last resort (i.e. using access to protected members) Be critical of classes with more than about seven data members

Class Inheritance Implements “Is A” relationship Design and document for inheritance or prohibit it – Non-virtual in C++, final in Java, non-overridable in VB so can’t inherit from it Move common interfaces, data, and behavior as high as possible in the inheritance tree Be suspicious of classes with only one instance Be suspicious of base classes with only one derived class Be suspicious of classes that override a routine but do nothing in the derived routine – E.g. base Cat class with Scratch() but some cats are declawed and can’t scratch

Class Inheritance Be suspicious of deep inheritance trees – Found to be significantly associated with increased fault rates Prefer polymorphism to type checking with switch statements – But not in all cases, e.g.: switch (ui.command() ) { case Command_OpenFile: OpenFile(); break; case Command_Print: Print(); break; … } switch (shapeType) { case Circle: DrawCircle(); break; case Square: DrawSquare(); break; … }

Member Functions and Data Keep the number of routines in a class as small as possible Disallow implicitly generated member functions and operators you don’t want – E.g. to disallow =, you could define it and make it private Minimize the number of different routines called by a class – fan out – One study found the number of faults was correlated with the total number of routines that were called from within the class Minimize indirect routine calls to other classes – E.g. account.ContactPerson().GetDaytimeInfo().PhoneNumber(); In general minimize the extent a class collaborates with other classes

Constructors Initialize all member data in constructors if possible Enforce the singleton property by using a private constructor public class MaxID { // Private members private static final MaxID m_instance = new MaxID(); // Private constructor private MaxID() { … } … // Public routines public static MaxID getInstance() { return m_instance; } MaxID.getInstance()// Access reference to singleton

Constructors For copy constructors or overloaded assignment operators, prefer deep copies to shallow copies – Even if it may be more inefficient, often leads to reference problems

Classes to Avoid Avoid creating god classes – Omniscient classes that are all-knowing and all powerful – Consider whether that functionality might be better organized into those other classes Eliminate irrelevant classes – E.g. class consisting only of data but no behavior; could be demoted to attribute of another class Avoid classes named after verbs – Only behavior but no data could be a routine of another class