28
CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Embed Size (px)

Citation preview

Page 1: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

CET203SOFTWARE DEVELOPMENT

Session 3BInterfaces

Page 2: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Objectives

• Define interfaces to extend polymorphism beyond inheritance hierarchies

• Appreciate the scope for extensibility which polymorphism provides

Page 3: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Inheritance

• There are two aspects to inheritance:– the subclass inherits the interface (i.e. access to

public members) of its superclass – this makes polymorphism possible

– the subclass inherits the implementation of its superclass (i.e. instance variables and method implementations) – this saves us copying the superclass details in the subclass definition

• In C#, the : keyword automatically applies both these aspects.

Page 4: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Interfaces• Sometimes we may want two classes to share a common

interface without putting them in an inheritance hierarchy.

• This might be because– they aren’t really related by a true ‘is a’ relationship– we want a class to have interfaces shared with more than one

would-be superclass, but C# does not allow such ‘multiple inheritance’

– we want to create a ‘plug and socket’ arrangement between software components, some of which might not even be created at the current time

Page 5: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Revision

Book

Author : String

OrderCopies(copies : int)

Magazine

orderQty : intcurrIssue : String

AdjustQty(quantity : int)RecvNewIssue(newIssue : String)

DiscMag

RecvNewIssue(newIssue : String)

CashTill

runningTotal : double

SellItem(pub : Publication)ShowTotal()

<<abstract>>Publication

title : Stringprice : doublecopies : int

SellCopy()

Page 6: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Tickets

• In addition to books and magazines, we now want to accommodate selling tickets, e.g. for entertainment events, public transport, etc.

• These are not like Publications because– we don’t have a finite ‘stock’ but print them on

demand at the till– tickets consist simply of a description, price and

client (for whom they are being sold)– these sales are really a service rather than a

product

Page 7: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Exercise 1a

• What features do Tickets have in common with Publications?

Page 8: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Exercise 1b

• Suggest a class diagram for the Ticket class

Page 9: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

SellCopy() for Ticket public void SellCopy() { Console.WriteLine("**************************************"); Console.WriteLine(" TICKET VOUCHER "); Console.WriteLine(this.ToString()); Console.WriteLine("**************************************"); Console.WriteLine(); }

• This is quite different to – and not simply a modification of – the behaviour involved in selling a Publication.

• The ticket details are simply ‘printed’ in the system console window using ToString().

• In particular note the absence of any stock control functionality because we don’t have a ‘stock’ of tickets.

Page 10: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

To the CashTill

• We don’t feel that Ticket belongs in an inheritance hierarchy with Publications.

• But we do want to be able to check tickets through the till as we can with publications.

• Just like publications, tickets provide the operations which CashTill needs:– SellCopy()– Price (the property provides the operation of obtaining the

price)

• We will define this set of operations as an ‘Interface’ called ISaleableItem.

Page 11: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

ISaleableItem Interface

• The ISaleableItem interface will capture the small amount of functionality – just two operations – which are needed for the CashTill to process an item.

• Any class which implements this interface will then be acceptable to the till.

• Note that we have included the property Price. The get and/or set methods within a property can also form part of the interface.

Page 12: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

ISaleableIteminterface ISaleableItem{ double Price { get; } void SellCopy();}

Page 13: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Interface definition

• The interface defines purely the signatures of operations without their implementations.

• All the methods are implicitly public so this is not stated, and there can be no instance variables or constructors.

• An interface defines the availability of specified operations without saying anything about their implementation. That is left to classes which implement the interface.

• In this example we have provided the get operation for the property price but not the set operation since the CashTill is only interested in selling the items, not changing their prices.

Page 14: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Contract

• An interface is a sort of contract.• The ISaleableItem interface says “I undertake to

provide, at least, methods with these signatures: double Price{get;} void SellCopy();

though I might include other things as well”• Where more than one class implements an interface

it provides a guaranteed area of commonality which polymorphism can exploit.

Page 15: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Exercise 2

