Chapter 3 Idioms - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Chapter 3 Idioms

Description:

Idiom. Low-level pattern specific to a programming language. ... Describing Idioms. Design Patterns are described ... Idiom // Example ReadLock. void ... – PowerPoint PPT presentation

Number of Views:180
Avg rating:3.0/5.0
Slides: 18
Provided by: brahimm
Category:
Tags: chapter | idiom | idioms

less

Transcript and Presenter's Notes

Title: Chapter 3 Idioms


1
Chapter 3 - Idioms
CIS 476/566 Software Architecture and Design
Patterns
  • Dr. Brahim Medjahed
  • brahim_at_umich.edu

2
Outline
  • Major Types of Patterns
  • Idioms

3
Three Major Types of Patterns
  • Architectural Patterns
  • Design Patterns
  • Idioms

4
Architectural Pattern
  • Expresses a fundamental structural organization
    or schema for software systems.
  • Provides a set of predefined subsystems,
    specifies their responsibilities, and includes
    rules and guidelines for organizing the
    relationships between them.

5
Design Pattern
  • Provides a scheme for refining the subsystems or
    components of a software system, or the
    relationships between them.
  • Describes commonly recurring structure of
    communicating components that solves a general
    design problem within a particular context.

6
Idiom
  • Low-level pattern specific to a programming
    language.
  • It describes how to solve implementation-specific
    problems in a programming language
  • Example memory management in C

7
Describing Idioms
  • Design Patterns are described
  • By means of software design constructs, for
    example objects, classes, inheritance,
    aggregation and use-relationship.
  • Idioms are described
  • By means of programming language constructs.

8
Example Single Thread Execution
  • Reason
  • To prevent concurrent access to data or other
    resources (aka Critical Section).
  • Synopsis
  • Synchronize on a same object the portions of code
    that should not be concurrently executed.
  • Example
  • Several threads set and get a datum held by an
    object. The time of an access is
    non-deterministic.

9
Idiom
  • class HoldDatum
  • private Datum datum
  • public synchronized void set (Datum d)
  • datum d
  • public synchronized Datum get ()
  • return datum

10
Guarded Suspension
  • Reason
  • A method should wait to execute until a
    precondition holds.
  • Synopsis
  • Use an object's methods wait and notify.
  • Example
  • A queue is used by two threads to exchange
    objects. A thread can take an object from the
    queue only if the queue contains elements.

11
Idiom
  • // Code requiring pre-condition
  • synchronized void guardedMethod ()
  • while (! precondition)
  • wait ()
  • // code requiring precondition
  • // Code that changes pre-condition
  • synchronized void stateChangingMethod ()
  • // code possibly changing precondition
  • notify ()
  • // Both methods are synchronized on same object

12
Balking
  • Reason
  • Disregard the call to a method of an object, if
    the state of the object is not appropriate to
    execute the call.
  • Synopsis
  • Test the object's state, in a synchronized block
    for thread safety, and return if the state is
    inappropriate.
  • Example
  • A toilet with an automatic flusher should not
    begin a flush cycle when another cycle is in
    progress.

13
Idiom
  • void balkingMethod ()
  • synchronized (this)
  • if (! someCondition)
  • return
  • else
  • ... // code possibly changing some condition
  • ... // do whatever

14
Read/Write Lock
  • Reason
  • To allow concurrent read access to an object, but
    exclusive write access.
  • Synopsis
  • Use a specialized Scheduler whose method enter is
    replaced by two methods readLock and writeLock.
  • Example
  • An on-line auction system where remote users get
    and set bids.

15
Idiom
  • // Example ReadLock
  • void synchronized readLock ()
  • while (pendingWriters activeWriters gt 0)
  • wait ()
  • activeReaders
  • // being reading

16
Idiom (contd)
  • // Example WriteLock
  • void synchronized writeLock ()
  • pendingWriters
  • // block readers
  • while (activeReaders activeWriters gt 0)
  • wait ()
  • pendingWriters--
  • activeWriters
  • // being writing

17
Idiom (Contd)
  • // Example of done()
  • void synchronized done ()
  • if (activeWriters gt 0)
  • activeWriters--
  • else if (activeReaders gt 0)
  • activeReaders--
  • else
  • error ()
  • notifyAll ()
Write a Comment
User Comments (0)
About PowerShow.com