Object Oriented Techniques

1. Introduction

The huge growth in the level of functionality required from computer systems has resulted in larger, more complex systems and a need existed for newer, more flexible methods of developing software. These were aimed at enabling systems to be extended more easily and enable the re-use of existing software components to avoid duplication of effort. This is evident now in the huge libraries coming with development environments. Furthermore, the growth of the internet has resulted in the need for device-independent portable software which is very robust and secure. Applications must be able to migrate easily to a wide variety of computer systems, hardware architectures and operating system architectures. They must be able to work with a variety of GUIs, for example on a range of computers and mobile devices.

2. Requirements for a Design Method

From the user's perspective, software should be built from components where each component models an object in the real world. Thus the software modelling and development method should provide :-

• capabilities for more closely modelling real world problems.

• capabilities for constructing effective interfaces to software systems using familiar metaphors.

From the programmer's perspective, a program component should describe one of these real world objects and it's behaviour to the machine. The development method should also enable:-

• means for constructing reusable software components and easily extensible libraries of software modules.

• means for easily modifying and extending implementations of components without having to recode from scratch.

These requirements are fulfilled by object oriented design methods.

3. The Object Oriented Approach : A Brief Introduction.

Object orientation provides capabilities for more closely modelling real world problems and for constructing effective interfaces to software systems using familiar metaphors.

An object is a model of a real world object. A class is a blueprint of a set of objects and will specify the type of attributes and behaviour of that object. e.g. the class of all buttons will specify that buttons will have a colour and a label, buttons can be clicked and so on. This enables code to be written to represent a button which is picked up every time you create a button. The attriutes and behaviour are said to be encapsulated within the object. Objects communicate by message-passing e.g. textbox1.gettext(); sends a message to a text box to get the text out of it!

Object orientation also provides mechanisms for re-use of software components :-

•Inheritance allows a software component to be modified for more general or more specific use. For example, a window class is a generalisation of velux windows , sash etc.; a chesspiece class is a generalisation of queen, king, knight,pawn etc. A pawn results from a specialisation of a chesspiece.

• Polymorphism enables software components to be created which will work on any type of data.

As object orientation allows a more direct representation of the real world model in the code, the result is that the transformation from system requirements (defined in users terms) to system specification (defined in computer terms) is greatly reduced. Users, analysts, system designers and programmers are all dealing with objects and their relationships at different levels of detail. The object oriented development process is repetitive, involving increasingly clearer specifications of what is to be done and it involves increased communication between all the parties involved. It has led to development methods such as agile programming, where porgrammers and business end-users work closely together. In UML, class diagrams are used to specify what classes of object will exist in the system. There are three fundamental class types – boundary classes, which deal with user interaction, entity classes which deal with the key entities in the application and control classes which specify how processes reposented earlier as use case unfold.

Example 1: Object Orientation and The User Interface

Object oriention is very evident when designing the user interface to programs, using toolkits like WPF as the interface can be built from interaction objects (such as windows, buttons, dialog boxes, forms etc.) that present the functionality of the system to the user. Each of these objects, consisting of data, describing their state (e.g. a button can be pressed or not, a menu item can be activated or not, a window may have a specific background colour) and the operations which define their behaviour (e.g. what to do when a button is pressed), behave as autonomous agents with specific responsibilities.

Development environments such as Visual Studio let you build interfaces from pre-written librariesof classes of interaction objects( e.g. WPF), which can be adapted to suit your purposes. Thus, prototypes can be quickly built to let you see what a system is going to look like before most of the code is written.

According to Microsoft

“WPF, a component of Microsoft .NET Framework 4, supports UI, media, documents, hardware acceleration, vector graphics, scalability to different form factors, integration with Windows, interactive data visualization, and superior content readability.”

Example 2: Game Development

The XNA Framework class library is a library of classes, interfaces, and value types that are included in XNA Game Studio. This library provides access to XNA Framework functionality and is designed to be the foundation on which XNA Game Studio applications, components, and controls are built.

Object Oriented Design: Basic Concepts

A representation in software of a real world problem is created by identifying what the main objects - the actors in the problem specification - are, finding out what their attributes are, what operations can act on them and how they interact.

1. Objects

An object is a software model of a real-world or other object. In programming terms, it is a software bundle of variables and related methods.

Objects have both attributes ( which describe the objects state) and methods which implement the objects behaviour. Objects communicate with each other by sending messages.

Objects can be ..

• things - e.g. books, planes, cars, timetables, bookings forms, buttons, windows, scrollbars, files, chessboards.

• people - e.g. students, staff, punters, users.

• concepts - e.g. departments, graphics, flights, accidents. lists,queues

They can also represent :-

• roles played by people, things, or concepts e.g. people can be voters, taxpayers, employees; windows can be text or graphics windows.

• relationships between abstractions of things, people or concepts e.g. people purchase things, desktops contain folders and files.

Each of the above describes sets or classes of real world objects, characterised by attributes ( data items which describe them) and behaviour (operations).

An object is an instance of a class.

The state of an object is the values of its attributes and the other objects it is linked to.

An object can also have an identity.

1.1 Examples of Objects.

Examples of objects include user interface components such as buttons, textboxes, lists, labels scrollbars and so on. These have attributes such as label. position, colour etc. and methods such as on_mouseclick, get_text, and so on which specify what is to be done when something happens.

