Oosad 04


Published on

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Oosad 04

  1. 1. Object Oriented Concepts Chapter 04
  2. 2. The four basic OO concepts• Class – software abstraction of an object, including data and functionality• Object – person, place, thing, event, concept, screen, or report• Attribute - variable• Method – function or procedure
  3. 3. • It is optional in UML to show attributes and methods of a class• There are two types of attributes – Instance attributes – applicable to a single object – Static attributes – applicable to all instances of a single class – Underlined• Instance methods – operate on a single instance – Enroll a student• Static methods – operate on all instances – Search
  4. 4. Abstraction, Encapsulation and Information Hiding• Abstraction – Process of including features, attributes and methods that are of interest to your application and ignore the rest – It is an analysis issue that deals with what a class knows or does – Depends on the context in which you define the abstraction – The act of defining the interface of something – Act of painting a clear box around something
  5. 5. Encapsulation• Deals with – how you intend to modularize the features of a system. – How functionality is included is compartmentalized within a system• You can build anything you want and then you can later change the implementation and it will not affect other components within the system• Act of painting the box black• Define something is going to be done, but you are not telling the rest of the world how you are going to do it
  6. 6. Information Hiding• Restricting access to data attributes and some methods, to make your application maintainable• If one class wants information about another class, it should have to ask for it, instead of taking it.• Prevents highly coupled codes
  7. 7. Inheritance• Two or more classes often share the same attributes and/or methods• Don’t need to write same code repeatedly• Models “is a” and “is like” relationships• Enables you to reuse existing data and code easily• E.g. – STUDENTS have names, addresses and telephone numbers, vehicles • PROFESSORS have also the same• Create a class PERSON having those attributes and make STUDENT and PROFESSOR inherit from it• Everything a superclass knows the subclass knows or does it for free
  8. 8. Modeling Inheritance• Line with closed arrowhead Class A (the super class)• B inherits from A Class B (The subclass)
  9. 9. Inheritance Tips and Techniques• Look for similarities• Look for existing classes• Follow the sentence rule• Avoid implementation inheritance
  10. 10. Single and Multiple Inheritance• Single – when a class inherits from only one other class, we call this single inheritance• Multiple – when a class inherits from two or more other classes
  11. 11. Single Inheritance Airplane Vehicle maximumAltitude maximumFuel maximumFuel maxiumumNumberOfPassengers maximumSpeed speed maximumNumberOfPassenger s accelerate numberOfEngines Decelerate speed accelerate bank climb decelerate decend Airplane Car Car maximumAltitude make maximumSpeed modelmake numberOfEnginesmaximumFuelmaxiumumNumberOfPassengersmodel bankspeed turn Climb decendacceleratedecelerateturn
  12. 12. FishwingSpantype Multiple Inheritancenameeat Lizardfly Fish maximumSpeed wingSpan scaleColors Type numberOfLegsLizard eat eatnumberOfLegs fly flyscaleColorsmaximumSpeedeatfly AmphibianAmphibian namemaximumSpeed jumpname swimnumberofLegsscaleColorsjumpSpan jumpeatswim
  13. 13. Abstract and Concrete Classes• Abstract classes are in italics• Difference – objects are instantiated from concrete classes, but not from abstract classes• Your software will instantiate airplane and car objects but will never create vehicle objects
  14. 14. Association• Exist between objects – Students TAKE courses, professors TEACH courses – Criminals ROB banks etc…• Identify the relationship and describe it – Students TAKE courses • How many students?, How many courses? Etc – Identify the Cardinality and Optionality • Multiplicity
  15. 15. Modeling Associations UML multiplicity indicators• Thin line connecting two classes Indicator Meaning• Depict three things 0..1 Zero or one – Label 1 One only – Cardinality 0..* Zero or more – Role 1..* One or more n Only n (where n>1) Zero to n (where 0..n n>1) One to n (where label 1..n Cardinality A n>1) Cardinality B Class A role A role B Class B
  16. 16. 0..* 1..* 0..* consists of 1 Student student registers Semester Courseteaching 0..1 0..*assistant assists manager 0..1 0..1 holds 1 Employee Position manages 0..* staff member
  17. 17. How associations are Implemented• Associations are maintained through combination of attributes and methods• The attributes store the information necessary to maintain the relationship and methods keep the attributes current• Students would have an attribute takes, array that stores information of courses the student is currently taking – Add Course, Remove Course• Course may have attribute student – Add Student, Remove Student
  18. 18. Aggregation• Models “is part of” association – An airplane is made up of fuselage, wings, engines, landing gear, flaps and so on. – A delivery Shipment contains one or more packages• Modeling aggregation – Diamond , also model multiplicity and roles – Two way association – Two flavors • Hollow diamond – • Solid diamond – indicates composition, strong form
  19. 19. Aggregation tips and techniques• Apply the sentence rule• It should be part in the real world• You should be interested in the part• Show multiplicity and roles• Aggregation is inherited
  20. 20. Collaboration & Messages• Collaboration occurs between objects when one object asks another for information or to do something.• Eg. The airplane collaborates with its engines to fly• Objects collaborate with one another by sending each other messages• Message is either a request to do something or a request for information• Messages are modeled in UML sequence diagram and UML collaboration diagrams• Discussed Later
  21. 21. Collaboration Tips and Techniques• Some sort of association must exist• A corresponding method must exist in the target object• There might be a return value• There may or may not be parameters• Messages show collaboration, not data flows• Sometimes the target needs to collaborate• Each method should do something• An object can collaborate with itself
  22. 22. aStudent: Student :Seminar :Course enrollStudent(aStudent) isStudentEligible(aStudent) GetSeminarHistory() seminarHistory EligibilityStatus EnrollmentStatus ()
  23. 23. Persistence• How to store objects to permanent storage• The values of the objects as well as information needed to maintain the relationships must be saved permanently• Also concerned with their retrieval and deletion• Two types of objects exist: Persistent and Transient• Persistent: Customer• Transient: Customer editing screen
  24. 24. Persistence Tips and Techniques• Business/domain classes are usually persistent• User Interface classes are usually transitory• You need to store both attributes and association
  25. 25. Persistent Vs Transitory Associations• Persistent associations are those that are permanent – Information to maintain it is saved – Eg. TAKE, TEACH• Transitory associations are not saved to permanent storage and usually involve at least one transitory objects – If you are not persisting the object then you wont be persisting its relationship with other objects
  26. 26. Class Exercise• Explain classes, attributes, methods and associations in your projects• What is multiplicity – Give example• When do we include Roles when we model associations, give example from your project point of view• Describe the implementation of associations in your project• Give two examples of flavors of aggregation• Give example for one rule in aggregation• What is collaboration and how is collaboration achieved?• What is the difference between persistence and transient objects, associations?
  27. 27. Customer 0..n 0..n Product 0
  28. 28. Exercise 2 a. To the whole object1. Object b. “Is part of” associations2. Class c. Result of instantiation3. Method d. Line with closed arrowhead4. Instance e. Definition of a object5. Static f. Asking for information6. Inheritance g. Function and procedure7. Association h. Line with diamond8. Collaboration9. Role i. Optional in association10. Label j. Object can be instantiated11. Abstract classes k. Thin line12. Concrete classes l. Access restriction13. Modeling inheritance m. Paint a box black14. Modeling aggregation n. Relation15. Modeling association o. Object cant be instantiated16. Information hiding p. Paint a box clear17. encapsulation q. To one object18. abstraction r. In case of recursion
  29. 29. Coupling• Measure of how much two items such as classes or methods are interrelated – Coupled: when one class depends on another class – Loosely Coupled: when one class interacts with another class but doesn’t know any of the implementation details of the other class – Highly Coupled: when one class relies on the implementation of the other class• High coupling is a major reason for maintenance• Increasing the coupling between two items may make sense for performance reasons
  30. 30. Coupling Tips and Techniques• Avoid high coupling if you can• Document high coupling thoroughly
  31. 31. Cohesion• Measure of how much an item, such as a class or method, makes sense• Method - Highly cohesive – it does one thing and one thing only – Enroll a student in a course• Small and cohesive methods that do one thing and on thing only are easier to understand and maintain• Highly cohesive class – one type of object and one type of object only – University Information System – Professors instead of employees
  32. 32. Polymorphism• You can treat instances of various classes the same way within your system.• You can send a message to an object without first knowing what type it is and the object will still do “the right thing”, at least from its point of view.• Eg. The poker game – Darn polymorphism• The different objects respond to the message in their own way• Eg: Polymorphism at the University – How the university handles the hiring of a new staff
  33. 33. Interface• Definition of a collection of one or more operation signatures• Implemented by classes and components• To implement an interface, a class or component must include methods that have the operation signatures defined by the interface• Eg. Class STUDENT implements the “Serializable” interface and the “Searchable” interface – The Lollipop notation – The box notation
  34. 34. Components• Modular, extensible unit of independent deployment that has contractually specified interfaces(s) and explicitly defined dependencies• Components should be modular, extensible, and open – Modular – components contains everything it needs to fulfill its responsibilities – Extensible – a component can be enhanced to fulfill more responsibilities than it was originally intended to – Open – it can operate on several platforms and interface with other components through a single programming interface• Component diagrams show the software components that make up a larger piece of software, their interfaces, and their interrelationships.• For the sake of our discussion, a components may be any large-grain item