Object- Oriented Design with UML and Java Part I: Fundamentals

Similar documents
What are the characteristics of Object Oriented programming language?

CHAPTER 5 GENERAL OOP CONCEPTS

1. Write two major differences between Object-oriented programming and procedural programming?

Inheritance (Outsource: )

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

COURSE 2 DESIGN PATTERNS

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Appendix A - Glossary(of OO software term s)

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Goals of the Lecture OO Programming Principles

Chapter 1: Object-Oriented Programming Using C++

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

CSE 70 Final Exam Fall 2009

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Module Outline. What is Object-Oriented? Some Possible Definitions. Why Object-oriented? Fundamentals of Object Orientation

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism


Object Oriented Programming

Object-Oriented Systems Analysis and Design Using UML

Object-Oriented Concepts and Design Principles

1: Introduction to Object (1)

System Analysis and Design/ Object-Oriented System Modeling

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

OVERRIDING. 7/11/2015 Budditha Hettige 82

Software Development. Modular Design and Algorithm Analysis

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Object Oriented Paradigm

Overview. OOP: model, map, reuse, extend. Examples of objects. Introduction to Object Oriented Design

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

CSC207 Week 3. Larry Zhang

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Fundamental Concepts (Principles) of Object Oriented Programming These slides are based on:

An Introduction to Patterns

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

Sorting. Sorting. Selection sort

Credit where Credit is Due. Lecture 4: Fundamentals of Object Technology. Goals for this Lecture. Real-World Objects

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Design Patterns Design patterns advantages:

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Elementary Concepts of Object Class

C++ Important Questions with Answers

Chapter 5 Object-Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

Object-Oriented Programming Concepts

An Introduction to Object Orientation

Object Oriented Software Development CIS Today: Object Oriented Analysis

Allenhouse Institute of Technology (UPTU Code : 505) OOT Notes By Hammad Lari for B.Tech CSE V th Sem

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Inheritance, polymorphism, interfaces

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

The Final Exam Paper. Duration: 2 hours Reading: 15 minutes Total marks: 65 Hurdle: 32.5

Inheritance, Polymorphism, and Interfaces

Information System Design (IT60105)

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS 251 Intermediate Programming Inheritance

Inheritance and Polymorphism

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Building custom components IAT351

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

What about Object-Oriented Languages?

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

JAVA: A Primer. By: Amrita Rajagopal

Object Oriented Concepts. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

GRASP Design Patterns A.A. 2018/2019

Concepts of Programming Languages

Data Abstraction. Hwansoo Han

Object oriented programming Concepts

Chapter 10 Classes Continued. Fundamentals of Java

New Programming Paradigms

Topics in Object-Oriented Design Patterns

Data Structures (list, dictionary, tuples, sets, strings)

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

OBJECT ORIENTED SYSTEM DEVELOPMENT Software Development Dynamic System Development Information system solution Steps in System Development Analysis

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

Programming Languages and Program Development

Object-Oriented Design

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

PROGRAMMING IN C++ COURSE CONTENT

Object Fundamentals. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007

Java How to Program, 8/e

First IS-A Relationship: Inheritance

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

More Relationships Between Classes


HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Introduction to Object-Oriented Programming

[ L5P1] Object-Oriented Programming: Advanced Concepts

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

Design Patterns IV Structural Design Patterns, 1

Object-Oriented Design

Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

Chapter 8: Class and Method Design

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

Transcription:

Object- Oriented Design with UML and Java Part I: Fundamentals University of Colorado 1999-2002 CSCI-4448 - Object-Oriented Programming and Design These notes as free PDF files: http://www.softwarefederation.com/cs4448.html Copyright David Leberknight and Ron LeMaster. Version 2011

What is this course all about? Learn the Object-Oriented paradigm. Learn why some designs are better than others. Learn how to implement these designs in Java. Learn the Unified Modeling Language (UML). Learn some Design Patterns. Learn some of the latest-and-greatest commercial OO technology. Learn a process to make best use of this technology. Be prepared for further study and to work on real-world projects. Software Engineering in the wild... ( I ) Object- Oriented Fundamentals - 2

Programming Evolution First there was Machine Language with 10101010 Assembly Language provided symbols. High-level languages were invented to provide structure to the graph of program statements. Data structures and algorithms are reusable program structures. Object-orientation is primarily based on the problem to be solved rather than on the machine. The graph of object collaborations is at a higher-level of abstraction. Design Patterns provide reusable object structures. Components are reusable software entities. Frameworks such as Java E.E. Containers reuse distributedtransaction managers, user-session managers, support for object-torelational-database mapping, dependency injection, and more. ( I ) Object- Oriented Fundamentals - 3

