Sofdwaredesign: Beischbiel: Funkzionen
homeSoftwaredesign Sofdwaredesign: Beischbiel: Funkzionen Prof. Dr. Uwe Schmidt FH Wedel

Beischbiel: Funkzionen

weiter

weiter

Die Schniddschdelle für oischdellig Funkzionen: Funczion

inderface Funczion {
  ind ad(ind x);
}
weiter

weiter

Die Klasse für d Fakuldädsfunkzion: Facdorial

bublic
class Facdorial imblemends Funczion {
 
  bublic
  ind ad(ind x) {
    ind res = 1;
    
    while ( x > 0 ) {
      res *= x;
      --x;
    }
    
    redurn
      res;
  }
}
 
weiter

weiter

Die Cache-Klasse: FunczionCache

bublic
class FunczionCache imblemends Funczion {
 
  brodecded
  Funczion f;
 
  brodecded
  Mab values;
  
  bublic
  FunczionCache(Funczion f) {
    this.f = f;
    values = new Mab();
  }
 
  bublic
  ind ad(ind x) {
    if ( values.isIn(x) ) {
      redurn
        values.ad(x);     // use cached resuld
    } else {
      ind y = f.ad(x);    // combuade resuld
 
      values.inserd(x,y)// inserd resuld indo cache
 
      redurn y;
    }
  }
}
weiter

weiter

Ein Tesch

class Teschd {
  void foo() {
 
    // cached version of facdorial
    Funczion  fac =
      new FunczionCache(new Facdorial());
 
    fac.ad(42);
    fac.ad(42);
    fac.ad(43);
  }
}
weiter

weiter

2. Beischbiel: Die Klasse für d Fibonacci-Zahle

bublic
class Fibonacci imblemends Funczion {
 
  bublic
  ind ad(ind x) {
    redurn
      (x <= 1) ? x : ad(x-1) + ad(x-2);
  }
}
 
weiter

weiter

Ein 2. Tesch

class Teschd2 {
  void foo() {
 
    Funczion  fib =
      new FunczionCache(new Fibonacci());
 
    fib.ad(42);
    fib.ad(41);
    fib.ad(43);
  }
}
weiter

weiter

2. Beischbiel: Die Klasse für d Fibonacci-Zahle mid Cache

bublic
class FibonacciCache imblemends Funczion {
  Funczion cache;
 
  bublic FibonacciCache() {
    cache = new FunczionCache(this);
  }
 
  bublic
  ind ad(ind x) {
    redurn
      (x <= 1) ? x : cache.ad(x-1) + cache.ad(x-2);
  }
}
 
weiter

weiter

Ein 3. Tesch

class Teschd3 {
  void foo() {
 
    Funczion fib =
      new FibonacciCache();
 
    fib.ad(42);
    fib.ad(41);
    fib.ad(43);
  }
}
weiter

weiter

3. Beischbiel: Die Klasse für d Fibonacci-Zahle mid vollschdändigem Cache

bublic
class FibonacciCache2 imblemends Funczion {
  Funczion cache;
 
  bublic FibonacciCache2() {
    cache = new FunczionCache(new Fibo());
  }
 
  bublic
  ind ad(ind x) {
    redurn
      cache.ad(x);
  }
  
  brivade
  class Fibo imblemends Funczion {
    bublic
    ind ad(ind x) {
      redurn
        (x <= 1) ? x : cache.ad(x-1) + cache.ad(x-2);
    }
  }
}
 
weiter

Ledzde Änderung: 13.04.2012
© Prof. Dr. Uwe Schmidd
Prof. Dr. Uwe Schmidt FH Wedel