Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
198
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Pattern Variants – Possible implementation alternatives and variations on the pattern.

Related Patterns – Other patterns that are either associated with or closely related to the pattern.

Example – A Java code example.

Software Abstraction and Reuse

or “Run that by me one more time...”

Design patterns represent an important evolutionary step in software abstraction and reuse. These two concepts are central to the idea of programming—some would say they are the two most important ones.

Abstraction represents a way for developers to solve complex problems by breaking them up into progressively simpler ones. The solutions to simpler problems, when “tagged” with a label or name, can then be used as building blocks to solve the more complicated projects that we as developers encounter each day.

Reuse is equally vital to software development. In a sense, the history of software development is marked by a constant search to find progressively more sophisticated ways to reuse code. Why all the interest? What's the motivation? Actually, reuse is a perfectly understandable goal given the nature of software development. After all, given a complicated software project complete with a tight deadline schedule, which would you rather do? (Select the best answer.)

Write all the code from scratch, subjecting yourself and those around you to a slow and painful process of testing and validating everything that you write.

Use proven and tested code as the foundation for your work.

Don't get me wrong—coding is a blast. It's the testing, debugging, documentation, and post-release support that we developers don't generally like all that much. Over the years, we've come up with quite a few ways to reuse code and development concepts.

The earliest kind of reuse was snippet reuse (a.k.a. CaP – Cut and Paste). The less said about this as a method for effective software reuse, the better. Likewise, this approach does not offer any real qualitative benefits in terms of code abstraction.

Algorithmic reuse provided a more general way to manage reuse. You can reuse an algorithm, like searching and sorting, to abstract an approach (usually mathematical) to solving a particular kind of computing problem.

Functional reuse, and its counterpart, data structure reuse, allow you to reuse a coding abstraction more directly. For example, any developer who wants to model something like an address could define a structure with all the necessary fields, then reuse the structure in any project which required an address. Likewise, an operation like computeTax could be defined as a function (or procedure or subroutine or method, depending on the programming language), and subsequently copied as a whole to new projects.

Two extensions of these reuse concepts are the function library and the API. They represent ways to package functionality and make functionality available to future applications without actually having to copy code.

The development of object-oriented languages represents a tremendous evolutionary leap forward in terms of abstraction and reuse. With this technology, an entire generation of more sophisticated ways to get more mileage out of code was born.

The concept of the class as blueprint for objects provided a major advancement by combining two earlier mechanisms: functional and data abstraction. By packaging an entity’s structure (data) with functionality that applies to the entity (behavior), you gain a way to effectively reuse a software element.

Beyond the core concept of the class, object-oriented languages gives us a variety of other ways to leverage existing code. The concepts of subclasses and interfaces, for instance, opened new possibilities for reuse in software development. Finally, groups of classes can be associated with each other and effectively be treated as a logical software component, providing a very powerful model for reuse at the system level.

In the table below, the Reusability heading indicates the repeatability of the approach.

9

Comparing approaches for reuse and abstraction

Type of reuse

Reusability

Abstraction

Genericity

Snippet

Very poor

Nothing

Very poor

Data structures

Good

Data type

Moderate – good

Functional

Good

Method

Moderate – good

Template

Good

Operation to type

Good

Algorithmic

Good

Formula

Good

Class

Good

Data + method

Good

Interface

 

 

 

Polymorphism

 

 

 

Abstract class

 

 

 

Interface

 

 

 

Code library

Good

Functions

Good – very good

API

Good

Utility classes

Good – very good

Component

Good

Group of classes

Good – very good

Design pattern

Excellent

Problem solution

Very good

Abstraction is an indication of what has been abstracted. Genericity shows how easy it is to apply this method without rewriting or modifying code. Note that the reusability of these approaches heavily depends on how effectively the techniques are applied. Clearly, any capability can be used or misused.

Perhaps the most exciting possibility of a design pattern is that it enables us as developers to more effectively apply the other reuse techniques. A pattern can, for example, provide us with guidelines to effectively manage inheritance in a certain situation, or to effectively designate class relationships to solve a specific problem.

Summary

Design patterns are a valuable tool in software development; every developer is able to code more effectively using them. This book presents some of the best known design patterns; there are many, many more. Welcome to the world of patterns.

10