Common mistakes Basic Design Principles Amit Shabtay.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

1 Object-Oriented Reengineering © R. Marinescu Lecture 5 Radu Marinescu Principles of Object-Oriented Design.
Inheritance Inheritance Reserved word protected Reserved word super
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Common mistakes Basic Design Principles David Rabinowitz.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
Review Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Review What have we done during the course? Where to learn more? What is for the.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Generic Programming David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
R R R CSE870: Advanced Software Engineering: Frameworks (Cheng, Sp2003)1 Frameworks A Brief Introduction.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
Review David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 Review What have we done during the course? Which topics we have not discussed?
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
TEST-1 7. Object-Oriented Design Principles. TEST-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Advanced Principles I Principles of Object-Oriented Class Design Copyright  by Object Mentor, Inc All Rights Reserved Portions of this material.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Principles of Object-Oriented Design SEI, SJTU WEB APPS and SERVICES.
Ex3 Preview, Swing tutorial Ex1 review Amit Shabtay.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
Design Principles iwongu at gmail dot com.
The benefits of SOLID in software development Ruben Agudo Santos (GS-AIS-HR)
Frameworks & Patterns Use of Organized Classes. Frameworks vs Toolkits Framework Framework  Start with classes and interfaces that define a rudimentary.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Software Design Principles
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
Abstraction ADTs, Information Hiding and Encapsulation.
Chapter 7: The Adapter Pattern. Object Oriented Adapters Suppose that you have existing software. You have outsourced some of your work and there is a.
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1 Design by Principles (Robert Martin) Software Engineering.
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
Principles of Object Oriented Design
PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D. S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle.
SOLID Design Principles
S.Ducasse Stéphane Ducasse 1 Some Principles Stéphane Ducasse ---
1 Good Object-Oriented Design Radu Marinescu Lecture 3 Principles of Object-Oriented Design Part II 19 th November 2002.
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
NCCUCS Software Engineering 補充投影片 April 14, 2014.
14 Jul 2005CSE403, Summer'05, Lecture 09 Lecture 09: Fundamental Principles and Best Practices for Software Design Valentin Razmov.
Appendix 1 - Packages Jim Fawcett copyright (c)
Interface Segregation / Dependency Inversion
OCP and Liskov Principles
Course information Old exam Resit Report Result and walkthrough
Advanced Programming in Java
More Design Heuristics
Inheritance ITI1121 Nour El Kadri.
Interfaces.
Copyright © by Curt Hill
null, true, and false are also reserved.
Subtype Polymorphism, Subtyping vs
Design Tips.
Objects First with Java A Practical Introduction using BlueJ
Principles of Object-Oriented Design
Objects First with Java A Practical Introduction using BlueJ
Principles of Object-Oriented Design Part II
Principles of Object-Oriented Design
Object Oriented Design & Analysis
Some principles for object oriented design
Notes on software design
Chapter 10 – Component-Level Design
Presentation transcript:

Common mistakes Basic Design Principles Amit Shabtay

March 3rd, 2004 Object Oriented Design Course 2 Tirgul Summery Basic design principles Advanced design principles (LSP, …) Intro to eclipse, unit testing, JUnit Generic programming (STL, Java generics) AspectWerkz- AOP framework ODBC,JDBC Exercise previews and reviews

March 3rd, 2004 Object Oriented Design Course 3 Course Requirement Basic understanding of OOD Basic knowledge of C++, Java 3 programming exercises 2 theoretical exercises Exam

Basic Design Principles

March 3rd, 2004 Object Oriented Design Course 5 Common Mistakes Repeated often Especially with the inexperienced Don’t you make them! How to recognize the danger signals?

March 3rd, 2004 Object Oriented Design Course 6 Danger Signals (1) public class Counter { public int howManyA(String s) { int conut = 0; for(int i = 0; i < s.length(); ++i) if(s.charAt(i) == 'a') ++count; return count; } } Is this a class?

March 3rd, 2004 Object Oriented Design Course 7 Danger Signals (2) Class City extends Place { … } Class Jerusalem extends City implements Capital { … } Class TelAviv extends City { … } What is wrong here?

March 3rd, 2004 Object Oriented Design Course 8 Danger Signals (3) Class Person { String getName(); void setName(String name); int getAge(); void setAge(int age); Car getCar(); void setCar(Car car); } What do we see ?

March 3rd, 2004 Object Oriented Design Course 9 Basic Design Principles The Open Closed Principle The Dependency Inversion Principle The Interface Segregation Principle The Acyclic Dependencies Principle These principles and more: post.php?p= &postcount=1

