Академический Документы
Профессиональный Документы
Культура Документы
NET
Introduction
About Miguel
Sports
Super Bowl
Instead of selling product, addressing business issues Setting and Achieving Goals
Sales
Personal Success
Object Oriented Analysis and Design (OOAD) Proper application of OOP Principles
Abstraction Encapsulation Inheritance Polymorphism
Code Reuse
Rapid Application Development Lower Cost of Development Shorter Application Development Cycle
Caveats
Design Phase More Critical Initially, Longer Design Phase
What is a class?
An artifact of human classification! Classify based on common behavior or attributes Agree on descriptions and names of useful classes Create vocabulary; we communicate; we think!
A named syntactic construct that describes common behavior and information A data structure (type) that includes both data (fields) and behavior (methods)
What is an object?
Identity: Objects are distinguishable from one another (Different reference means different object) Behavior: Objects can perform tasks (C#: methods, VB.NET: Subs, Functions) State: Objects store information (fields)
No identity, accessible state, no added behavior (behavior is allowed, just not extensible)
Value types:
Reference types:
Directly contain their data Each has its own copy of data Operations on one cannot affect another
Store references to their data (known as objects) Two reference variables can reference same object Operations on one can affect another
int i, j; i=5; j=i; MyClass myObject1, myObject2; myObject1 = new MyClass(); myObject2 = myObject1;
Dim i, j As Integer i = 5 j = i Dim myObject1, _ myObject2 As MyClass myObject1 = NewMyClass() myObject2 = myObject1
YES
YES
All variables (value or reference) and their respective values are stored on the stack All classes and objects (instances of classes) are stored on managed heap Classes are loaded into the managed heap using the Class Loader of the Common Language Runtime (CLR)
Abstraction
The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise. Edsger Dijkstra
Encapsulation
Logical Encapsulation
Physical Encapsulation
Combination
Why Encapsulate?
Allows control
Withdraw( ) Deposit( )
Use of the object is solely through the public methods Use of the object is unaffected if the private data type changes
balance 12.56
Allows change
internal (Friend)
protected internal (Protected Friend)
For example, each bank account has its own balance. If two accounts have the same balance, it is only a coincidence.
Withdraw( ) Deposit( ) balance owner 12.56 "Fred"
For example, suppose all accounts share the same interest rate. Storing the interest rate in every account would be a bad idea. Why?
Withdraw( ) Deposit( )
balance
99.12
interest 7%
A static method is called on the class, not the object (i.e. Class.Method( ) )
An account object
InterestRate( )
interest 7%
Classes contain static data and
Inheritance
Generalization
Musician
Base class
Specialization
Violin Player
Derived class
Single inheritance: deriving from one base class Multiple inheritance: deriving from two or more base classes
Stringed Instrument Musical Instrument Pluckable
Inheritance (cont.)
Inheritance Is the Reuse of Class Members in Other Classes The Common Type System (CTS) Only Supports Single Inheritance for Classes Member Hiding
Redefine the same method in the derived class Use the new (Shadows) keyword
Class Hierarchies
String Musician
plays
Stringed Instrument
???
Violin Player
plays
Violin
???
Polymorphism
The method name resides in the base class The method implementations reside in the derived classes Polymorphism Allows a Reference Variable to Call the Correct Method Virtual Methods Enable Polymorphism in the Common Type System
String Musician TuneYourInstrument( ) A method with no implementation is called an operation
Interfaces
Interfaces contain only operations (noncoded methods), not implementation Interfaces are a way of sharing behavior between different classes
Musician interface Nothing but operations. You cannot create instances of an interface.
May contain some implementation. You cannot create instances of an abstract class.
Must implement all inherited operations. You can create instances of a concrete class.
Normal method calls are resolved at compile time Polymorphic method calls are resolved at run time
Musician interface TuneYourInstrument( )
Late binding
runtime
Violin Player concrete
TuneYourInstrument( )
Early binding
Binding Example
IFly TakeOff( ) Land( )
Bird: IFly
TakeOff( ) { } Land( ) { }
Plane p = new Plane(); Bird b = new Bird(); IFly flier = (IFly) p; flier.TakeOff(); flier.Land();
Use cases
Provide a functional description of major processes Use a non-technical language to describe the process Show a boundary around the problem to be solved
Graphically describe who or what will use the processes Is not limited to persons
Actors
Internet Client
Add Customer
Database
Telesales Agent
Remove Customer
Use case descriptions provide the basis for initial class design
A user requests the orders for a customer by using a particular customer ID. The ID is validated by the database, and an error message is displayed if the customer does not exist. If the ID matches a customer, the customers name, address, and date of birth are retrieved, in addition to any outstanding orders for the customer. Details about each order are retrieved, including the ID, the date, and the individual order items that make up an order.
<<extends>>
Remove Customer
Association Aggregation
Association
A class depends on another class to perform some functionality Roles are the direction of the association Multiplicity determines how many objects can participate in a relationship
Multiplicity
Customer
0..*
Order
Association
Aggregation
An Order class contains an OrderItem class An OrderItem class is a part of an Order class *
Aggregation
Order
OrderItem
Class Diagrams
Customer * Order
1..*
Order Item *
Product
Class is a defined set of data and behavior Attributes are the data contained in a class Operations are the actions performed on that data Accessibility: Public (+), Private (-), Protected (#)
Customer
-CustomerID: Long +Name: String +DOB: Date +AddCustomer( ) +GetDetails(ID: Integer) -CheckValid( ): Boolean
Attributes (Fields)
Design phase of development process is more critical Good OOAD in the design phase can consume up to 60% of development process Initial move to OOAD increases immediate cost ROI is seen in later application or component development
Observations
New programmers and current VB 6.0 developers find it more difficult to shift to OOAD OOAD and OOP should have more emphasis in .NET development
Additional Resources
OOP with Microsoft Visual Basic .NET and Microsoft Visual C# .NET Step by Step, Robin A. Reynolds-Haertle, Microsoft Press, 2002, ISBN: 0735615683 Object Oriented Software Construction, 2nd Edition, Bertrand Meyer, Prentice Hall, 1997, ISBN: 0136291554 Professional UML with Visual Studio .NET, John Slater, et al, Wrox Press, 2002, ISBN: 1861007957 Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design, and the Unified Process, Craig Larman, Prentice Hall, 2001, ISBN: 0130925691