Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.facade
Class MathFacade

java.lang.Object
  extended by dk.rode.thesis.facade.MathFacade

@Participant(value="Facade")
public class MathFacade
extends Object

A math facade is a small math library that represents an-easy-to-use high-level interface for complex mathematical functions. (yeah, right...)

Facade usage is thread-safe.

Implementation notes:
The small examples used throughout this thesis does not really justify use of Facade; they are simply not complex enough. Still, this Facade implementation address several issues discussed by Gamma et al. [Gamma95, p.185-193]:

Author:
Gunni Rode / rode.dk

Field Summary
private  Map<Integer,AckermannSequence> ackermann
          A map storing calculated Ackermann values, A(m,n), per m value.
private static MathFacade instance
          The singleton instance to use.
private  ReversiblePrimeSequence primes
          The prime sequence to deliver the prime numbers.
private  UnboundedRandomSequence random
          An unbounded random sequence that uses a Fibonacci sequence as the seed.
 
Constructor Summary
private MathFacade()
          Private constructor as this class is a singleton.
 
Method Summary
 int getAckermannNumber(int m, int n)
          Returns the value of the Ackermann function, A(m,n), for m and n values supplied as m and n, respectively.
static MathFacade getInstance()
          Returns the math library to use.
 BigInteger getNthFibonnaciNumber(int n)
          Returns the n'th Fibonacci number.
 int getNthPrimeNumber(int n)
          Returns the n'th prime number.
 boolean getRandomBoolean()
          Returns a pseudo random boolean value.
 BigInteger getRandomFibonnaciNumber(int n)
          Returns a random Fibonacci number between the first and the n'th number.
 long getRandomNumber()
          Returns a random number.
 int getRandomNumber(int highBound)
          Return a pseudo random number between one and highBound.
 int getRandomNumber(int lowBound, int highBound)
          Return a pseudo random number between lowBound and highBound.
 int getRandomPrimeNumber(int n)
          Returns a random prime number between the first and the n'th prime number.
 String toString()
          Return the string representation of this facade.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ackermann

private Map<Integer,AckermannSequence> ackermann
A map storing calculated Ackermann values, A(m,n), per m value.

Never null.


instance

private static final MathFacade instance
The singleton instance to use.

Never null.


primes

@ParticipantUsage(value="SubsystemClass",
                  type=ReversiblePrimeSequence.class)
private ReversiblePrimeSequence primes
The prime sequence to deliver the prime numbers.

Will "grow in size" as needed.

Lazily created.


random

private final UnboundedRandomSequence random
An unbounded random sequence that uses a Fibonacci sequence as the seed.

Never null.

Constructor Detail

MathFacade

private MathFacade()
Private constructor as this class is a singleton.

Method Detail

getAckermannNumber

public int getAckermannNumber(int m,
                              int n)
Returns the value of the Ackermann function, A(m,n), for m and n values supplied as m and n, respectively.

Parameters:
m - The m value for the Ackermann function; cannot be negative.
n - The n value for the Ackermann function; cannot be negative.
Returns:
The A(m,n) value.
Throws:
IllegalArgumentException - If m or n are illegal.

getInstance

public static final MathFacade getInstance()
Returns the math library to use.

Returns:
The math library; never null.

getNthFibonnaciNumber

public BigInteger getNthFibonnaciNumber(int n)
Returns the n'th Fibonacci number.

Parameters:
n - Identifies the Fibonacci number to return; cannot be smaller than one.
Returns:
The n'th Fibonacci number.
Throws:
IllegalArgumentException - If n is smaller than one.

getNthPrimeNumber

public int getNthPrimeNumber(int n)
Returns the n'th prime number.

Parameters:
n - Identifies the prime number to return; cannot be smaller than one.
Returns:
The n'th prime number.
Throws:
IllegalArgumentException - If n is smaller than one.

getRandomBoolean

public boolean getRandomBoolean()
Returns a pseudo random boolean value.

Returns:
True or false.

getRandomFibonnaciNumber

public BigInteger getRandomFibonnaciNumber(int n)
Returns a random Fibonacci number between the first and the n'th number.

Parameters:
n - The maximum number to return; cannot be smaller than one.
Returns:
A random Fibonacci number between the first and the n'th number.
Throws:
IllegalArgumentException - If n is smaller than one.

getRandomNumber

public long getRandomNumber()
Returns a random number.

Returns:
A random number.

getRandomNumber

public int getRandomNumber(int highBound)
Return a pseudo random number between one and highBound.

Parameters:
highBound - The high bound.
Returns:
The pseudo random number.
Throws:
IllegalArgumentException - If highBound is smaller than one.

getRandomNumber

public int getRandomNumber(int lowBound,
                           int highBound)
Return a pseudo random number between lowBound and highBound.

Parameters:
lowBound - The low bound.
highBound - The high bound.
Returns:
The pseudo random number.
Throws:
IllegalArgumentException - If highBound is smaller than lowBound.

getRandomPrimeNumber

public int getRandomPrimeNumber(int n)
Returns a random prime number between the first and the n'th prime number.

Parameters:
n - The maximum number to return; cannot be smaller than one.
Returns:
A random prime number between the first and the n'th prime number.
Throws:
IllegalArgumentException - If n is smaller than one.

toString

public String toString()
Return the string representation of this facade.

Overrides:
toString in class Object
Returns:
The string representation; never null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.