Docstoc

Object Oriented Software Development What is object oriented

Document Sample
Object Oriented Software Development What is object oriented Powered By Docstoc
					                                                                                                                     9/5/2011




    Object Oriented Software
                Development

        j             programming basics
2. C# object oriented p g       g




What is object oriented
programming?
  A programming approach which uses objects
  An object is a software component which has
  properties and behaviour
     j
  Objects often model real-world entities
  Most modern programming languages
  support object orientation
    C#, Java, VB.NET, C++, PHP, etc.



                  Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                                 2




Why object oriented?
  simplicity: software objects model real world
  objects, so the complexity is reduced and the
  program structure is very clear
  modularity: each object forms a separate
  entity whose internal workings are decoupled
  from other parts of the system
  modifiability: changes inside one part of the
  program do not affect any other part


                  Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                                 3




                                                                                                                           1
                                                                                                                   9/5/2011




Why object oriented?
 extensibility: adding new features can be
 solved by introducing a few new objects and
 modifying some existing ones
 maintainability: objects can be maintained
 separately, making locating and fixing
 problems easier
 re-usability: objects can be reused in
 different parts of a program, or in different
 programs as code libraries, no need to
 duplicate code
                Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                               4




Where should we use it?
 Many kinds of software application can
 benefit from object oriented approach
 GUI applications, web applications, games,
 etc.
 OO allows clean separation of different parts
 of an application
   Domain (entities, business rules)
   User interface
   Data storage

                Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                               5




Objects
 An entity, or thing, is represented as an
 object in the program
 e.g. an object representing an Employee in a
 company
 Objects have attributes to represent state of
 object, e.g. Name, Location of an Employee
 Objects have methods to define the
 behaviour which object can perform, e.g. an
 Employee could Move to a new location
                Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                               6




                                                                                                                         2
                                                                                                                 9/5/2011




Encapsulation
 We often use something without needing to
 know the details of how it works inside
 To turn a car, you turn the steering wheel
 Don’t need to know the details of how the
 steering wheel connects to the car wheels
 and makes them turn
 Steering system encapsulates these details
 Driver only needs to invoke cars’s steering
 behaviour by turning the steering wheel
              Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                             7




Encapsulation
 Other parts of program interact with object by
 invoking behaviour
 To make Employee change location, call
 Move method
 Don’t directly access Location attribute
 Don’t need to know how Employee object
 does this operation – may be more than
 simply changing attribute
 Attributes can be packaged within protection
 of behaviour
              Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                             8




Information hiding
 Object has a public interface
 Other objects use this interface to
 communicate with it
 Details of how state is stored and how
 behaviour is implemented are kept private
 Private details can be changed without
 affecting objects that depend on it



              Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                             9




                                                                                                                       3
                                                                                                                 9/5/2011




Responsibilities and collaboration
 Encapsulation allows objects to have
 responsibilities
 This allows objects to interact, or
 collaborate, with each other
 Program consists of objects which interact,
 just as real world entities interact
 e.g. In a company each person has a job to
 do (responsibilities), and people collaborate
 to achieve the company’s aims
              Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                           10




Messages
 Collaborating objects interact with each other
 by sending messages
 Message is simply the name of a method to
 be called on the receiving object
 Information may be passed to receiving
 object as method parameter(s)
 Reply may be passed back as method return
 value


              Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                           11




Classes
 May have more than one object of the same
 kind that have common characteristics
 Class is a template for creating objects of the
 same kind
 Employee class can be used to create many
 Employee objects
 When we write the code we are actually
 writing the class definitions


              Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                           12




                                                                                                                       4
                                                                                                                      9/5/2011




Classes and objects
 An object is a specific instance of a class
 Class defines the attributes and methods
 which are common to all instances of class
          j                       p
 Each object can have its own specific values
 for these attributes
 Each Employee object will have a Name, but
 the value is different in each object
 Objects are created from the class as the
 program runs
               Object Oriented Software Development                        2. C# object oriented programming basics
                                                                                                                13




Class diagrams
                                                                       private – cannot be
                                                                       accessed by other
                                                                       objects
  class name




  attributes



   methods
                                                                       public – can be
                                                                       accessed by other
                                                                       objects



               Object Oriented Software Development                        2. C# object oriented programming basics
                                                                                                                14




Object diagrams


                                             each object here is an instance
                                             of the Employee class with its
                                             own values for the attributes




               Object Oriented Software Development                        2. C# object oriented programming basics
                                                                                                                15




                                                                                                                            5
                                                                                                                       9/5/2011




