Design Patterns - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Design Patterns

Description:

... meaning they result in a physical directory in the file system. ... Anytime a class uses another class in some fashion, a dependency exists between the two. ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 40
Provided by: ell166
Category:
Tags: design | patterns

less

Transcript and Presenter's Notes

Title: Design Patterns


1
Design Patterns Frameworks Chapter 1 UML
JAVA
  • Conducted By Andy LAI

2
Introduction
  • UML and Java may be languages for software
    development, but they exist in different planes
    of reality. UML occupies the visual world,
    whereas Java is textual in nature.
  • UML is also richer than Java in the sense that it
    offers more abstract and powerful ways of
    expressing a particular concept or relationship.
    However, there is generally only one way to
    represent that concept or relationship in the
    Java language. For example, a Java variable
    declaration can be expressed in multiple ways in
    UML.

3
Introduction cont.
  • This chapter provides an overview of some key UML
    concepts related to classes and how they relate
    to the implementation world. The primary purpose
    is to review the basic mapping for the benefit of
    those who may be new to the UML world. A
    secondary purpose is to identify ways in which
    the use of UML notation can effectively enhance
    the significance of a specific piece of Java code
    without actually altering the equivalent Java
    code.

4
Topics Representing Structure
  • 1. Class
  • In the UML, a Java class is represented via a
    compartmentalized rectangle. Three horizontal
    compartments are used
  • Name compartment Shows the Java class name.
  • Attribute compartment Lists variables defined on
    the class, if any
  • Operations compartment Shows methods defined on
    the class, if any

5
  • An abstract class is identified by italicizing
    the class name. A stereotype may be used
    alongside a class name to unambiguously identify
    it as a specific type of Java class, such as an
    applet. You can also use stereotypes to identify
    specific types of class (such as ltlt Business
    Entity gtgt) in your particular domain vocabulary
    to make the classes more meaningful wherever they
    appear. Note that the tool may use the
    stereotyping mechanism to affect code generation.

6
2. Variable
  • In Java variables may manifest themselves in
    various UML. The simplest form of variable
    declaration is to list it within a classs
    attribute compartment.
  • VisiblityIndicator name type
  • Underlining the attribute indicates the static
    nature of the variable.

7
  • The visibility of an attribute is indicated by
    preceding the attribute with for public, for
    protected, and for private. The figure below
    shows a class with attributes.
  • Variables may also manifest themselves due to an
    objects relationships with other objects.

8
  • The two variables shown in the class are private
    variables. The name of first variable is instance
    and its type is AutioClipManager. An initial
    value can be indicated for a variable by
    following the variables type with an equal sign
    followed by the value like this
  • ShutDownBoolean false
  • If a variable is underlined, that means it is a
    static variable.

9
3. Method
  • Underlined methods are static methods. The
    methods in the bottom compartment are shown as
    follows
  • VisiblityIndicator name ( formalParameters )
    returnType
  • The getInstance method returns an
    AudioClipManager object. The UML indicates a void
    method by leaving out the returnType from a
    method to indicate that it does not return
    anything. Therefore the stop method method shown
    is the class does not return any result.

10
  • A stereotype is used like an adjective to modify
    what comes after it. The constructor stereotype
    indicates that the methods that follow it are
    constructors. The misc stereotype indicates that
    methods that come after it are regular methods.
  • If an ellipsis ( ) appears in the bottom
    compartment of a class, it means that the class
    has additional methods not shown in the diagram.
    If an ellipsis appears in the middle compartment
    of a class, the class has additional variables
    not shown in the diagram.

11
  • 3.1 Visibility Indicators Omitted
  • When a method or variable is shown without a
    visibility indicator, that means only that there
    is no indication of the methods or variables
    visibility. It does not imply that the methods or
    variables are public, protected, or private.

12
  • 3.2 Method parameters Omitted
  • A methods parameters may be omitted if their
    return values are also omitted.

13
4. Two-Compartment Class and One-Compartment
Class
  • If a class is drawn with only two compartments,
    it simply means that its variables are not shown.
    It does not mean that it has no variables.

AudioClipManager
  • The simplest form of a class contains only one
    compartment with the class name.

14
5. Object
  • Although both Java and UML have the concept of an
    object, there is no direct mapping between a UML
    object and Java code. This is because objects are
    dynamic entities, which are based on class
    definitions. Java Applications are written in
    terms of Java classes that result in the creation
    of Java objects when the application is actually
    executed.
  • In UML, objects are used to model dynamic aspects
    of the system via interaction diagrams.

15
(No Transcript)
16
6. Interface
  • In the UML, a Java interface is depicted as a
    class stereotyped with ltltinterfacegtgt. Stereotyped
    classes may optionally have icons associated with
    them. In the case of an interface, the UML iconic
    representation is a small circle.
  • An interface is just a collection of operations
    with no implementations and no data or state.

17
  • Complete decoupling of an objects interface and
    implementation provides the highest degree of
    adaptability, so
  • Clients see only the interface
  • The implementation can be replaced at any time
  • Implementation changes are transparent to
    clients.
  • Interfaces are a sign of good, adaptable designs.
    Many design patterns use interfaces.

18
7. Package
  • A Java package maps to a UML package. Packages
    may be logical, meaning you may only use them as
    a grouping mechanism. Packages can also be
    physical, meaning they result in a physical
    directory in the file system.

