By Rick Mercer with help from - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

By Rick Mercer with help from

Description:

This class is essentially closed for modification. ... 'Closed For Modification' - Extending the behavior of the module does not result ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 14
Provided by: rickmercer
Category:

less

Transcript and Presenter's Notes

Title: By Rick Mercer with help from


1
Open-Closed Principle
  • By Rick Mercer with help from
  • C Report, Robert Martin's Engineering Notebook

2
Abstraction
  • Ch 13 Relationships Among Classes starts like
    this
  • Inner classes
  • Sun added them mostly for the listener event
    model
  • Packaging
  • Organize classes and interfaces into a logical
    unit
  • Java 5's sdk now has over 160 packages
  • Inheritance Relationships
  • Class inherits methods and variables from its
    parent class
  • May declare abstract methods
  • Interfaces
  • Make sure a class implements certain operations

3
Why interfaces and abstract classes?
  • "All systems change during their life-cycles.
    This must be born in mind when developing system
    expected to last longer than the first version",
    Ivar Jacobsen
  • Software entities (classes modules, functions,
    etc) should be open for extension but closed for
    modification, Bertrand Meyer 1988
  • Now known as the Open-Closed principles

4
Modules that never change?
  • If a change to one module (class or method)
    results in a cascade of changes to other modules.
    you have bad design
  • When requirements change (and they always do),
    your design should allow devs to extend the
    behavior by adding new code, good design, not by
    changing the behavior of existing code

5
Open and Closed?
  • The behavior of a module should be extended--as
    in adding behavior
  • can add extensions to a house leaving the other
    rooms intact
  • You will be using the abstract classes and
    extends to do this, or interfaces and implements
  • The source code of this kind of module can not be
    changed

6
One Design
  • Imagine Client represents any module that depends
    on some other module named Server

7
Abstraction
  • Client depends on an abstraction that does not
    change. It could be an abstract class or a Java
    interface

8
Example Strategy Pattern
  • Previous UML showed the general form
  • Next slide shows has UML diagram of a specific
    application of the Strategy pattern
  • It is also an application of the open closed
    principle
  • Why?
  • Which class plays the role of client?
  • Which class plays the role of AbstractServer?
  • Note IBM Rational Software Architect has ltltusegtgt
    for dependency

9
(No Transcript)
10
Other Examples
  • This design supports the Open-Closed Principle
    because implementing a new Strategy for deciding
    which move to make does not require a change to
    the Player Class. 
  • This class is essentially closed for
    modification. 
  • However, because we can pass in an object
    that implements ComputerStrategy, the class is
    open for extension. 
  • Extending Player (the client) only requires new
    modules (classes) be written that implement
    ComputerStrategy,
  • There are no changes to the existing Player

11
Other Examples
  • Eclipse
  • Plugins can be added to Eclipse with no changes
    to Eclipse
  • A plugin "extends" Eclipse once you figure out
    where the "Extension Points" are (see plug in
    page)
  • We are using Open-Closed principle in SpamFilter
  • A module depends on List rather than a concrete
    class like ArrayList, LinkedList, or Vector
  • If you change ArrayList or add a new List class,
    you do not need to change RuleParser or Message
  • Sorting elements with a comparator

12
Abstract Classes work too
  • Abstract classes allow some of the non changing
    implementation to exist for the subclasses
  • Example AbstractList implements List
  • From a blog about the Open-Closed principle
  • Every good application is probably teeming with
    abstract base classes for this very thing, and I
    know of 3 open-source applications ... where you
    can find it
  • DotNetNuke - Provider model for the data access
    layer
  • Community Server - Provider model for the data
    access layer
  • Enterprise Library - Used in every application
    block to add new functionality

13
Open-Closed Principle Summary
  • Software entities (classes modules, functions,
    etc) should be open for extension but closed for
    modification, Bertrand Meyer 1988
  • "Open For Extension" - It is possible to extend
    the behavior of the module as the requirements of
    the application change (i.e. change the behavior
    of the module).
  • "Closed For Modification" - Extending the
    behavior of the module does not result in the
    changing of the source code or binary code of the
    module itself.
  • Strategy pattern is one example of this Principle
Write a Comment
User Comments (0)
About PowerShow.com