Object Oriented Design Principles PowerPoint PPT Presentation

presentation player overlay
1 / 18
About This Presentation
Transcript and Presenter's Notes

Title: Object Oriented Design Principles


1
Object Oriented Design Principles
  • Procedural Abstraction
  • Cohesion and Coupling
  • What makes a good Java program?

2
Elements of Software Design
  • The Time class illustrated
  • procedural abstraction
  • encapsulation
  • data abstraction
  • cohesion
  • coupling
  • We will examine each one in turn

3
Procedural Abstraction
  • procedural abstraction the organisation of a
    program's computation into methods
  • factorisation identify recurring segments of
    code and capture them once in a single method
  • top-down decomposition design a complex method
    by breaking it up into smaller chunks
  • bottom-up composition write some simple methods
    and then use them to design more complex ones

4
Encapsulation
  • encapsulation hiding implementation details
    behind a public interface
  • e.g. a microwave oven you don't need to see
    (and don't get to see) the internal workings
    all you get is the buttons and dials on the front
  • a class is the interface plus the implementation
  • interface the signatures of public methods (and
    declarations of public data)
  • implementation the method bodies, private data
    (and private methods

5
Data Abstraction
  • data abstraction separate the details of how a
    compound object is used from the primitive data
    objects that represent it
  • programs should be designed to use abstract data
  • they should not assume anything about the details
    of the data that is not required to perform the
    task
  • concrete implementations of the data can then be
    provided separately, and can be changed without
    affecting the rest of the program

6
Cohesion and Coupling
  • cohesion the degree to which the computation in
    a method is directed towards a single purpose
  • high cohesion is desirable
  • write separate methods for separate purposes
  • convoluted method names are a sign of low
    cohesion
  • coupling the degree to which methods rely on
    each other
  • low coupling is desirable
  • don't pass unnecessary parameters
  • don't communicate by shared variables
    unnecessarily

7
Procedural abstraction the benefits
  • Greater readability
  • detail is hidden behind well-named methods
  • the whole program may be shorter, individual
    methods may be shorter, the contribution of a
    method should be obvious from its signature
  • Greater maintainability
  • easier to read makes it easier to maintain
  • reduced repetition means fewer inconsistent
    changes

8
... and more benefits
  • Greater testability
  • the program is made up of smaller independently
    testable and de-buggable pieces
  • less repetition means less code to be tested
  • Greater re-usability
  • each smaller method does some single well-defined
    task that may be needed in other parts of the
    program

9
... and possibly one more?
  • better performance?
  • although we now have the overhead of extra method
    invocations, the overhead is small
  • it may be easier to find bottlenecks if the code
    is modularised
  • improving the code in a bottleneck will improve
    overall performance if that code is re-used many
    times

10
Understanding how Java works
  • in order to produce high-quality software, you
    must understand how Java works internally
  • how objects are created from class definitions
  • how variables are represented as blocks of memory
  • how objects are represented as blocks of memory
  • how references are used to identify objects
  • how references are passed between methods
  • how static variables are represented
  • how static methods are called
  • duration, scope and visibility of data and
    methods

11
Good use of variables
  • use each variable that you declare
  • initialise each variable properly
  • use each variable for one purpose only
  • minimise scope of identifiers
  • minimise duration of identifiers
  • minimise visibility of identifiers
  • give sensible names for identifiers
  • used named constants (using final)

12
Java Garbage Collection
  • basic variables have a predefined duration
    objects do not
  • there is no facility to destroy objects in Java
  • Java collects garbage" objects that are no
    longer needed
  • it automatically detects objects that no longer
    have any references to them
  • un-used objects are not deleted immediately
  • when java decides that the amount of free memory
    is too low
  • when the program terminates
  • when you invoke System.gc()
  • Java's use of garbage collection can make Java
    unsuitable for time-critical applications

13
How do you get good software?
  • use a good development process
  • you must design and test as well as implement
  • produce a good design
  • procedural abstraction and data abstraction
  • high cohesion, low coupling
  • use consistent coding conventions
  • produce readable code
  • continually review the quality

14
Readability
  • readability is essentially about consistency and
    convention
  • a scheme for identifier names
  • a scheme for alignment, indentation and spaces
  • a scheme for organising the class definition
  • self-documenting code
  • good variable names
  • good method names
  • good comments

15
Portability
  • Java was originally design for use on the
    Internet, so portability was paramount
  • "write once, run anywhere"
  • The Java approach
  • the Java language specification,
  • which is unusually comprehensive (specifies the
    internal representation for int, etc.),
  • is protected by law
  • Java uses bytecode ...

16
Compilers and Interpreters
  • a compiler translates a program written in some
    language into some other format usually
    executable code
  • an interpreter takes each statement of a program
    in turn, translates it, and executes the
    resulting machine instruction
  • just-in-time compilers translate methods one at a
    time just before they are called.
  • java compiles into bytecode this is the same
    regardless of what machine you are compiling on

17
Java Bytecode
  • Bytecode is the "machine code" of the Java
    Virtual Machine (JVM)
  • The JVM is abstract it doesn't necessarily
    exist
  • There are different ways of getting a machine to
    act like the virtual machine (i.e. to understand
    the bytecode)
  • interpret it
  • just-in-time compile it
  • physically implement it on a chip
  • The bytecode is machine independent your
    ".class" files compiled on Linux will run on a PC
    or a Mac, as long as an appropriate JVM simulator
    is installed (e.g. on a PC, java.exe in the Java
    SDK)

18
Next lecture ...
  • Testing
Write a Comment
User Comments (0)
About PowerShow.com