Attribute types
 Each attribute represents a piece of information
 of a specific type, which can be:
   a C# built-in type, e.g. string, int
   any .NET Framework type, e.g. DateTime (we will
   look t NET t         i        d t il later)
   l k at .NET types in more detail l t )
   any class in your application, e.g. Location (a class
   which would represent a work location)
 Attributes of an object are also known as
 instance variables or fields
   prefer these as attribute has another meaning in C#
                  Object Oriented Software Development                      2. C# object oriented programming basics
                                                                                                                 16




Method signatures
 Specify method name, return type and
 parameter types, e.g.:
 Move
   Needs a Location as a parameter
   returns no value (return type is void)
 Email
   Returns a string containing the email address
   Doesn’t need to pass any parameters


                  Object Oriented Software Development                      2. C# object oriented programming basics
                                                                                                                 17




Class diagram with more detail

            attribute types




                       parameters
                                                                          return type




                  Object Oriented Software Development                      2. C# object oriented programming basics
                                                                                                                 18




                                                                                                                             6
                                                                                                                   9/5/2011




C# class example code
 OOBasicsDemo project

 Employee.cs




                Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                             19




Creating objects
 Need test program to create some objects
 and make them do something
 Program.cs
                    yp              pp
 Main method – entry point to a C# application
 Creates object using new keyword



 emp1, emp2 are object references each of
 which “points” to an object of type Employee
                Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                             20




null references
 A reference can be null
 The reference is declared but does not
 actually point to an object

                            reference declared, not assigned to object - null



                                                    reference assigned to object


          reference no longer assigned to object - null



                Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                             21




                                                                                                                         7
                                                                                                                  9/5/2011




Constructors
 Constructor is executed when object is
 created to initialise object

    p y
 Employee must have constructor with
 matching signature
 Class can have more than one constructor,
 each with different signature
 Employee has two constructors – one takes
 no parameters (default constructor)
               Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                            22




Sending messages to objects
 Send message by calling method
 In example code, method of Employee sends
 message to a TimeSheet object to ask it to
 record an entry
   Calls AddEntry method
 Employee does not need to know how
 TimeSheet does this
 Note that test program sends message to
 Employee object to start this off
               Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                            23




C# messages example code
 OOBasicsDemo project

 Employee.cs
 TimeSheet.cs
 Program.cs




               Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                            24




                                                                                                                        8
                                                                                                                              9/5/2011




 Sequence diagram

                     object




time


                                                                    message



         This diagram shows that Program calls RecordOvertime on
         Employee object emp1, which as a result calls AddEntry on
         TimeSheet object ts – look at code to see how this works

                           Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                                        25




 Object relationships
       Need to define relationships to allow objects
       to interact
       Can show relationships in class and object
       diagrams
       Need to implement these in code
       Learn code patterns for implementing various
       relationship types



                           Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                                        26




 “has-a” relationship
       One object contains one or more other
       objects
         Department has Employees, Employee has-a
         Location
                  ti    h d         hi
         aggregation – shared ownership
         composition – exclusive ownership
         association – no ownership
       Usually implemented as an instance variable
       in one class
         Employee has an attribute of type Location
                           Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                                        27




                                                                                                                                    9
                                                                                                                  9/5/2011




“uses-a” relationship
 One object has some kind of association
 with another
   Employee uses-a TimeSheet
 Association is temporary, not implemented by
 instance variable
 Can be implemented through a method
 parameter
   Employee’s RecordOvertime method has a
   parameter of type TimeSheet

               Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                            28




“is-a” relationship
 Inheritance relationship
 We will come back to this later...




               Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                            29




C# relationships example code
 OOBasicsDemo project

 Employee.cs
 Location.cs
 TimeSheet.cs
 Department.cs



               Object Oriented Software Development                    2. C# object oriented programming basics
                                                                                                            30




                                                                                                                       10
                                                                                                                                  9/5/2011




Relationships in class diagram

                  aggregation                                                   association (has-a)




 Note that we are not showing the
 currentLocation field of Location in the
                                                                            association (uses-a)
 Employee class box now as it is
 implied by the relationship


                        Object Oriented Software Development                           2. C# object oriented programming basics
                                                                                                                            31




Relationships in object diagram
                                                         “snapshot” of objects which
                                                         exist at a point in time while
                                                         the program runs




                                                          here there are two Employee
                                                          objects associated with the same
                                                          Location object




                        Object Oriented Software Development                           2. C# object oriented programming basics
                                                                                                                            32




What’s next?
 Next we will look in more detail at the syntax
 of C# and how to write C# classes




                        Object Oriented Software Development                           2. C# object oriented programming basics
                                                                                                                            33




                                                                                                                                       11

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:2
posted:12/21/2011
language:
pages:11