19
  • Packages may be stereotyped to distinguish the
    type of package, for example, using ltltsubsystemgtgt
    to identify the package as a subsystem. A
    subsystem refers to a group of UML elements and
    represents a behavioral unit in a model. It can
    have interfaces as well as operations. Subsystems
    are typically significant from an analysis and
    design perspective. There is no direct mapping
    between a subsystem and a Java language
    construct.

20
Topics Representing Relationships
  • Relationships play a key role in capturing and
    modeling the important structural aspects of a
    Java application.

21
  • 1. Inheritance
  • The UML concept of generalization is analogous to
    inheritance in Java. Generalization maps directly
    to the extends keyword and is shown visually via
    a line with a triangle at the end nearest the
    super class.

22
  • 2. Realization
  • In Java, a class may implement one or more
    interfaces. The Java keyword implements maps to
    the concept of realization in UML.
  • In the UML, realization can be shown in two
    different ways. If the stereotyped class approach
    is used for representing an interface,
    realization is shown via a dashed line with a
    triangle at the end touching the interface.

23
  • If the circle notation is used for an interface,
    a plain, solid line connecting the interface and
    the implementation class is used.

24
  • An operation with no implementation in a class is
    italicizied
  • Coded as an abstract function in Java
  • The corresponding class is italicized, too.
    Why?
  • Employee is not an interface
  • But MouseListener is
  • All operations are abstract
  • There is no data
  • MouseAdapter implements the MouseListener
    interface
  • Provides empty implementations of all methods

25
  • 3. Dependency
  • Anytime a class uses another class in some
    fashion, a dependency exists between the two. The
    relationship is that of the use depending on the
    class that is using. In UML, a dependency is
    shown via a dotted line with an arrow touching
    the class that is causing the dependency.
  • A dependency exists if a class
  • Has a local variable based on another class
  • Has a reference to an object directly
  • Has a reference to an object indirectly, for
    example, via some operation parameters
  • Uses a classs static operation

26
  • Dependency relationships also exist between
    packages containing classes that are related.
    Dependencies between packages are shown via a
    dotted line with an arrowhead.

27
  • 4. Association
  • Association models the relation has between
    objects
  • The representation of Association depends on
    the stage of design and implementation
  • At a conceptual design stage, a Customer can have
    one or more Orders, and each Order is for just
    one Customer
  • At the specification and implementation satge,
    data members and operations are defined

28
  • Responsibility and greater detail about the
    association become more important as the design
    is refined
  • At a specification design stage, navigability is
    added
  • Refined use cases have identified that an agent
    needs to locate
  • Both current and past orders from a customer
  • The customer who placed an order, but only if it
    is a current order
  • At an implementation stage, all data members and
    operations are needed
  • Navigability implies a pointer or reference

29
  • A unidirectional association implies that
    an object of the class from which the arrow is
    originating may invoke methods on the class
    towards which the arrow is pointing. In Java,
    this manifests itself as an instance variable on
    the class that may invoke methods.

30
  • A bidirectional association simply means
    that either object in the association may invoke
    methods on the other. In Java, this results in an
    instance variable on each class based on the type
    of the other class.

31
  • From an implementation perspective in Java,
    the roles may be appropriate as the names of the
    instance variables in the respective classes. It
    is perfectly reasonable to leave it unnamed. In
    such a case, the role name can simply be based on
    the name of the class.

32
  • A multiplicity example is shown. For
    example, assume that a corporation employs
    several persons, and a person can work for a
    maximum of three corporations.

33
  • Information relevant to the association
    roles cannot always reside with the classes
    involved in the association. For instance, it
    would be inappropriate to store the session
    between a shopper and the virtual shopping cart
    in either class. In such a case, an association
    class may be used to model this situation.

34
  • 5. Aggregation
  • Aggregation is a stronger form of association. It
    is used to show a logical containment
    relationship, that is, a whole formed of parts.
    Although the parts may exist independently of the
    whole, their existence is primarily to form the
    whole. For example, a computer may be modeled as
    an aggregate of a mother-board, a CPU, and I/O
    controller, as so on. Note that the I/O
    controller may exist independently.

35
  • Aggregation models the object relation part of,
    or is composed of
  • An OrderItem is part of an Order
  • From an implementation viewpoint, this implies
    that the only one way to access an OrderItem is
    through the Order

36
  • Unlike association instances, instance of an
    aggregation cannot have cyclic links. That is, an
    object may be directly or indirectly be part of
    itself. For example, if an instance of A
    aggregates an instance of B, then that instance
    of B cannot itself aggregate that same instance
    of A.

37
  • 6. Composition
  • Composition is appropriate for modeling situation
    that call for physical containment. It implies a
    much stronger whole-part coupling between the
    participants such that parts cannot exist without
    the whole. That is parts share the life cycle of
    the whole. They are created when the whole comes
    to life and destroyed when the whole ceases to
    exist.
  • Composition is shown in the same way as
    aggregation except that the diamond is filled in.

38
  • 7. Reflexive Relationships
  • A class may have an association with itself. For
    example, if a person employs another person, the
    Person class may have an association with itself
    with the role names of employer and employee.
    Such a relationship is called a reflexive
    relationship. It would be perfectly acceptable to
    show two separate Person class icons with
    relation drawn between them.

39
Question Answer Session
Write a Comment
User Comments (0)
About PowerShow.com