Activity: Implement Component
Purpose
- To produce source code according to design classes.
|
| Steps
There is no strict order between the steps. Start implementing the operations, and
implement associations and attributes as they are needed to be able to compile and run the
operations. |
| Input
Artifacts:
|
Resulting Artifacts:
|
| Worker: Implementer |
Follow the Programming Guidelines when implementing the classes.
The primary basis for implementation is the classes, with public operations, attributes
and associations. It is important be aware that not all public operations, attributes and
associations are defined during design.
The secondary basis for implementation is the use-case realizations, which shows how
the classes and objects interact to perform the use case.
It is recommended that you implement the classes incrementally; compile, link and run
some regression tests a couple of times a day.
Before you implement a class from scratch, consider adapting existing implementation
classes, typically by subclassing or by instantiation.
To implement operations, do the following:
- Choose an algorithm
- Choose data structures appropriate to the algorithms
- Define new classes and operations as necessary
- Code the operation
Choose an Algorithm
Many operations are simple enough to be implemented straight away from the operation
and its specification.
Nontrivial algorithms are primarily needed for two reasons: to implement complex
operations for which a specification is given, and to optimize operations for which a
simple but inefficient algorithm serves as definition.
Choose Data Structures Appropriate To the Algorithms
Choosing algorithms involves choosing the data structure they work on. Many
implementation data structures are container classes, such as, arrays, lists, queues,
stacks, sets, bags, and variations of these. Most object-oriented languages, and
programming environments provide class libraries with these kinds of reusable components.
Define New Classes and Operations as Necessary
New classes may be found to hold intermediate results for example, and new low-level
operations may be added on the class to decompose a complex operation. These operations
are often private to the class, that is, not visible outside the class itself.
Code the Operation
Write the code for the operation, starting with its interface statement, e.g. the
member function declaration in C++, subprogram specification in Ada, or method in Visual
Basic. Follow the Programming Guidelines.
There are two basic approaches to implementing an object's states.
- Direct implementation of a state-machine mechanism
This is the most obvious way of implementing a state machine, with a variable that
holds the state.
- Using concurrent processes
An object can be implemented as a task or, light-weight process in the programming
language or operating system. This is the most general approach because it preserves the
inherent concurrency of real objects. Events are implemented using inter-task calls using
the facilities in the programming language or operating system. Use this solution with
care because it might require the introduction of many tasks.
If the object's behavior is complex, in terms of states, then it has often been
described with a state diagram in design. This state diagram will serve as an important
input when you implement the class. For more information, refer to Guidelines: State Diagram.
If a class or parts of a class can be implemented reusing an existing class, use
delegation rather than inheritance.
Delegation means that the class is implemented with the help of other classes. The
class references an object of the other class by using a variable. When an operation is
called, the operation calls an operation in the referenced object (of the reused class),
for actual execution. Thus, you can say that it delegates responsibility to the other
class.
A one-way association is implemented as a pointer - an attribute which contains an
object reference. If the multiplicity is one, then it is implemented as a
simple pointer. If the multiplicity is many, then it is a set of
pointers. If the many end is ordered, then a list can be used instead of a set.
A two-way association is implemented as attributes in both directions, using techniques
for one-way associations.
A qualified association is implemented as a lookup table (for example, a Smalltalk
Dictionary class) in the qualifying object. The selector values in the lookup table are
the qualifiers, and the target values are the objects of the other class.
If the qualifier values must be accessed in order, then the qualifiers can be arranged
into a sorted array or a tree. In this case, access time will be proportional to log N
where N is the number of qualifier values.
If the qualifiers are drawn from a compact finite set, then the qualifier values can be
mapped into an integer range and the association can be efficiently implemented as an
array. This approach is more attractive if the association is mostly full rather than
being sparsely populated and is ideal for fully populated finite sets.
Most object-oriented languages and programming environments provide class libraries
with reusable components to implement different kinds of associations.
Implement attributes either as a variable of a built-in primitive, a reusable component
class, or define a new class. Defining a new class is often more flexible, but introduces
unnecessary indirection. For example, an employee's Social Security number can either be
implemented as an attribute of type String or as a new class.

Alternative implementations of an attribute.
It may also be the case that groups of attributes are combined into new classes, as the
following example shows. Both implementations are correct.

The attributes in Line are implemented as associations to a Point
class.
If a design error is discovered in any of the steps, rework feedback has to be provided
to the design. If the required change is small, and the same individual is designing and
implementing the class, the there is no need for a formal change request. The individual
can do the change in the design.
If the required change affects several classes, for example a change in a public
operation, then a formal change request should be submitted to a CCB (change control
board). See Activity: Fix a Defect.
Before you start unit testing, there are some checks you can do. Testing is more
expensive, so try to do some of the following:
- Always compile the code. Set the compiler's warning level to the most detailed level.
- Mentally check the operations. Read through the code, trying to follow all the paths,
and identify all exception conditions. Do this as soon as anything new is implemented.
- Use tools to check the code for errors. For example, a static code rule checker.
|