Lecture01
description
Transcript of Lecture01
- 1. Object Oriented Software Modeling and DesignCE 350 Abdel-Karim Al-Tamimi, Ph.D. [email_address] http://faculty.yu.edu.jo/altamimi Al-Tamimi 2010
2. Overview
- Object Oriented Concepts
- Object Oriented Programming (OOP) vs. Structured/Procedural Programming
Al-Tamimi 2010 3. Introduction to OOP
- Things in real life areobjects , and an object has both:attributesandbehaviorsassociated with it
- Adamis a member of classPerson , as a member of this class it takes onPerson attributesandbehaviors
Walking, Eating, Sleeping, Eye-Color, Age, Height, Attributes Behaviors 4. OOP Concepts
- An object may have only attributes
-
- Multiple attributes: struct
-
- A single attribute(e.g. integer)
- Attributes = data
- Behavior = functions
- Restricting the access to objects data or functions is calleddata hiding
- Combining data and behavior in the same object is calledencapsulation
5. OOP vs. Procedural Programming Inputs Outputs object OOP Global Data Procedural Function 1 Function 2 Function 4 Function 3 6. Moving from Procedural to OOP
- Proceduralseparatesdata from the data-manipulations done on it
- OOPencapsulatesboth data and its manipulation inside one object
7. Procedural/Structured Programming
- Carry out the required tasks using functions, if the function is too big, break it down to smaller functions
-
- Functional decomposition
- Functions are correlated with each other to form a data flow diagram (DFD)
Input Process Output 8. Structured Programming Students Exams Courses Tutors Add Student Enter for Exams Check Exam Marks Issue Certificate ExpelStudent 9. Structured Programming
- Harder to maintain as the system grows
- The impact of one change can affect many functions, which are hard to trace and to re-test
- To overcome these difficulties in maintaining the code and to perform expected adjustments, object oriented approach was introduced
10. How to Think in Terms of Objects
- Know the difference betweeninterfaceandimplementation
- Think more abstractly
- Give the user the minimal interface possible
11. Object to Object Communications MyObject AnotherObject Data Data MyFunction1 MyFunction2 Function_1 Function_2 12. Object to Object Communications
- Each object provide a specificinterfaceto allow other objects to communicate with it
- The classinterfaceincludes the following:
-
- The method/function name
-
- The inputs passed to the method/function
-
- The return type of the method/function
- Any public data is considered also as a part of the class interface(better use get/set methods)
- By designing the class interfaces , the developerhideextra or sensitive data
13. Object to Object Communications 14. Interface vs. Implementation 15. Interface vs. Implementation
- Only the services the end user needs are represented
-
- Data hiding with the use of encapsulation
- Change in the class implementation should not require change in the class users code
-
- Interface is still the same
- Always provide the minimal interface
- Use abstract thinking in designing interfaces
-
- No unnecessary steps
-
- Implement the steps in the class implementation
16. Interface vs. Implementation Student Age, GPA, +getAge():integer +admit():void +expel():void +getFullName():string attributes methods 17. How to determine the minimum interface possible
- Only what the user absolutely needs
-
- Fewer interfaces as possible
-
- Use polymorphism
- Starts with hiding everything (private)
-
- Only use public interfaces (try not to use public attributes, instead get/set)
- Design your class from the users perspective and what they need (meet the requirements)
18. Determining the Classs Environment
- Determine the class user
-
- What type of technical programmer
- Determine the behavior of the object
- Environmental constraints on objects
-
- Hardware or/and software constraints
- The next step is the implementation, anything that is not apublic interfaceis considered animplementation
19. Concept of Scope
- Local attributes
-
- Local to a specific method
- Object attributes
-
- Attributes shared between object methods
- Class attributes
-
- Attributes share between objects
-
- Starts withstaticidentifier
public class Employee{ static int Count; } 20. Object Attributes vs. Class Attributes 21. Inheritance
- Inheritanceallowsreusingcode, with a better organization and overall management
- Inheritanceallows classes to inherit attributes from other classes, and you can still overwrite common behaviors
- Inheritancecategorizes classes intosuperclasses(parent class) andsubclasses(child class)
22. Inheritance 23. Multiple Inheritance
- Class can inherit multiple classes
- In real life: kids inherit both father and mother attributes and behaviors
- Multiple inheritance is a complex solution that can solve complex problems elegantly
-
- Problem to the designer
-
- Problem to the programming language compiler writer
-
- Java and C# decided not to allow it in the form C++ allows it
- C# uses interfaces to carry on the architecture of methods that a child class should have
24. Polymorphism
- Polymorphismmeans many shapes
- Polymorphismallows a single behavior to have different interfaces, and thus different inner-object behavior
- Polymorphismmeans thatsimilarobjects can respond to thesame messageindifferent manners
- Polymorphismgives the developer more options and different levels ofcustomizationto achieve a certain data manipulation or output
25. Polymorphism Al-Tamimi 2010 +ReturnArea():double Shape +ReturnArea():double Circle +ReturnArea():double Square Area= Length^2 Area= Radius^2Returns a double value 26. Polymorphism Al-Tamimi 2010 +GetArea(Shape) MainClass Will call Shape.ReturnArea() Area= Length^2 Area= Radius^2If shape is Circle If shape is Square 27. Constructors
- Only with objects
- No return value (even void)
- Method name is identical to class name
public Student(int StudentNumber) { /* Implementation Code */ }... Student BestStudent = new Student(1234); ... 28. Constructors
- Default constructor (no arguments)
- Any object will have a constructor even if you do not define it, since it inherits its superclass constructor
-
- Every object is a subclass ofobjectclass
-
- Superclass constructor is called by default
- Always provide a constructor
- Overloading methods/constructors
-
- Same method name, different parameters
-
- Polymorphism : using different objects that have the same superclass (child override parent)
29. Constructors
- Initialize all attributes
- Make sure that the initialization values leaves the object in a stable and safe state
- Do not include implementation in the constructor
-
- Call a method if needed
Student Age, GPA, +Student: +getAge:integer +admit:void +expel:void +getFullName:string 30. Error Handling
- What can be done as a software developer when there is a potential risk of causing an error:
-
- Ignore the problem that might cause the error
-
-
- Very dangerous, even if it has a very low probability
-
-
- Check the problem then abort the program
-
-
- Handle the error by aborting the program, and show the error message
-
-
- Check the problem, and try to fix it
-
-
- You already know how to fix it, may not be the best solution
-
-
- Check the problem and throw an exception
-
-
- Throw the exception to the calling object to handle it
-
31. Exception Handling
- Exception is unexpected event that happens in the system
- We use keywords : tryandcatchandthrowto manage exceptions
try { // potential risky code } catch( Exception object) { // how to solve the exception // can not solve it, throw an exception } 32. Composition
- Compositionis to combine multiple objects to achieve a better or more complex objects
- Example: A computer is composed of different objects : memory, hard drive, motherboard, and each of these objects are composed of smaller objects etc
33. Resources
- Matt Weisfeld,The Object Oriented Thought Process , Sams Publishing, ISBN:0-672-32611-6