Ooad Unit 2
Ooad Unit 2
Ooad Unit 2
UNIT - 2
Class
• A class is a description of a set of objects that share the same attributes, operations, relationships, and
semantics. Graphically, a class is rendered as a rectangle.
• Names
• Attributes
• Operations
• Responsibilities
Names
• Every class must have a name that distinguishes it from other classes. A name is a textual string. That name
alone is known as a simple name; a qualified name is the class name prefixed by the name of the package in
which that class lives.
Attributes
• An attribute is a named property of a class that describes a range of values that instances of the property may
hold. A class may have any number of attributes or no attributes at all. An attribute represents some property
of the thing you are modeling that is shared by all objects of that class.
Operations
• An operation is the implementation of a service that can be requested from any object of the class to affect
behavior. In other words, an operation is an abstraction of something you can do to an object that is shared by
all objects of that class. A class may have any number of operations or no operations at all.
Responsibilities
• A responsibility is a contract or an obligation of a class. When you create a class, you are making a statement
that all objects of that class have the same kind of state and the same kind of behavior. At a more abstract
level, these corresponding attributes and operations are just the features by which the class’s responsibilities
are carried out.
Common Modeling
Techniques for Class
• Common Uses
• To model the vocabulary of a system
• To model simple collaborations
• To model a logical database schema
Common Modeling
Techniques
Modeling Simple Collaborations
• Identify the mechanism you’d like to model. A mechanism
represents some function or behavior of the part of the
system you are modeling that results from the interaction
of a society of classes, interfaces, and other things.
• For each mechanism, identify the classes, interfaces, and
other collaborations that participate in this collaboration.
Identify the relationships among these things as well.
• Use scenarios to walk through these things. Along the way,
you’ll discover parts of your model that were missing and
parts that were just plain semantically wrong.
• Be sure to populate these elements with their contents. For
classes, start with getting a good balance of
responsibilities. Then, over time, turn these into concrete
attributes and operations.
Modeling a Logical Database Schema
• Identify those classes in your model whose state must
transcend the lifetime of their applications.
• Create a class diagram that contains these classes. You can
define your own set of stereotypes and tagged values to
address database-specific details.
• Expand the structural details of these classes. In general, this
means specifying the details of their attributes and focusing on
the associations and their multiplicities that relate these
classes.
• Watch for common patterns that complicate physical database
design, such as cyclic associations and one-to-one
associations. Where necessary, create intermediate abstractions
to simplify your logical structure.
• Consider also the behavior of these classes by expanding
operations that are important for data access and data integrity.
In general, to provide a better separation of concerns, business
rules concerned with the manipulation of sets of these objects
should be encapsulated in a layer above these persistent
classes.
• Where possible, use tools to help you transform your logical
design into a physical design.
Forward and Reverse Engineering
To forward engineer a class diagram,
• Identify the rules for mapping to your implementation language or languages of choice. This is something
you’ll want to do for your project or your organization as a whole.
• Depending on the semantics of the languages you choose, you may want to constrain your use of certain
UML features. For example, the UML permits you to model multiple inheritance, but Smalltalk permits only
single inheritance. You can choose to prohibit developers from modeling with multiple inheritance (which
makes your models language- dependent), or you can develop idioms that transform these richer features into
the implementation language (which makes the mapping more complex).
• Use tagged values to guide implementation choices in your target language. You can do this at the level of
individual classes if you need precise control. You can also do so at a higher level, such as with collaborations
or packages.
• Use tools to generate code.
• To reverse engineer a class diagram,
• Identify the rules for mapping from your implementation language or languages of choice. This is something
you’ll want to do for your project or your organization as a whole.
• Using a tool, point to the code you’d like to reverse engineer. Use your tool to generate a new model or
modify an existing one that was previously forward engineered. It is unreasonable to expect to reverse
engineer a single concise model from a large body of code. You need to select portion of the code and build
the model from the bottom.
• Using your tool, create a class diagram by querying the model. For example, you might start with one or
more classes, then expand the diagram by following specific relationships or other neighboring classes.
Expose or hide details of the contents of this class diagram as necessary to communicate your intent.
• Manually add design information to the model to express the intent of the design that is missing or hidden in
the code.
ADVANCED
CLASSES
• Classifiers: A classifier is
a mechanism that describes
structural and behavioral
features. Classifiers include
classes, associations,
interfaces, datatypes, signals,
components, nodes, use cases,
and subsystems.
• Visibility: One of the design details you can specify for an attribute or operation is visibility. The
visibility of a feature specifies whether it can be used by other classifiers. In the UML, you can specify
any of four levels of visibility.
• Instance and Static Scope:Another important detail you can specify for a classifier’s attributes and
operations is scope. The scope of a feature specifies whether each instance of the classifier has its own
distinct value of the feature or whether there is just a single value of the feature shared by all instances of the
classifier.
• Multiplicity: The number of instances a class may have is called its multiplicity. Multiplicity is a
specification of the range of allowable cardinalities an entity may assume. In the UML, you can specify the
multiplicity of a class by writing a multiplicity expression in the upper-right corner of the class icon.
• Attributes:
• Operations:
• Template Classes: A template is a parameterized element. In such languages as C++ and Ada, you can
write template classes, each of which defines a family of classes. A template may include slots for classes,
objects, and values, and these slots serve as the template’s parameters. You can’t use a template directly; you
must instantiate it first. Instantiation involves binding these formal template parameters to actual ones.
• Standard Elements:
Common Modeling Techniques
Modeling the Semantics of a Class
• Specify the responsibilities of the class. A responsibility is a contract or obligation of a type or class and is rendered in a note attached
to the class, or in an extra compartment in the class icon.
• Specify the semantics of the class as a whole using structured text, rendered in a note (stereotyped as semantics) attached to the
class.
• Specify the body of each method using structured text or a programming language, rendered in a note attached to the operation by a
dependency relationship.
• Specify the pre- and postconditions of each operation, plus the invariants of the class as a whole, using structured text. These elements
are rendered in notes (stereotyped as precondition, postcondition, and invariant) attached to the operation or class by a
dependency relationship.
• Specify a state machine for the class. A state machine is a behavior that specifies the sequences of states an object goes through during
its lifetime in response to events, together with its responses to those events.
• Specify internal structure of the class.
• Specify a collaboration that represents the class. A collaboration is a society of roles and other elements that work together to provide
some cooperative behavior that’s bigger than the sum of all the elements. A collaboration has a structural part as well as a dynamic part,
so you can use collaborations to specify all dimensions of a class’s semantics.
• Specify the pre- and postconditions of each operation, plus the invariants of the class as a whole, using a formal language such as OCL.
ADVANCED RELATIONSHIPS
A relationship is a connection among things. In object-oriented modeling, the four most important relationships are
dependencies, generalizations, associations, and realizations. Graphically, a relationship is rendered as a path, with
different kinds of lines used to distinguish the different relationships.
Dependencies
• A dependency is a using relationship, specifying that a change in the specification of one thing may affect
another thing that uses it , but not the reverse. Graphically, a dependency is rendered as a dashed line,
directed to the thing that is depended on. Apply dependencies when you want to show one thing using
another.
• First, there are stereotypes that apply to dependency relationships among classes and objects in class
diagrams.
1. bind Specifies that the source instantiates the target template using the given actual parameters.
2. derive Specifies that the source may be computed from the target.
3. permit Specifies that the source is given special visibility into the target.
4. instanceOf Specifies that the source object is an instance of the target classifier. Ordinarily shown using text notation in the
form source : Target
5. instantiate Specifies that the source creates instances of the target.
6. powertype Specifies that the target is a powertype of the source; a powertype is a classifier whose objects are the children of a
given parent.
7. refine Specifies that the source is at a finer degree of abstraction than the target.
8. use Specifies that the semantics of the source element depends on the semantics of the public part of the target
• There are two stereotypes that apply to dependency relationships among packages.
1. import Specifies that the public contents of the target package enter the public namespace of the source, as if they had been
declared in the source.
2. access Specifies that the public contents of the target package enter the private namespace of the source. The unqualified names
may be used within the source, but they may not be re-exported.
• Two stereotypes apply to dependency relationships among use cases:
1. extend Specifies that the target use case extends the behavior of the source
2. include Specifies that the source use case explicitly incorporates the behavior of another use case at a location specified by the
source
• One stereotype you’ll encounter in the context of interactions among objects is
1. send Specifies that the source class sends the target event.
• one stereotype that you’ll encounter in the context of organizing the elements of your system into subsystems
and models is.
1. trace Specifies that the target is a historical predecessor of the source from an earlier stage of development
Generalizations
• A generalization is a relationship between a general classifier (called the superclass or parent) and a more
specific classifier (called the subclass or child).
• The UML defines four constraints that may be applied to generalization relationships:
1. complete Specifies that all children in the generalization have been specified in the model (although some may be elided in the
diagram) and that no additional children are permitted
2. incomplete Specifies that not all children in the generalization have been specified (even if some are elided) and that additional
children are permitted
3. disjoint Specifies that objects of the parent may have no more than one of the children as a type. For example, class Person
can be specialized into disjoint classes Man and Woman.
4. overlapping Specifies that objects of the parent may have more than one of the children as a type. For example, class
Vehicle can be specialized into overlapping subclasses LandVehicle and WaterVehicle (an amphibious vehicle is both).
Associations
• An association is a structural relationship, specifying that objects of one thing are connected to objects of
another.
• For example, a Library class might have a one-to-many association to a Book class, indicating that each
Book instance is owned by one Library instance. Furthermore, given a Book, you can find its owning
Library, and given a Library, you can navigate to all its Books. Graphically, an association is rendered
as a solid line connecting the same or different classes.
• You use associations when you want to show structural relationships.
• There are four basic adornments that apply to an association: a name, the role at each end of the association,
the multiplicity at each end of the association, and aggregation.
• For advanced uses, there are several other properties you can use to model subtle details, such as navigation,
qualification, and various flavours of aggregation.
Navigation
• Navigation Given a plain, unadorned association between two classes, such as Book and Library, it’s
possible to navigate from objects of one kind to objects of the other kind. Unless otherwise specified,
navigation across an association is bidirectional.
Visibility
• Visibility Given an association between two classes, objects of one class can see and navigate to objects of
the other unless otherwise restricted by an explicit statement of navigation.
Qualification
• Qualification In the context of an association, one of the most common modeling idioms you’ll
encounter is the problem of lookup. Given an object at one end of an association, how do you identify an
object or set of objects at the other end?
Composition
• Composition Aggregation turns out to be a simple concept with some fairly deep semantics. Simple
aggregation is entirely conceptual and does nothing more than distinguish a “whole” from a “part.” Simple
aggregation does not change the meaning of navigation across the association between the whole and its
parts, nor does it link the lifetimes of the whole and its parts.
Association Classes
• Association Classes In an association between two classes, the association itself might have
properties. For example, in an employer/employee relationship between a Company and a Person, there is
a Job that represents the properties of that relationship that apply to exactly one pairing of the Person and
Company. It wouldn’t be appropriate to model this situation with a Company to Job association together
with a Job to Person association.
Constraints
• Constraints These simple and advanced properties of associations are sufficient for most of the structural
relationships you’ll encounter. However, if you want to specify a shade of meaning, the UML defines five
constraints that may b
1. ordered Specifies that the set of objects at one end of an association are in an explicit order. e applied to
association relationships.
2. set The objects are unique with no duplicates.
3. bag The objects are nonunique, may be duplicates.
4. ordered set The objects are unique but ordered.
5. list or sequence The objects are ordered, may be duplicates.
6. readonly A link, once added from an object on the opposite end of the association, may not be modified
or deleted. The default in the absence of this constraint is unlimited changeability.
Realizations
• A realization is a semantic relationship between classifiers in which one classifier specifies a contract that
another classifier guarantees to carry out. Graphically, a realization is rendered as a dashed directed line with
a large open arrowhead pointing to the classifier that specifies the contract.
• Realization is different enough from dependency, generalization, and association relationships that it is
treated as a separate kind of relationship. Semantically, realization is somewhat of a cross between
dependency and generalization, and its notation is a combination of the notation for dependency and
generalization.
Common Modeling Techniques
Modeling Webs of Relationships
• Don’t begin in isolation. Apply use cases and scenarios to drive your discovery of the relationships among a
set of abstractions.
• In general, start by modeling the structural relationships that are present. These reflect the static view of the
system and are therefore fairly tangible.
• Next, identify opportunities for generalization/specialization relationships; use multiple inheritance sparingly.
• Only after completing the preceding steps should you look for dependencies; they generally represent more-
subtle forms of semantic connection.
• For each kind of relationship, start with its basic form and apply advanced features only as absolutely
necessary to express your intent.
• Remember that it is both undesirable and unnecessary to model all relationships among a set of abstractions
in a single diagram or view. Rather, build up your system’s relationships by considering different views on
the system. Highlight interesting sets of relationships in individual diagrams.
INTERFACES, TYPES, AND ROLES
• An interface is a collection of operations that are used to specify a service of a class or a component.
• A type is a stereotype of a class used to specify a domain of objects, together with the operations (but not the
methods) applicable to the object.
• A role is the behavior of an entity participating in a particular context.
• Graphically, an interface may be rendered as a stereotyped class in order to expose its operations and other
properties. To show the relationship between a class and its interfaces, a special notation is provided.
• Provided interface (one that represents services provided by the class) is shown as a small circle attached to
the class box.
• Required interface (one that represents services required by a class of another class) is shown as a small
semicircle attached to the class box.
Names
• Every interface must have a name that distinguishes it from other interfaces. A name is a textual string. That
name alone is known as a simple name; a path name is the interface name prefixed by the name of the
package in which that interface lives.
Relationships
• Like a class, an interface may participate
in generalization, association, and
dependency relationships. In addition, an
interface may participate in realization
relationships. Realization is a semantic
relationship between two classifiers in
which one classifier specifies a contract
that another classifier guarantees to carry
out.
Operations
• An interface is a named collection of operations used to specify a service of a class or of a component. Unlike
classes or types, interfaces do not specify any implementation. Like a class, an interface may have any
number of operations. These operations may be adorned with visibility properties, concurrency properties,
stereotypes, tagged values, and constraints.
Common Modeling
Techniques
Modeling the Seams in a System
• Within the collection of classes and components in your system, draw a line
around those that tend to be tightly coupled relative to other sets of classes and
components.
• Refine your grouping by considering the impact of change. Classes or
components that tend to change together should be grouped together as
collaborations.
• Consider the operations and the signals that cross these boundaries, from
instances of one set of classes or components to instances of other sets of
classes and components.
• Package logically related sets of these operations and signals as interfaces.
• For each such collaboration in your system, identify the interfaces it requires
from (imports) and those it provides to others (exports). You model the
importing of interfaces by dependency relationships, and you model the
exporting of interfaces by realization relationships.
• For each such interface in your system, document its dynamics by using pre-
and postconditions for each operation, and use cases and state machines for the
interface as a whole.
Modeling Static and Dynamic Types
• Specify the different possible types of that object by rendering each type as a class (if the abstraction requires
structure and behavior) or as an interface (if the abstraction requires only behavior).
• Model all the roles the class of the object may take on at any point in time. You can mark them with the
«dynamic» stereotype. (This is not a predefined UML stereotype, but one that you can add.)
• In an interaction diagram, properly render each instance of the dynamically typed class. Display the type of
the instance in brackets below the object’s name, just like a state. (We are using UML syntax in a novel way,
but one that we feel is consistent with the intent of states.)
PACKAGES
• A package is a general-purpose mechanism for organizing the model itself into a hierarchy; it has no meaning
to the execution. Graphically, a package is rendered as a tabbed folder.
Names
• Every package must have a name that distinguishes it from other packages. A name is a textual string. That
name alone is known as a simple name; a qualified name is the package name prefixed by the name of the
package in which that package lives, if any. A double colon (::) separates package names.
Owned Elements
• A package may own other elements, including classes, interfaces, components, nodes, collaborations, use
cases, diagrams, and even other packages. Ownership is a composite relationship, which means that the
element is declared in the package. If the package is destroyed, the element is destroyed. Every element is
uniquely owned by exactly one package.
Visibility
• You can control the visibility of the elements owned by a package just as you can control the visibility of the
attributes and operations owned by a class. Typically, an element owned by a package is public, which means
that it is visible to the contents of any package that imports the element’s enclosing package.
Common Modeling
Techniques
Modeling Groups of Elements
• Scan the modeling elements in a particular architectural
view and look for clumps defined by elements that are
conceptually or semantically close to one another.
• Surround each of these clumps in a package.
• For each package, distinguish which elements should be
accessible outside the package. Mark them public, and all
others protected or private. When in doubt, hide the
element.
• Explicitly connect packages that build on others via import
dependencies.
• In the case of families of packages, connect specialized
packages to their more general part via generalizations.
Modeling Architectural Views
• Identify the set of architectural views that are significant in the context of your problem. In practice, this
typically includes a design view, an interaction view, an implementation view, a deployment view, and a use
case view.
• Place the elements (and diagrams) that are necessary and sufficient to visualize, specify, construct, and
document the semantics of each view into the appropriate package.
• As necessary, further group these elements into their own packages.
OBJECT DIAGRAMS
Object diagrams model the instances of things contained in class diagrams. An object diagram shows a set of
objects and their relationships at a point in time.
• An object diagram is a diagram that shows a set of objects and their relationships at a point in time.
Graphically, an object diagram is a collection of vertices and arcs.
• An object diagram is a special kind of diagram and shares the same common properties as all other diagrams
—that is, a name and graphical contents that are a projection into a model. What distinguishes an object
diagram from all other kinds of diagrams is its content.
• Object diagrams commonly contain
• Objects
• Links
• Like all other diagrams, object diagrams may contain notes and constraints.
Common Modeling
Techniques
Modeling Object Structures
• Identify the mechanism you’d like to model. A mechanism
represents some function or behavior of the part of the system you
are modeling that results from the interaction of a society of classes,
interfaces, and other things.
• Create a collaboration to describe a mechanism.
• For each mechanism, identify the classes, interfaces, and other
elements that participate in this collaboration; identify the
relationships among these things as well.
• Consider one scenario that walks through this mechanism. Freeze
that scenario at a moment in time, and render each object that
participates in the mechanism.
• Expose the state and attribute values of each such object, as
necessary, to understand the scenario.
• Similarly, expose the links among these objects, representing
instances of associations among them.
Reverse Engineering
• Chose the target you want to reverse engineer. Typically, you’ll set your context inside an operation or
relative to an instance of one particular class.
• Using a tool or simply walking through a scenario, stop execution at a certain moment in time.
• Identify the set of interesting objects that collaborate in that context and render them in an object diagram.
• As necessary to understand their semantics, expose these object’s states.
• As necessary to understand their semantics, identify the links that exist among these objects.
• If your diagram ends up overly complicated, prune it by eliminating objects that are not germane to the
questions about the scenario you need answered. If your diagram is too simplistic, expand the neighbors of
certain interesting objects and expose each object’s state more deeply.
• You will usually have to manually add or label structure that is not explicit in the target code. The missing
information supplies the design intent that is only implicit in the final code.