Fundamentals of Computer Science The Object-Oriented Paradigm and UML
Object-Oriented versus Structured Paradigm nThe object-oriented paradigm is a response to shortcomings in the structured paradigm. n The structured approach does not work as well for large projects. nThe structured approach requires that you focus on the system’s data or actions individually. nThe object-oriented approach integrates actions and data so that they are both viewed at the same level of importance.
nObjects/Classes are superior because … n They have low coupling and high cohesion. n Data and actions are packaged together. Objects are self-contained. Integrity of the data is easier to maintain. n They support information hiding. They have a well-defined interface. Member data is inaccessible outside of object. n They are easy to reuse. For all the above reasons. Inheritance further enhances reusability. Object-Oriented versus Structured Paradigm
The Object-Oriented Paradigm nObject-oriented programming is a logical extension of the concept of modularity. nThe concept of objects was introduced in nObject-oriented programming began to gain wide spread acceptance in the mid-80’s. nThe object-oriented paradigm facilitates the construction of large systems by promoting high cohesion and low coupling.
The Object-Oriented Paradigm nThe primary benefit of object-oriented design/programming is that it facilitates software reusability. n Inheritance plays an important role in software reusability. nThe transition from the analysis phase and the implementation phase is less severe when the object-oriented approach is used. n Many of the same diagrams used in the analysis phase may also be used, with modification, in the design phase.
nThe object-oriented approach to software design is generally agreed to be superior to the structured approach; especially for large projects. nThe object-oriented paradigm is based on the following four principles: n Abstraction n Encapsulation n Inheritance n Polymorphism nFor a programming language to be object-oriented it must support the above principles. The Object-Oriented Paradigm
Abstraction nWhat is an abstraction? A simplification of reality in which essential features are extracted from a conceptual or real-world entity. nModels are types of abstractions. nWhat are models/abstractions used for? n To represent some physical or conceptual thing that is to be built. n As a plan of attack for solving a problem. n As a blueprint of the desired system. n As a means of communicating complex ideas to others. n As a means of viewing the “big picture”.
Encapsulation nEncapsulation is the act of combining data and the subroutines that act on that data in one entity, usually a class. nThe data is, in most cases, only directly accessible to the subroutines encapsulated with the data. nThe purpose of the subroutines is to protect the integrity of the accompanying data.
Object-Oriented Paradigm for a Bank Account System
Classes and Objects nAn objects is an instances of a class. nA class is... n The most important building block of any object- oriented system. n A description of a set of objects that share the same attributes and behaviors. n A blueprint for creating an object. n An abstraction (simplification) of reality. n A representation of a software thing, a hardware thing, or even a conceptual thing. n Graphically represented in UML as a rectangle.
nAll classes have … n Names - Used to distinguish one class from another. A class must have a name. n Attributes - Member data. n Operations (behaviors) - Member functions. nAttributes and operations are the most important features of a class definition. nA class usually collaborate with other classes. Classes and Objects
Inheritance nThere are different types of relationships that can exist between collaborating classes. nInheritance is … n A type of relationship that can exist between two classes. One of the classes is the base (or parent) class; the other class is the derived (or child) class. n Used to show a ‘kind-of’ relationship. n All OO programming languages support single inheritance; some (C++) also support multiple inheritance. n Graphically represented in UML by a solid line with an open triangular arrowhead on the base class end.
Association nAn association is … n A type of relationship that can exist between one or more classes. n Used to show a ‘knows-a’ relationship. n Either unidirectional or bi-directional. n Graphically represented in UML by a solid line which may optionally be labeled and have a name direction indicator. nAssociation names are verbs or verb phrases. nThe same class can be on both ends of a binary association.
Aggregation nAggregation is … n A relationship between two classes. n A form of association. n Used to show a ‘has-a’ relationship. n Graphically represented in UML as a solid line with an open diamond on the ‘whole’ end. nAggregations and associations are implemented in exactly the same way. The difference is entirely conceptual.
Composition nComposition is … n A relationship between two classes. n A strong form of association. n Used to show a ‘contains-a’ or a ‘whole-part’ relationship. n Graphically represented in UML as a solid line with a solid diamond on the ‘whole’ end. nComposition relationships and associations are not implemented in exactly the same way. nIf the ‘whole’ object is destroyed, the part object will also be destroyed.
Polymorphism nPolymorphism … n Is used in the context of inheritance, where each class in the inheritance hierarchy has a function (or functions) with the same signature. n Is also known as dynamic binding. n Is the technique of deferring until run-time the decision on the function within the hierarchy to call. n Promotes software reusability. n Reduces code complexity.
Class Modeling nHow are the classes that comprise a system chosen? n From scenarios. n From a problem statement. n From CRC (Class, Responsibility, Collaboration) cards. CRC cards are often derived from scenarios and problem statements. n From past experience working with similar systems.
nNoun extraction is a valuable tool in obtaining an initial list of potential classes. n Underline all of the nouns in the problem statement or scenarios. n Refine the list of nouns (potential classes) Remove redundant classes Remove vague classes Remove things best modeled as attributes. Remove irrelevant classes. Class Modeling
Class Modeling - CRC Cards
Overview of the Unified Modeling Language
Overview of UML nUML is a language. n Conforms to specific rules. n Allows the creation of various models. n Does not tell the designer which models need to be created. nUML is a language for visualizing. n UML is a graphical language. n Pictures facilitate communication (a picture is worth a thousand words). nUML is a language for constructing and understanding. n UML supports both forward and reverse engineering.
nUML is a language for documenting design n Provides a record of what has been built. n Useful for bringing new programmers up to speed. n Useful when developing new releases of product. nUML is intended primarily for software- intensive systems Overview of UML
UML Relationship Syntax
UML Class Icon Syntax
nSequence diagrams... n Are one technique used by UML to model the dynamic aspects of a system. n Emphasize the time ordering of message traffic between objects. n Aid the developer visualize the system as it is running. n Are storyboards of selected sequences of message traffic between objects. nAfter class diagrams, sequence diagrams are the most widely used diagrams in UML. nSequence diagrams commonly contain objects and messages. Dynamic Modeling Using UML
nSequence diagrams are build around an X-Y axis. n Objects are aligned (in most cases) at the top of the diagram, parallel to the X axis. n Messages travel (in most cases) parallel to the X axis. n Time passes from top to bottom along the Y axis. n Where a message arrow appears relative to the Y axis and other message arrows, determines the relative time the message is sent. n Sequence diagrams most commonly show relative timings, not absolute timings. n Links between objects are implied by the existence of a message. Sequence Diagrams
UML Sequence Diagram Syntax