Chapter Seven - Design by Abstraction

Download Report

Transcript Chapter Seven - Design by Abstraction

George Blank
University Lecturer
CS 602
Java and the Web
Object Oriented Software Development
Using Java
Chapter 7
The Toolbox Chapter
• The program segments and patterns in
chapter seven are very common tools in a
programmer’s toolkit. You can think of them in
the way that an auto mechanic thinks of a set
of wrenches.
Reuse problems
• The concept of reuse conflicts strongly with
commercial copyrights, patents, and
academic standards of plagiarism.
• The ideal approach is to use only an
algorithm, and write your own code. Even
then, you have to worry about reusing code
you wrote for one employer at another.
Reuse continued
• For an example of the issues, see O’Reilly
policies at
http://www.oreilly.com/pub/a/oreilly/ask_tim/2
001/codepolicy.html
• I recommend a comment in any code you are
likely to reuse indicating the source.
• In this course, I don’t care where you get
code, or how much you use, as long as you
give credit.
Singleton Pattern
(Gang of Four)
• Problem:
– Exactly one instance of a class is needed. Objects
need a single point of access.
• Solution:
– Define a class method that returns the singleton object,
instantiating it if it does not exist.
Chapter 7 programs
• The programs in this chapter are particularly
likely to be useful to software professionals
who use Java. It is worth doing them even if
they are not required homework
• Singleton pattern
• Animation
• Plotting
Abstract Coupling
• The elements in section 7.3 are used
constantly, so they belong in your toolkit.
• Enumerations
• Direct Access
• Linked Lists
• Iterators
• Sorting Algorthms
Factory Design Patterns
• Problem:
– Need a generic way to allow to create objects.
• Solutions:
– Factory pattern (section 7.4.3)
– Abstract factory pattern (section 10.2.2)
– Factory method (section 9.5.6)
• It is worth your time to compare these
solutions to better understand each of them.
Minimize interfaces (p. 298)
• Keep your interfaces as small as possible to
maintain low coupling and high cohesion.
• Large interfaces with a lot of methods and
parameters usually indicate high coupling and
high complexity, and are hard to maintain.
Summary
• If you understand and apply the concepts
listed in the chapter summary, you will have a
good start toward becoming an effective and
efficient programmer.
• In particular, a solid understanding of the
design guidelines listed on the next slide is
expected of a software professional.
Design Guidelines
•
•
•
•
•
Refactor recurring code segments
Maximize extensibility
Prevent misuses by clients
Program to an interface, not implementation.
Separate functionalities with different
concerns.
• Minimize the interface.
Bibliography
• Jia, Xiaoping, Object Oriented Software
Development Using Java. Addison Wesley,
2003