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

Slides:



Advertisements
Similar presentations
Software Engineering Key design concepts Design heuristics Design practices.
Advertisements

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.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
1 Objects and ClassesStefan Kluth 1.6Objects and Classes 1.6What is an Object? 1.7Objects and Classes 1.8Object Interface, Class Inheritance, Polymorphism.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
Object-Oriented PHP (1)
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Object-oriented Programming Concepts
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
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.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Objected Oriented Programming & Design JAVA Shishir Gupta (704) (704)
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Good Design of Classes Classes let us create Abstract Data Types Should be well aware of benefits – Hide implementation details – Localize changes – Interface.
Object Oriented Software Development
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
ISBN Object-Oriented Programming Chapter Chapter
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
ITEC0724 Modern Related Technology on Mobile Devices Lecture Notes #2 1.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
Working Classes SCMP Special Topic: Software Development
Classes (Part 1) Lecture 3
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Object Based Programming
Subprograms and Programmer Defined Data Type
Lecture 22 Inheritance Richard Gesick.
Object-Oriented PHP (1)
Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Presentation transcript:

Software Engineering Class design

Class design – ADT Abstract Data Types:  Why use ADT? Hidden implementation details Changes do not affect whole program More informative interfaces Easier to improve performance Programs easier to verify “Self-documenting” programs “Higher level programming”

Class design – ADT Abstract Data Types:  ADT = abstract (mathematical) model + operations defined on it  Class = ADT + inheritance + polymorphism

Class design – interfaces Abstraction Encapsulation

Class design – interfaces Abstraction:  Example of good abstraction (C++): class Student { public: Student(); Student( FullName name, String address, String studentID ); virtual ~Student(); FullName GetName() const; String GetAddress() const; String GetStudentID() const;... private:... };

Class design – interfaces Abstraction:  Example of bad abstraction (C++): class StudentList: public ListContainer { public:... void AddStudent(Student student); void RemoveStudent(Student student);... Student NextListItem(); Student FirstItem(); Student LastItem();... private:... }; Different levels of abstraction

Class design – interfaces Abstraction: guidelines to build good interfaces  Present a consistent level of abstraction in the class interface – each class should implement one and only one ADT Heuristic test for inheritance relations: is inheritance being used only for “is a” relationships? (Answer should be YES)

Class design – interfaces Abstraction: guidelines to build good interfaces  Be sure you understand what abstraction the class is implementing

Class design – interfaces Abstraction: guidelines to build good interfaces  Provide services in pairs with their opposites – add/remove, activate/deactivate, on/off,...

Class design – interfaces Abstraction: guidelines to build good interfaces  Move unrelated information to separate classes – if you have “isolated” data and routines within a class, they should form a separate class

Class design – interfaces Abstraction: guidelines to build good interfaces  Make interfaces programmatic rather than semantic whenever possible programmatic = compiler can check semantic = e.g. “RoutineA must be called before RoutineB”

Class design – interfaces Abstraction: guidelines to build good interfaces  Beware of “erosion” of the interface´s abstraction under modification

Class design – interfaces Abstraction: guidelines to build good interfaces  Do not add public members that are inconsistent with the interface abstraction

Class design – interfaces Abstraction: guidelines to build good interfaces  Abstraction and cohesion come together and are strongly correlated

Class design – interfaces Encapsulation: guidelines to build good interfaces  Minimise accessibility of classes and members - “private” is better than “protected”, and both are better than “public” goal is to preserve the integrity of the interface abstraction

Class design – interfaces Encapsulation: guidelines to build good interfaces  Do not expose member data in public float x; float y; float z; Bad design: data (and their representation) are exposed to external manipulation float GetX(); float GetY(); float GetZ(); void SetX(float x); void SetY(float y); void SetZ(float z); Good design: internal data (and how they are represented, and where they are stored, etc.) are protected from external manipulation

Class design – interfaces Encapsulation: guidelines to build good interfaces  Avoid putting private implementation details into a class interface

Class design – interfaces Encapsulation: guidelines to build good interfaces  Do not make ANY assumption about the class users

Class design – interfaces Encapsulation: guidelines to build good interfaces  Avoid “friend classes”

Class design – interfaces Encapsulation: guidelines to build good interfaces  Do not put a routine into the public interface just because it uses only public routines

Class design – interfaces Encapsulation: guidelines to build good interfaces  Give priority to read-time convenience over write-time convenience – code is read much more than written. When writing code, make it good to be read, even if it demands more work to be written

Class design – interfaces Encapsulation: guidelines to build good interfaces  Beware of semantic violations of encapsulation Some examples of semantic violations of encapsulation: Not calling Initialise(), because Operation() calls it Not calling Terminate(), because LastOperation() calls it Not calling Database.Connect(), because Retrieve() calls it Using MAX_ROWS instead of MAX_COLUMNS because you know that every table has same number of rows and columns

Class design – interfaces Encapsulation: guidelines to build good interfaces  Beware of tight coupling

Class design & implementation Containment (“has a”):  “has a” should be always implemented through containment  Sometimes, it may be necessary to implement “has a” through private inheritance. This should be considered bad practice, as it leads to tight coupling and violates encapsulation

Class design & implementation Containment (“has a”):  Be critical of classes that contain more than about seven data members SEVEN = heuristic magic number

Class design & implementation Inheritance (“is a”):general considerations  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, will the data member be visible to derived classes?

Class design & implementation Inheritance (“is a”):general considerations  Implement “is a” through public inheritance Be, however, rigorous about implementing through public inheritance strictly “is a” relationships

Class design & implementation Inheritance (“is a”):general considerations  Design and document for inheritance, or prohibit it C++: non-virtual Java: final etc.

Class design & implementation Inheritance (“is a”):general considerations  Liskov substitution principle: all the routines defined in the base class should mean the same thing when they are used in each of the derived classes

Class design & implementation Inheritance (“is a”):general considerations  Do not reuse names of non-overridable base-class routines in derived classes

Class design & implementation Inheritance (“is a”):general considerations  Move common interfaces, data and behaviour as high as possible in the inheritance tree

Class design & implementation Inheritance (“is a”):general considerations  Be suspicious of classes of which there is only one instance: should it be an object instead of a class?

Class design & implementation Inheritance (“is a”):general considerations  Be suspicious of classes of which there is only one derived class

Class design & implementation Inheritance (“is a”):general considerations  Be suspicious of classes that override a routine and do nothing inside the derived routine operation()... operation() // empty body operation()...

Class design & implementation Inheritance (“is a”):general considerations  Avoid deep inheritance trees – usually, more than three levels of inheritance suggest overly complex design

Class design & implementation Inheritance (“is a”):general considerations  Prefer polymorphism to extensive type checking  Make all data private, not protected

Class design & implementation Inheritance (“is a”):general considerations  Multiple inheritance can be powerful, but it also can make the program too complex. If possible, avoid it  Inheritance (in general) is very powerful, but should always be used with care, so as not to increase program complexity unnecessarily

Class design & implementation Member functions and data:  Keep the number of routines in a class as small as possible  Disallow implicitly generated member functions and operators you do not want  Minimise the number of different routines called by a class  Minimise indirect routine calls to other classes – such as rout1.Rout2().Rout3().Rout4()

Class design & implementation Member functions and data:  In general, minimise the extent to which a class collaborates with other classes – try to minimise:  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 classes? Model real-world objects Model abstractions of real-world objects Reduce program complexity Isolate complexities Hide implementation details Limit effects of change

Why classes? (cont.) Streamline parameter passing Facilitate reusable code Plan for a family of programs Package related operations