njit designing for visibility chapter 19 applying uml and patterns craig larman

24
NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Post on 22-Dec-2015

223 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

NJIT

Designing for Visibility

Chapter 19

Applying UML and Patterns

Craig Larman

Page 2: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Objectives

Identify four kinds of visibility Design to establish visibility Illustrate kinds of visibility in the UML

notation

Page 3: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Introduction

Q. What is visibility?

A. Visibility is the ability of one object to see or have reference to another.

Page 4: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Visibility Between Objects

Q. When is visibility necessary? A. To send a message from one object

to another, the receiver object must be visible to the sender, so the sender has to have a pointer or reference to the receiver.

Page 5: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Visibility Between Objects

Example:Q. If A sends messages to B, which

must be visible to which? A. B is visible to A means A can send a

message to B.

Some say that "B is an acquaintance of A".

Page 6: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Visibility Between Objects

Page 7: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Visibility

Visibility is related to the scope: Is one resource (such as an instance) within

the scope of another?

The motivation to consider visibility: For an object A to send a message to an

object B, B must be visible to A.

Page 8: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Four Kinds of Visibility

How visibility can be achieved from object A to object B:

Attribute visibility - B is an attribute of A

Parameter visibility - B is a parameter of a method of A

Local visibility - B is a local object in a method of A

Global visibility - B is in some way globally visible

Page 9: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Attribute Visibility

Attribute visibility from A to B exists when B is an attribute of A Relatively permanent visibility because it

persists as long as A and B exist Common form of visibility

public class Register{…

private ProductCatalog Catalog;…

}

Page 10: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Attribute Visibility

Page 11: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Parameter Visibility

Parameter visibility from A to B exists when B is passed as a parameter to a method of A. Relatively temporary visibility because it

persists only within the scope of the method The 2nd most common form of visibility in the

OO systems

Page 12: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Parameter Visibility

Page 13: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Parameter to attribute Visibility

It is common to transform parameter visibility into attribute visibility.

Page 14: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Local Visibility

Local visibility from A to B exists when B is declared as a local object within a method of A.

Relatively temporary visibility since it persists only within the scope of the method.

Page 15: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Local Visibility

There are two common means by which local visibility is achieved: Create a new local instance and assign it to a

local variable. Assign the returning object from a method

invocation to a local variable. A variation of this method does not explicitly declare a variable, but one implicitly exists as the result of a returning object from a method invocation

Ex: anObject.getAnotherObject.doSomething();

Page 16: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Global Visibility

Global visibility from A to B exists when B is global to A. Relatively permanent visibility since it persists

as long as A and B exist. The least common form of visibility in OO

Systems.

Page 17: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Global Visibility

Ways to achieve global visibility: Assign an instance to a global variable. Use the Singleton pattern

Page 18: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Singleton Pattern (Gang of Four)

Problem: Exactly one instance of a class is needed.

Objects need a single point of access. Solution:

Define a class method that returns the singleton object, instantiating it if it does not exist.

Example: A print queue—many programs must access

one queue

Page 19: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Illustrating Visibility in the UML

Page 20: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Visibility in the UML

Public: Any outside classifier with visibility to the given

classifier can use the feature; specified by pre-pending the symbol “+”

Protected: Any descendant of the classifier can use the

feature; specified by pre-pending the symbol “#”

Private: Only the classifier itself can use the feature;

specified by pre-pending the symbol “-”

Page 21: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Terms: Classifier

A classifier is a mechanism that describes structural and behavioral features. Modeling elements that can have instances

are called classifiers. Classifiers include classes, interfaces,

datatypes, signals, components, nodes, use cases, and subsystems.

A classifier has structural feature (in the form of attributes), as well as behavioral features (in the form of operations).

Page 22: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Terms: Feature

A feature is a property, such as operations or attributes that is encapsulated within entity such as an interface, a class, or a datatype.

Page 23: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Questions & Answers

Q. Which would you use if you wanted a relatively permanent connection?

A. attribute, or global Q. Which would you use if you didn't want

a permanent connection? A. parameter, or local Q. How would you create a local visibility? A. create a new instance - use result of a

method call

Page 24: NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman

Questions & Answers

Q. how would you achieve a global visibility?

A. use a global variable in C++, static (or class) variable (in C++ or Java) - use the Singleton pattern (a static method that returns the object)