3100 SYSC - System Analysis and Design Basic Concepts
Transcription
3100 SYSC - System Analysis and Design Basic Concepts
SYSC 3100 - System Analysis and Design Basic Concepts Introduction to OO Development and Software Engineering Principles Everything is an Object! • You, me, your neighbour, desks, chairs, doors, etc. • Some of these things are the same, and some of them are different. • Some of them are “active”, and others are “passive”. Objectives • • • • • Define objects, attributes and operations. Differentiate between OOA, OOD and OOP (!) Describe a Class and an object. Define information hiding and encapsulation. Define generalization and specialization and the isA rule. • Define polymorphism and over-riding. • Describe active objects and persistence. Object-Oriented Development (OOD) • Objects are abstractions of real-world or system entities and manage themselves • Objects are independent and encapsulate state and representation information (attributes). • System functionality is expressed in terms of object services (operations) • Shared data areas are eliminated. Objects communicate by message passing • Objects may be distributed and may execute sequentially or in parallel Objects, Attributes, Operations Bicycle objects Bicycle class abstract into Polygon objects Attributes frame size wheel size number of gears material Operations shift move repair Polygon class abstract into Attributes vertices border color fill color Operations draw erase move O-O World View • An object-oriented system is regarded as a network of cooperating objects which - interact by sending each other messages - maintain their own state - have an individual identity o4: C4 o1: C1 o3:C3 state o1 ops1() state o3 state o4 ops3 () ops4 () o2: C3 o6: C1 state o2 ops3 () state o6 ops1 () o5:C5 state o5 ops5 () Advantages of OOD • Easier maintenance. Classes may be understood as stand-alone entities, including their own functionality and data • Classes are appropriate reusable components • For some systems, there may be an obvious mapping from real world entities to system classes, e.g., accounts in banking system Object-oriented development • Object-oriented analysis (OOA), design (OOD) and programming (OOP) are related but distinct • OOA is concerned with developing an object model of the application domain • OOD is concerned with developing an objectoriented system model (structure, behaviour) to implement requirements • OOP is concerned with realising an OOD using an OO programming language such as Java or C++ Principal Concepts – – – – – – – – – Objects, classification encapsulation and information hiding modularity generalization and inheritance typing polymorphism dynamic binding concurrency persistence Classes and Objects •Object –A chunk of structured data in a running software system –Has properties • Represent its state –Has behaviour • How it acts and reacts • May simulate the behaviour of an object in the real world –Has an identity: each object is unique High-level Examples Class Identity Behaviour State Person. ‘Hussain Pervez.’ Speak, walk, read. Studying, resting, qualified. Shirt. My favourite button white denim shirt. Shrink, stain, rip. Pressed, dirty, worn. Sale. Sale no #0015, 16/06/02. Earn loyalty points. Invoiced, cancelled. Bottle of ketchup. This bottle of ketchup. Spill in transit. Unsold, opened, empty. Objects Jane: date of birth: 1955/02/02 address: 99 UML St. position: Manager Greg: Savings Account 12876: balance: 1976.32 opened: 1997/03/03 date of birth: 1970/01/01 address: 75 Object Dr. Margaret: Mortgage Account 29865: date of birth: 1980/03/03 address: 150 C++ Rd. position: Teller balance: 198760.00 opened: 2000/08/12 property: 75 Object Dr. Transaction 487: Instant Teller 876: amount: 200.00 time: 2001/09/01 14:30 location: Java Valley Cafe Classes •A class: –Is a unit of abstraction in an object oriented analysis, design, or program –Represent instances of real-world and system entities –Represents similar objects • Its instances –Is a kind of software module • Describes its instances’ structure (properties) • Contains operations/methods to implement their behaviour Classes and Objects: Example Person JoeSmith:Person MarySharp:Person name: string birthdate: date name=“Joe Smith” birthdate=21 October 1983 name=“Mary Sharp” birthdate=16 March 1950 Class with Attributes Objects with Values File Person name birthdate changeJob changeAddress fileName sizeInBytes lastUpdate print GeometricObject color position move (delta : Vector) select (p : Point): Boolean rotate (in angle : float = 0.0) Encapsulation and Information Hiding Encapsulation: The technique of hiding details which are not needed by the user of an abstraction Applied in object-oriented systems by the separation of object interfaces and bodies A_STACK client’s view interface PUSH POP body Message-passing and Encapsulation ‘Layers of an onion’ model of an object: Message from another object requests a service. Operation called only via valid operation signature. An outer layer of operation signatures… …gives access to middle layer of operations… …which can access inner core of data Data accessed only by object’s own operations. An object’s data is hidden (encapsulated). Object-Oriented Computation Model system operation control flow data flow #1 draw #2 insert print Attributes #1 remove update Attributes #4 #3 insert #4 clear Attributes #2 Attributes #3 remove draw-line Organizing Classes into Inheritance Hierarchies •Superclasses –Contain features common to a set of subclasses •Inheritance hierarchies –Show the relationships among superclasses and subclasses •Inheritance –The implicit possession by all subclasses of features defined in its superclasses An Example Inheritance Hierarchy Account SavingsAccount ChequingAccount MortgageAccount •Inheritance –The implicit possession by all subclasses of features defined in its superclasses Generalization and Specialization • Classification is hierarchic in nature • Generalization: the technique of factoring out common properties into shared classes – facilitates reuse and change control – the reverse operation is the specialization • For example, a person may be an employee, a customer, a supplier of a service • An employee may be paid monthly, weekly or hourly • An hourly paid employee may be a driver, a cleaner, a sales assistant Specialization Hierarchy More general (superclasses) Person Employee monthly paid Customer weekly paid Driver Cleaner Supplier hourly paid Sales assistant More specialized (subclasses) The Isa Rule •Always check generalizations to ensure they obey the “isa” rule –“A checking account is an account” –“A village is a municipality” •Should ‘Province’ be a subclass of ‘Country’? –No, it violates the isa rule • “A province is a country” is invalid! Inheritance hierarchy of mathematical objects MathematicalObject Shape Shape2D Ellipse Polygon Circle Quadrilateral Rectangle Point Matrix Shape3D Line Plane Make Sure all Inherited Features Make Sense in Subclasses Account ba lance op ened creditOrOverdra ftLi mit credit de bit calculateInterest SavingsAccount ChequingAccount hi ghes tChequ eNumbe r with drawUsing Ch eque calculateServiceCharge MortgageAccount colla teral Property colla teral Valu e se tColla teral Valu e Methods, Operations and Polymorphism •Operation –A higher-level procedural abstraction that specifies a type of behaviour –Independent of any code which implements that behaviour • E.g., calculating area (in general) –Operations are defined during Analysis and Design –They are implemented with methods Methods, Operations and Polymorphism •Method –A procedural abstraction used to implement the behaviour of a class. –Several different classes can have methods with the same name • They implement the same abstract operation in ways suitable to each class • E.g, calculating area in a rectangle is done differently from in a circle Polymorphism •A property of object oriented software by which an abstract operation may be performed in different ways in different classes. –Requires that there be multiple methods of the same name –The choice of which one to execute depends on the object (i.e., type) that is in a variable, on which the method is executed –Reduces the need for programmers to code many if-else or switch statements Overriding •A method would be inherited, but a subclass contains a new version instead –For restriction • E.g. scale(x,y) would not work in Circle –For extension • E.g. SavingsAccount might charge an extra fee following every debit –For optimization • E.g. The getPerimeterLength method in Circle is much simpler than the one in Ellipse How a decision is made about which method to run 1. 2. 3. 4. If there is a concrete method for the operation in the current class, run that method. Otherwise, check in the immediate superclass to see if there is a method there; if so, run it. Repeat step 2, looking in successively higher superclasses until a concrete method is found and run. If no method is found, then there is an error – In Java and C++ the program would not have compiled Dynamic binding •Occurs when decision about which method to run can only be made at run time –Needed when: • A variable is declared to have a superclass as its type, and • There is more than one possible polymorphic method that could be run among the type of the variable and its subclasses Abstract Classes and Methods •An operation should be declared to exist at the highest class in the hierarchy where it makes sense – The operation may be abstract (lacking implementation) at that level – If so, the class also must be abstract • No instances can be created • The opposite of an abstract class is a concrete class – If a superclass has an abstract operation then its subclasses at some level must have a concrete method for the operation • Leaf classes must have or inherit concrete methods for all operations • Leaf classes must be concrete Concurrency •The nature of objects as self-contained entities make them suitable for concurrent implementation. •There are two forms of objects in concurrent systems. •Passive: –operations execute only in response to external stimuli –call other objects only in response to external stimuli •Active –operations may execute asynchronously –has an independent thread of control –call other objects “spontaneously” –change state “spontaneously” Active transponder object • Active objects may have their attributes modified by operations but may also update them autonomously using internal operations • Transponder object broadcasts an aircraft’s position. The position may be updated using a satellite positioning system. The object periodically update the position by triangulation from satellites An active transponder object class Transponder extends Thread { Position currentPosition ; Coords c1, c2 ; Satellite sat1, sat2 ; Navigator theNavigator ; public Position givePosition () { return currentPosition ; } public void run () { while (true) { c1 = sat1.position () ; c2 = sat2.position () ; currentPosition = theNavigator.compute (c1, c2) ; } } } //Transponder Persistence • The ability of an object to exist after the termination of the program which created it • Allows data storage in terms of objects (e.g. files are objects) • Provides a model for interaction with a DBMS or FMS • E.g., Java serialization, Object-oriented DBMS OO program Database Summary • An object is some “thing” which has attributes (variables) and operations that they can perform • A class is the blueprint or template that describes objects that have similar characteristics. • Big idea: hide implementation of objects to make it easy to change without affecting the world. • Polymorphism allows different types to implement common operations differently. • “Active objects” operate spontaneously and independently of each other (like us!).