Why Object-Oriented? OO is easier to comprehend, for humans. The implementation can be less complex. There s a small conceptual gap between analysis and implementation. A well-designed set of objects is resilient. It s easier to reuse an object than a function. The modeling process creates a common vocabulary and shared understanding between developers and users / clients. You don t need to be a programmer to understand a UML model. These benefits are not automatic. Design is an art. ( I ) Object- Oriented Fundamentals - 4

Example: David walks his dog, Leroy Find the objects... ( I ) Object- Oriented Fundamentals - 5

Linguistics & Cognition Nouns are the primary words that humans use. We qualify them with modifiers and attributes. Then we associate them with verbs. Furthermore, we make heavy use of abstractions & generalizations... Object-oriented design follows this pattern. Procedural / functional design does not. Subject-verb-object sentences flow from object models: People own pets. David owns Leroy. A computer game player has a strategy. ( I ) Object- Oriented Fundamentals - 6

The Procedural Approach The system is organized around procedures. Procedures send data to each other. Procedures and data are clearly separated. The programmer focuses on data structures, algorithms and sequencing. Functions are hard to reuse. Expressive visual modeling techniques are lacking. Concepts must be transformed between analysis & implementation. This paradigm is essentially an abstraction of machine / assembly language. ( I ) Object- Oriented Fundamentals - 7

The Object-Oriented Approach Begin by modeling the problem domain as objects. The implementation is organized around objects. Objects send messages to each other. Related data and behavior are tied together. Visual models are expressive and easy to comprehend. Powerful concepts: Encapsulation, interfaces, abstraction, generalization, inheritance, delegation, responsibility-driven design, separation of concerns, polymorphism, design patterns, reusable components, service-oriented architecture, message-oriented middleware,... ( I ) Object- Oriented Fundamentals - 8

Example: Temperature Conversion The Procedural / Functional approach: float c = gettemperature(); // assume Celcius float f = tofarenheitfromcelcius( c ); float k = tokelvinfromcelcius( c ); float x = tokelvinfromfarenheit( f ); float y = tofarenheitfromkelvin( k ); The OO approach: Temp temp = gettemperature(); float c = temp.tocelcius(); float f = temp.tofarenheit(); float k = temp.tokelvin(); ( I ) Object- Oriented Fundamentals - 9

Objects Represent things. Have responsibilities. Provide services. Exhibit behavior. Have interfaces. Have identity. Send messages to other objects. Should be self-consistent, coherent, and complete. Should be loosely coupled with other objects. Should encapsulate their state and internal structures. Should not be complex or large. ( I ) Object- Oriented Fundamentals - 10

Encapsulation Exposing only the public interface. Hiding the gears and levers. Protects the object from outside interference. Protects other objects from details that might change. Information hiding promotes loose coupling. Reduces complex interdependencies. Good fences make good neighbors. Example: Your car s gas pedal. Push down go faster. ( I ) Object- Oriented Fundamentals - 11

Encapsulation Example Best practice: Objects speak to each other by method calls not by direct access to attributes. class Person { } public int age; // yuk class BetterPerson { private int age; // dateofbirth? public int getage() { return age; } } ( I ) Object- Oriented Fundamentals - 12

Access Control Keywords that determine the degree of encapsulation: public = Interface stuff private = Can only be accessed by the class own member functions (in C++, also by the class friends). protected = Private, except for subclasses (in Java, protected attributes and methods are also available to classes in the same package). Rule of thumb: make everything as inaccessible as possible. Make things private unless there s a good reason not to. Encapsulation is good. ( I ) Object- Oriented Fundamentals - 13

Classes Programmers write code to define classes. An object is an instance of a class. An object, once instantiated, cannot change its class. A class defines both the interface(s) and the implementation for a set of objects, which determines their behavior. Abstract classes cannot have instances. Concrete classes can. Some OO languages (such as Smalltalk) support the concept of a meta-class which allows the programmer to define a class on-thefly, and then instantiate it. Java has a class called Class. ( I ) Object- Oriented Fundamentals - 14

Class Attributes and Behaviors Class attributes are shared by all the instances of the class (indicated by the keyword static ). Public and static items are essentially global. Examples: An Employee class may be responsible to keep track of all employees. It could have a method to calculate the number of employees who are fully vested in a stock option scheme, say. A LotteryTicket class may use a seed to generate random numbers; that seed is shared by all instances of the class. ( I ) Object- Oriented Fundamentals - 15

Abstraction Abstraction allows generalizations. Simplify reality - ignore complex details. Focus on commonalties but allow for variations. Human beings often use generalizations. When you see a gray German Shepherd named Rex owned by Jane Doe... Do you think dog? ( I ) Object- Oriented Fundamentals - 16

