Session 04: C# OOP 2 OOP in C#: Object-Oriented Design.

Slides:



Advertisements
Similar presentations
Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester,
Advertisements

1 OOP in C#:Object Interaction. Inheritance and Polymorphism. Session 2: OOP in C#
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
Object Oriented Programming: Classes and Objects.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Ch 12: Object-Oriented Analysis
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Assignment I, part 1. Groups of three students. Specify one as group leader. group names to TA and me. Create an object-oriented conceptualization.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
Using Diagrams to Represent Program Structure OMT and UML Some pictures and material are from “Design Patterns” by Gamma et al.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Introduction - The Need for Data Structures Data structures organize data –This gives more efficient programs. More powerful computers encourage more complex.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
FEN  Conceptual modelling: The Entity-Relation (ER) Model Conceptual models Entity – Relation (ER) models.
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Module 7: Object-Oriented Programming in Visual Basic .NET
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Introduction to Object-Oriented Programming
1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1.
Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN AK - IT Softwarekonstruktion.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
FEN  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model.
Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits.
Inheritance in C++ Content adapted from a lecture by Dr Soo Yuen Jien.
Object-Oriented Programming in C++
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Class Relationships and Object Interaction. 8/8/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
Distributed Java Programming Distributed Java Programming Class #2 August 22, 2002.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
Domain Model Classes and Objects Association Structure Requirement Specification Domain Model.
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Session 2: OOP in C# OOP in C#: –Object Interaction. –Inheritance and Polymorphism. Autumn 20121UCN Technology: IT/Computer Science.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN AK - IT: Softwarekonstruktion.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Kyung Hee University Class Diagramming Notation OOSD 담당조교 석사과정 이정환.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Designing Classes Lab. The object that you brought to class Put it in the basket we will exchange them now.
Unit 1 - Introducing Abstract Data Type (ADT) Part 1.
draw a sequence diagram
Creating and Using Classes
Lecture 13 Writing Classes Richard Gesick.
Chapter Three - Implementing Classes
The Basics of Class Diagrams for a single class
COP 3330 Object-oriented Programming in C++
Using Diagrams to Represent Program Structure
JAVA CLASSES.
By Rajanikanth B OOP Concepts By Rajanikanth B
Introduction to Object-Oriented Programming
Classes.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion The Anatomy of a Class Classes are usually written by this pattern: class ClassName { declaration of attributes constructors properties methods } FEN 2013-01-27 AK IT: Softwarekonstruktion

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; } FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion Methods public bool Withdraw(double amount) public void Deposite(double amout) public void GiveInterest() FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion Properties public int InterestRate { get{return interestRate;} set{if( value>=0) interestRate = value;} } FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion 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 . Means aggregation FEN 2013-01-27 AK IT: Softwarekonstruktion

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. Goes for aggregation as well. FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion Object Interaction: 1 - 1 The Banking example shows object interaction: The classes are connected – in UML: FEN 2013-01-27 AK IT: Softwarekonstruktion

In the Code The association is implemented by an object reference (attribute). public class Customer{ //… private BankAccount account; account= new BankAccount(no, ir, bal); Customer is responsible for creating BankAccount objects. FEN 2013-01-27 AK IT: Softwarekonstruktion

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"); Methods in the other class is called using the reference. FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion Forest Exercise Solution? FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion 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

AK IT: Softwarekonstruktion 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); View Source FEN 2013-01-27 AK IT: Softwarekonstruktion

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 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion Exercise Do the Forest exercise (1 – 3) on Session04.docx. Use pair programming. FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion 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

AK IT: Softwarekonstruktion 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); View Source FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion Exercise Do part 4 and 5 (Banking) of the exercises on Session04.docx Use pair programming. FEN 2013-01-27 AK IT: Softwarekonstruktion

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

AK IT: Softwarekonstruktion 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? In Banking1 FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion Again – it depends on business logic: A collection of references on the 1-side? A single reference on the n-side? In Banking2 FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion n - m (*..*) Here we have no choice: There must be a collection in at least one of the classes: In Banking3 But this is often a problematic solution: Complicated to maintain. Lots of coding is required for doing updates, inserts and deletes. FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion n - m (*..*) 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: FEN 2013-01-27 AK IT: Softwarekonstruktion

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: Let’s dive into the code FEN 2013-01-27 AK IT: Softwarekonstruktion

AK IT: Softwarekonstruktion 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