• Think of a car and a driving game in an arcade.• They certainly are not related by any “is a”

relationship – they are entirely different kinds of things, one a vehicle, the other an arcade game.

• They do share some common operations however• Suggest an interface that they could both implement

Note: there may be a number of interfaces that could be suggested

Page 16: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Implementing an interface

• We now need to state that both Publication (and all its subclasses) and Ticket both offer the operations defined by this interface:class Publication : ISaleableItem{ [...class details...]}class Ticket : ISaleableItem{ [...class details...]}

Page 17: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Inheritance & Interfaces

• It is possible for a class in C# to inherit from a single superclass and to implement one or more interfaces

• This is denoted using one : and commas between the superclass and interfaces

• This is one way of getting around the lack of multiple inheritance in C#!

class MyClass : MySuperClass, IMyInterface{ [...class details...]}

Page 18: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Polymorphic CashTill• The CashTill class already employs polymorphism: the SellItem method

accepts a parameter of type Publication which allows any of its subclasses to be passed:

public void SellItem (Publication pub)

• We now want to broaden this further by accepting anything which implements the SaleableItem interface:

public void SellItem (ISaleableItem selb)

• When the type of a variable or parameter is defined as an interface, this works just like a superclass type.

• Any class which implements the interface is acceptable for assignment to the variable/parameter because the interface is a type and all classes implementing it are subtypes of that type

Page 19: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

The new class diagram

Page 20: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Code for CashTill.SellItem()public void SellItem(ISaleableItem selb){ String msg;

runningTotal = runningTotal + selb.Price; selb.SellCopy();

msg = "Sold " + selb.ToString() + " @ " + selb.Price + "\nSubtotal = " + runningTotal; Console.WriteLine(msg);}

Page 21: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Exercise 3

Publications2C.sln

Page 22: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Exercise 3

• Assuming the following objects have been declared:CashTill myTill = new CashTill();Book book1 = new Book("Software Development", 10.99, 3, "Liz Gandy");Magazine mag1 = new Magazine("C# monthly", 2.5, 10, 10, "September");DiscMag dmag1 = new DiscMag("C# programming", 2.5, 10, 10, "September");Ticket ticket1 = new Ticket("The Sound of Music", 25.99, "Sunderland Empire");

• Write the code statements to sell one copy of each item through the new CashTill and then show the overall total

Page 23: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Extensibility

• Polymorphism allows objects to be handled without regard for their precise class.

• This can assist in making systems extensible without compromising the encapsulation of the existing design.

• For example, we could create new classes for more products or services and so long as they implement the SaleableItem interface the CashTill will be able to process them without a single change to its code!

Page 24: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Distinguishing subclasses

• What if we have an object handled polymorphically but need to check which subtype it actually is?

• The is operator can do this:

object is class

• This test is true if the object is of the specified class (or a subclass), false otherwise.

Page 25: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Exercise 4

Publications2D.sln

Page 26: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Exercise 4

• Consider the following objects:Ticket myTicket;Magazine myMag;DiscMag myDiscMag;

• Which of the following will be true or false:myDiscMag is DiscMagmyTicket is PublicationmyDiscMag is MagazinemyMag is DiscMagmyDiscMag is PublicationmyTicket is ISaleableItem

Page 27: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Using “is” public void SaleType(ISaleableItem selb) { if (selb is Publication) { Console.WriteLine("This is a Publication"); } else if (selb is Ticket) { Console.WriteLine("This is a Ticket"); } else { Console.WriteLine("This is a an unknown sale type"); } }

Page 28: CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces

Summary• Sometimes we want to employ polymorphism without

all the classes concerned having to be in an inheritance hierarchy.

• The ‘interface’ construct allows us to provide shared interfaces (i.e. collections of operations) in this situation.

• There is no inherited implementation – the class must implement ALL the operations itself as appropriate.

• Any number of classes can implement a particular interface.

• If necessary we can determine the type of an object which is referenced polymorphically.