Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1,...

25
Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction. Collections of Objects FEN 2013-01-27 1 AK IT: Softwarekonstruktion

Transcript of Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1,...

Page 1: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

AK IT: Softwarekonstruktion 1

Session 04:C# OOP 2

OOP in C#:Object-Oriented Design.

Realisation of Object Associations:1-1, 1-n, n-m.

UML Class Diagram.More about Object Interaction.

Collections of Objects

FEN 2013-01-27

Page 2: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

FEN 2013-01-27 AK IT: Softwarekonstruktion 2

The Anatomy of a Class

Classes are usually written by this pattern:

class ClassName {

declaration of attributesconstructorspropertiesmethods

}

Page 3: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

FEN 2013-01-27 AK IT: Softwarekonstruktion 3

The Class BankAccount- attributes and constructor

namespace Banking{

public class BankAccount{

private double balance;private int accNo;private int interestRate;

public BankAccount(int no, int ir){

balance = 0;accNo = no;intrestRate = ir;

}

Page 4: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

FEN 2013-01-27 AK IT: Softwarekonstruktion 4

Methods

public bool Withdraw(double amount)

public void Deposite(double amout)

public void GiveInterest()

Page 5: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

FEN 2013-01-27 AK IT: Softwarekonstruktion 5

Properties

public int InterestRate{

get{return interestRate;}set{if( value>=0) interestRate = value;}

}

Page 6: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Object Interaction

• Objects may be connected in different ways:– Association (“know-of-relation”).

One object uses another.

– Aggregation (“part-of-relation”).One object is a part of another.

• The distinction is not always clear .

FEN 2013-01-27 AK IT: Softwarekonstruktion 6

Means aggregation

Page 7: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Cardinality or Multiplicity• Tells how many other objects an

object may be associated with:– One customer may have one

account, an account must belong to a customer.

– One customer may have many accounts, an account must belong to one customer.

– A customer may one or more accounts, an account may belong to one or more customers.

FEN 2013-01-27 AK IT: Softwarekonstruktion 7Goes for

aggregation as well.

Page 8: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Object Interaction: 1 - 1

• The Banking example shows object interaction:– The classes are connected – in UML:

FEN 2013-01-27 AK IT: Softwarekonstruktion 8

Page 9: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

In the Code

public class Customer{//…private BankAccount account;//…

account= new BankAccount(no, ir, bal);

FEN 2013-01-27 AK IT: Softwarekonstruktion 9

Customer is responsible for creating BankAccount objects.

The association is implemented by an

object reference (attribute).

Page 10: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

In the Code

public class Program{ //… Customer c = new Customer(1, "Peter Thomsen"); //…

Console.WriteLine("Customer: "+ c.Name +" has DKK “ + c.Account.Balance + " in the bank");

FEN 2013-01-27 AK IT: Softwarekonstruktion 10

Methods in the other class is called using the reference.

Page 11: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Forest Exercise

• Solution?

FEN 2013-01-27 AK IT: Softwarekonstruktion 11

Page 12: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Implementing 1 - n• One customer may have many

accounts, an account must belong to one customer.

• Possible solution: A collection of BankAccounts in Customer (accounts)

FEN 2013-01-27 AK IT: Softwarekonstruktion 12

Page 13: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

In the Code

public class Customer{ //…

private List<BankAccount> accounts; //… public Customer(int cNo, string n){

//… accounts = new List<BankAccount>();

}public void AddAccount(BankAccount acc){

accounts.Add(acc);}

//…

FEN 2013-01-27 AK IT: Softwarekonstruktion 13

View Source

Page 14: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

AK IT: Softwarekonstruktion 14

Pair Programming(an eXtreme Programming practice)

• A well known and widely used technique:– Two programmers – one workstation– Programmer one is at the keyboard and have

focus on the details that are being typed.– Programmer two supervises programmer one’s

work and has the broader perspective and keeps focus on structure and what’s going to happen next.

– After a period (10 – 15 min.) programmer one and two swop roles.

FEN 2013-01-27

Page 15: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

AK IT: Softwarekonstruktion 15

Exercise

• Do the Forest exercise (1 – 3) on Session04.docx.

• Use pair programming.

FEN 2013-01-27

Page 16: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Implementing n - m• A customer may have one or more

accounts, an account may belong to one or more customers.

• Possible solution: A collection of BankAccounts in Customer (accounts) and a collection of Customers (owners) in BankAccount.

FEN 2013-01-27 AK IT: Softwarekonstruktion 16

Page 17: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

In the Code

public class BankAccount{ //…

private List<Customer> owners; //… public BankAccount(int no, double ir, double bal){ //… owners = new List<Customer>(); } public void AddOwner(Customer c) { owners.Add(c); }//…

FEN 2013-01-27 AK IT: Softwarekonstruktion 17

View Source

Page 18: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Exercise

• Do part 4 and 5 (Banking) of the exercises on Session04.docx

• Use pair programming.

FEN 2013-01-27 AK IT: Softwarekonstruktion 18

Page 19: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Implementing Associations Design Choices

• Several possibilities for the different cardinalities.

• The choice mostly depends on business logic (use cases)

FEN 2013-01-27 AK IT: Softwarekonstruktion 19

Page 20: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

1 – 1:

• One of the objects must have a reference to the other.

• But which one?• Depends of business logic:

– Is access typically from Customer

– or

– from BankAccount?

FEN 2013-01-27 AK IT: Softwarekonstruktion 20

In Banking1

Page 21: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

1 – n (1..*)

• Again – it depends on business logic:

– A collection of references on the 1-side?

– A single reference on the n-side?

FEN 2013-01-27 AK IT: Softwarekonstruktion 21

In Banking2

Page 22: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

n - m (*..*)

• Here we have no choice:– There must be a collection in at least one of the classes:

FEN 2013-01-27 AK IT: Softwarekonstruktion 22

• But this is often a problematic solution:• Complicated to maintain.• Lots of coding is required for doing updates, inserts and deletes.

In Banking3

Page 23: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

n - m (*..*)

FEN 2013-01-27 AK IT: Softwarekonstruktion 23

• But this is often a problematic solution:• Complicated to maintain.• Lots of coding is required for doing updates, inserts and deletes.

• Often it is a good design to introduce a connection object:

• And replace the n – m association by two 1 – n associations.• The designs for 1 – n may then be applied.• If there are any information on the association, this is necessary:

Page 24: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Example: Project Management

• Lets look at an other example:– An employee may work on several projects.– A project may have several employees working on it.– We need to record the number of hours a given

employee has spent on a given project:

FEN 2013-01-27 AK IT: Softwarekonstruktion 24

Let’s dive into the code

Page 25: Session 04: C# OOP 2 OOP in C#: Object-Oriented Design. Realisation of Object Associations: 1-1, 1-n, n-m. UML Class Diagram. More about Object Interaction.

Exercises• Do part 6 (EmpProjV1) on Session04.docx

• Do part 7 (Banking3) on Exercises03.pdf.

• Use pair programming.

FEN 2013-01-27 AK IT: Softwarekonstruktion 25