Adapter notes
The problem
A matrix manipulation package provides operations such as addition
and subtraction of matrices. These operations are static which
introduce an interesting twist to the problem. For example, one
cannot introduce an interface or abstract class to define
matrices. Interfaces and abstract classes cannot define static
methods. The class is like:
public class Matrix {
protected Matrix (Matrix m) {...} // shallow copy
public Matrix (double [] [] entry) {...}
public static Matrix plus (Matrix l, Matrix r) {...}
public static Matrix minus (Matrix l, Matrix r) {...}
}
A client expects operations with a different identifier (name), e.g.,
add instead of plus.
public static Matrix add (Matrix l, Matrix r) {...}
public static Matrix sub (Matrix l, Matrix r) {...}
The twist
The matrix manipulation package is given and it should not be modified.
The client should be modified as little as possible.
Observe that the matrix operations are static.
This makes sense. Adding two matrices together should not modify
either addend.
Related patterns
- Delegation: when not to use inheritance
- Proxy: hide one object from another
- Adapter: keep one class independent of another
- Facade: single interface to several classes