11
TK2023 Object- Oriented Software Engineering CHAPTER 15 Designing for Visibility

15 Visibility

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 15 Visibility

TK2023 Object-Oriented Software Engineering

CHAPTER 15

Designing for Visibility

Page 2: 15 Visibility

INTRODUCTION

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

A sender object can send a message to a receiver object only if the receiver is visible to the sender.

Example:

The Sale object is visible to the Register object.

: Register : Sale

1: setComplete(true)

Page 3: 15 Visibility

When creating a design of interacting objects, it is necessary to ensure that the necessary visibility is present to support message interaction.

There are four common ways to make an object visible to another: Attribute visibility Parameter visibility Local visibility Global visibility

Page 4: 15 Visibility

ATTRIBUTE VISIBILITY Attribute visibility from A to B exists when B is an

attribute of A. It is a relatively permanent visibility as it persists as

long as A and B exist. Example:

class Register {private ProductCatalog catalog;…public void enterItem(String itemID, int qty) {

…desc =

catalog.getProductDescription(itemID);…

}}

Page 5: 15 Visibility

PARAMETER VISIBILITY Parameter visibility from A to B exists when B is passed

as a parameter to a method of A. It is a relatively temporary visibility because it persists

only within the scope of the method. Example:

class Sale {

public void makeLineItem(ProductDescription pd, int qty) {

sl = new SalesLineItem(pd, qty);

}

}

Page 6: 15 Visibility

It is common to transform parameter visibility to attribute visibility. For example,

class SalesLineItem {

private ProductDescription desc;

public void makeLineItem(ProductDescription pd, int qty) {

desc = pd;

}

}

Page 7: 15 Visibility

LOCAL VISIBILITY

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

Like parameter visibility, it is a relatively temporary visibility as it persists only within the scope of the method.

Page 8: 15 Visibility

Example 1:class Store {

public Store() {

ProductCatalog pc = new ProductCatalog();

}

}

Page 9: 15 Visibility

Example 2:public class MyApplet extends JApplet {

public void init() {

Container contentPane = getContentPane();

contentPane.add(btnNext);

}

}

Page 10: 15 Visibility

Like parameter visibility, it is common to transform local visibility to attribute visibility. For example,

class Store {

private ProductCatalog catalog;

public Store() {

ProductCatalog pc = new ProductCatalog();

catalog = pc;

}

}

Page 11: 15 Visibility

GLOBAL VISIBILITY Global visibility from A to B exists when B is global

to A. It is a relatively permanent visibility as it persists as

long as A and B exist. This is the least common form of visibility in object-

oriented systems. One way to achieve global visibility is to assign an

object to a global variable. This is possible for languages like C++ but not for others, such as Java.

Another (much preferred) way to achieve global visibility is to apply the Singleton pattern.