March 3rd, 2004 Object Oriented Design Course 10 The Open Closed Principle Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Existing code should not be changed – new features can be added using inheritance or composition. Which is preferred?

March 3rd, 2004 Object Oriented Design Course 11 Example enum ShapeType {circle, square}; struct Shape { ShapeType _type; }; struct Circle { ShapeType _type; double _radius; Point _center; }; struct Square { ShapeType _type; double _side; Point _topLeft; }; void DrawSquare(struct Square*) void DrawCircle(struct Circle*);

March 3rd, 2004 Object Oriented Design Course 12 Example (cont.) void DrawAllShapes(struct Shape* list[], int n) { int i; for (i=0; i<n; i++) { struct Shape* s = list[i]; switch (s->_type) { case square: DrawSquare((struct Square*)s); break; case circle: DrawCircle((struct Circle*)s); break; } Where is the violation?

March 3rd, 2004 Object Oriented Design Course 13 Correct Form class Shape { public: virtual void Draw() const = 0; }; class Square : public Shape { public: virtual void Draw() const; }; class Circle : public Shape { public: virtual void Draw() const; }; void DrawAllShapes(Set & list) { for (Iterator i(list); i; i++) (*i)->Draw();

March 3rd, 2004 Object Oriented Design Course 14 The Dependency Inversion Principle A. High level modules should not depend upon low level modules. Both should depend upon abstractions. B. Abstractions should not depend upon details. Details should depend upon abstractions.

March 3rd, 2004 Object Oriented Design Course 15 Example void Copy() { int c; while ((c = ReadKeyboard()) != EOF) WritePrinter(c); } Where is the violation?

March 3rd, 2004 Object Oriented Design Course 16 Example (cont.) Now we have a second writing device – disk enum OutputDevice {printer, disk}; void Copy(outputDevice dev) { int c; while ((c = ReadKeyboard()) != EOF) if (dev == printer) WritePrinter(c); else WriteDisk(c); }

March 3rd, 2004 Object Oriented Design Course 17 Correct form class Reader { public: virtual int Read() = 0; }; class Writer { public: virtual void Write(char)=0; }; void Copy(Reader& r, Writer& w) { int c; while((c=r.Read()) != EOF) w.Write(c); }

March 3rd, 2004 Object Oriented Design Course 18 The Interface Segregation Principle The dependency of one class to another one should depend on the smallest possible interface. Avoid “fat” interfaces Example: Word toolbars

March 3rd, 2004 Object Oriented Design Course 19 The Interface Segregation Principle

March 3rd, 2004 Object Oriented Design Course 20 The Interface Segregation Principle

March 3rd, 2004 Object Oriented Design Course 21 Example class Door { public: virtual void Lock() = 0; virtual void Unlock() = 0; virtual bool IsDoorOpen() = 0; }; class Timer { public: void Regsiter(int timeout, TimerClient* client); }; class TimerClient { public: virtual void TimeOut() = 0; };

March 3rd, 2004 Object Oriented Design Course 22 A Timed Door A violation?

March 3rd, 2004 Object Oriented Design Course 23 Correct Form Two options: Adapter Multiple Inheiritence ? In which language?

March 3rd, 2004 Object Oriented Design Course 24 The Acyclic Dependencies Principle The dependency structure between packages must not contain cyclic dependencies.

March 3rd, 2004 Object Oriented Design Course 25 Example

March 3rd, 2004 Object Oriented Design Course 26 Correct Form

March 3rd, 2004 Object Oriented Design Course 27 The Law Of Demeter Only talk to your immediate friends. In other words: You can play with yourself. ( this.method() ) You can play with your own toys (but you can't take them apart). ( field.method(), field.getX() ) You can play with toys that were given to you. ( arg.method() ) And you can play with toys you've made yourself. ( A a = new A(); a.method() )

March 3rd, 2004 Object Oriented Design Course 28 Example

March 3rd, 2004 Object Oriented Design Course 29 How to correct

March 3rd, 2004 Object Oriented Design Course 30 Example Code

March 3rd, 2004 Object Oriented Design Course 31 Resources A nice resources page for OOD: About the principles (same site): g/OOPrinciples

March 3rd, 2004 Object Oriented Design Course 32 Package cohesion The Common Closure Principle Classes within a released component should share common closure. That is, if one needs to be changed, they all are likely to need to be changed. The Common Reuse Principle The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.