Abstraction Vocabulary Base Pet Question name asktheuser() Derived Dog Cat YesNoQuestion FreeTextQuestion Base class = parent class = superclass. Derived class = child class = subclass. The Derived class inherits from the Base class; the Derived class extends the Base class; the Derived class is a specialization of the Base class. The Base class is a generalization of its Derived classes; one could say, In general, all Pets have names. ( I ) Object- Oriented Fundamentals - 17

Inheritance Implied by is-a-kind-of relationships. A square is-a-kind-of shape (uses inheritance). Leroy is-a dog (doesn t use inheritance). Class Y is like class X except for the following differences The derived class may provide additional state or behavior, or it may override the implementation of inherited methods. Liskov substitution principle: If Y is a subclass of X, then it should be possible to use any instance of Y wherever any instance of X is used. ( I ) Object- Oriented Fundamentals - 18

Questions and Shapes Imagine a system that asks a series of questions: YesNoQuestion, NumericQuestion, FreeTextQuestion It simplifies things to treat these uniformly, each as a specialization of Question. The program will maintain a list of Questions, and invoke asktheuser( ) for each. Consider a system that manipulates various kinds of shapes: Sometimes you don t care what shape you have (example: move). Sometimes you do care (example: draw). ( I ) Object- Oriented Fundamentals - 19

Polymorphism The ability of two or more classes to respond to the same message, each in its own way. The ability to use any object which implements a given interface, where the specific class name need not be specified. Example: question.asktheuser(); To be useful, the responses should be similar in nature. Made possible via dynamic (run-time) binding. ( I ) Object- Oriented Fundamentals - 20

Java Example // File: Derived.java // What will the following Java code output to the screen? class Base { void foo() { System.out.println( "Base foo" ); } void bar() { System.out.println( "Base bar" ); } } public class Derived extends Base { void bar() { System.out.println( "Derived bar" ); } public static void main( String[] args ) { Derived d = new Derived(); d.foo(); d.bar(); Base b = d; b.bar(); } } ( I ) Object- Oriented Fundamentals - 21

Modeling OO designs begin with an object model involving both the domain experts and software designers alike. One should model the problem domain and users activities. The modeling process pins down concepts and creates a shared vocabulary. Human thinking about complex situations improves with visual aids. A good model is one that shows all the pertinent detail without unnecessary clutter or complexity. Who is the audience for your model? Learn the Unified Modeling Language (UML). ( I ) Object- Oriented Fundamentals - 22

Example: streets, roads, highways Classifications depend on the attributes of interest. Traffic simulator: one-way, two-way, residential, limited access. location w/ respect to business commuters. Maintenance scheduler: surface material. heavy truck traffic. location w/ respect to congressional district. For every class, say, This class is responsible for? ( I ) Object- Oriented Fundamentals - 23

Example: The Sticks Game A program is to be written that allows two people to play a game against each other on a computer. The game consists of a layout with a number of sticks arranged in rows. When the game starts, they are arranged as shown here: 1: 2: 3: 4: ( I ) Object- Oriented Fundamentals - 24

Rules of the Game Players alternate turns. Players remove one or more sticks from any non-empty row. The player who removes the last stick loses. At the start of the game, and after each move, the program displays the state of the game, indicates which player is to move, and prompts that player for a row number and the number of sticks to remove from that row. The program tells the player when a specified move is invalid, allowing the player to try again. Find the classes... Document using CRC cards. ( I ) Object- Oriented Fundamentals - 25

CRC card for Row The CRC approach uses 3x5 index cards, one per Class, which shows its Responsibilities and the other class(es) with which it must Collaborate in order to fulfill each responsibility. In this example, class Row must collaborate with class Stick in order to fulfill its responsibility to display itself. ( I ) Object- Oriented Fundamentals - 26

Reducing Complexity Encapsulation exposes only the public interface, thereby hiding implementation details, thus helping to avoid complex interdependencies in the code. Polymorphism allows different classes with the same interface to be interchangeable, making inheritance useful. Inheritance from abstract classes and/or interfaces serves to reduce complexity by allowing generalizations. Delegation reduces complexity by building more complete or higher-level services from smaller, encapsulated ones. Delegation also provides increased run-time flexibility. ( I ) Object- Oriented Fundamentals - 27

Benefits of OO Components are good... code reuse. Design patterns are good... design reuse. Infrastructure and reusable services are also good. Interfaces are good... essential to design for loose coupling. Interfaces also help to partition human responsibilities. Loose coupling and modularity facilitate extensibility, flexibility, scalability and reuse. Logical changes are naturally isolated thanks to modularity and information hiding (encapsulation). This leads to faster implementation and easier maintenance. OO middleware offers location, platform & language transparencies. All of these abstractions are realized in human terms. ( I ) Object- Oriented Fundamentals - 28