As a system is also based on a description of aspects of the real world, objects can also be things relevant to the application e.g.

1. In an accounting system for Eircom a telephone subscribers account would be an object.

2. In a chessgame objects would include pieces, board, etc.

3. In a theatre booking system a show, a booking and a ticket could be objects.

4. In a banking system a bank account would be an object.

Features of Objects - Data Encapsulation and information hiding

An object consists of attributes and methods.which alter the objects state or implement it's behaviour. The attributes and how the methods are implemented normally remain private to the object. In other words the attributes are encapsulated within the object and communication with an object is achieved through the interfaces to each of its methods (messages).

Objects have internal data which defines their current state. This data is normally only accessible through an objects operations or methods. The information is therefore hidden from parts of the system which don't need to know it.

Operations or methods determine an object’s behaviour. A method's interface (which includes the parameters it

needs ) describes its behaviour.

Objects communicate and are communicated with by sending messages . These invoke the operations or methods. A message tells an object what task it should perform, not how it should perform it, thus the implementation of the task remains hidden. Parameters hold the information needed by the object. Interfaces to the methods tell you how to send messages. A message involves the objecname + method + parameters.

This is how data encapsulation/information hiding is achieved. Interface and implementation are separated, access to data items is clearly defined, and can only be done through well defined interfaces. Objects only exist in a program as long as they are needed. When they're finished with they should be deallocated by the underlying language system.

An object may have an identity.

2. Describing Objects - Classes

A class is a blueprint for an object. A class definition specifies what attributes an object has and what operations ( or methods) act on those attributes and what messages can be sent to the object.

Example 1

This class definition defines a data type called overdrawn which describes an overdrawn bank account object.

class overdrawn

private: namedata

account number

balance

number of times contacted

protected:delete account

public:print nasty lettermethods or operations

send appointment with bank manager

cancel account

Example 2

This defines a phonebook entry class

class phone-entry

private:surname

forename

address

code

phone number

public:create phone-entry (surname, forname, address, code, number) change_number(newnumber)

change_surname(newname)

print_details

get_number

3. Inheritance

Inheritance can be defined as the ability to define a new class as an extension or specialisation of an existing class . When deriving one class from another the derived or child class will inherit the features - the attributes and operations - of the parent class and have some more of its own. This is known as inheritance. If a subclass inherits the features of more than one class it is known as multiple inheritance.

As it is possible to derive subclasses from classes as extensions or specialisations of those classes we can have hierarchies of classes and subclasses. These can be described graphically by class hierarchy diagrams.Inheritance is one way of achieving software reusablity as it provides a mechanism wherby software components can be adapted for specific purposes (see wpf example below).

Example 1

1. If we had a staff class, its descendant classes or subclasses could be part time and full time staff, subclasses of full time staff could be lecturer, manager etc. The relationship here is "is a" . Thus we end up with a class hierarchy as in the diagram below.

All staff members share certain attributes (e.g. name, PRSI number), and operations (e.g. print payslip) . However part time staff have a set of additional attributes which differ from those of full time staff (e.g. hourly rate as opposed to annual salary), and also different operations (e.g. pay is calculated differently).

Example 2

If we had a class book which described all book objects, it could have a subclass dictionary which could have extra features related to a dictionary. This could have a sub sub class spanish dictionary .The relationship here is "is a" e.g. a spanish dictionary is a dictionary which is a book. Try drawing a class hierarchy diagram for a book class.

Example 3

In a chessgame a class os chesspieces might have subclasses king, queen, pawn etc.

4. Polymorphism

The word polymorphism means " the ability to take different forms". It allows a programmer to send a message to an object without knowing the exact type of the object.

For example, in the staff class above, an operation, print_payslip, may be required to print a staff member's payslip, but this may need to be done differently depending on whether the staff member is part-time or full-time.

A polymorphic "print_payslip" method works differently for the two classes but the message to do it is the same.

In a chessgame a polymorphic move method would workk differently for different pieces.

Code Example : WPF

source:

A window with a button

At run time, WPFconverts the elements and attributes that are defined in xaml markup to instances of WPFclasses. For example, the Window element is converted to an instance of the Window class whose Title property is the value of the Title attribute.

Code-Behind

The main behavior of an application is to implement the functionality that responds to user interactions, including handling events (for example, clicking a menu, tool bar, or button) and calling business logic and data access logic in response. In WPF, this behavior is generally implemented in code that is associated with markup. This type of code is known as code-behind.

using System.Windows; // Window, RoutedEventArgs, MessageBox

namespace SDKSample

{

publicpartialclass AWindow : Window[inherits properties of the window class]

{

public AWindow() [constructor function called whenobject initiated]

{

InitializeComponent();

}

void button_Click(object sender, RoutedEventArgs e)

{

// Show message box when button is clicked

MessageBox.Show("Hello, Windows Presentation Foundation!");

}

}

}

In this example, the code-behind implements a class that derives from the Window class. The x:Class attribute is used to associate the markup with the code-behind class. InitializeComponent is called from the code-behind class's constructor to merge the UI that is defined in markup with the code-behind class. (InitializeComponent is generated for you when your application is built, which is why you don't need to implement it manually.)

Introduction to object orientation: page 1