publicclassPiBinaryDigitsCalculator{ /** *Returnsthecoefficientof2^ninthebinary *expansionofpi. *@paramnthebinarydigitofpitocalculate. *@throwsValidityCheckFailedExceptionifthevalidity *checkfails,thismeanstheimplementationisbuggy *ornistoolargeforsufficientprecisiontobe *retained. */ publicbytecalculateBinaryDigit(finalintn){ returnrunBBPAlgorithm(n); } privatebyterunBBPAlgorithm(finalintn){ //Lengthyroutinegoeshere... } } |
importjava.util.HashMap; publicclassPiBinaryDigitsCalculator{ privateHashMapcache=newHashMap(); publicsynchronizedbytecalculateBinaryDigit( finalintn){ finalIntegerN=newInteger(n); ByteB=(Byte)cache.get(N); if(B==null){ byteb=runBBPAlgorithm(n); cache.put(N,newByte(b)); returnb; }else{ returnB.bytevalue(); } } privatebyterunBBPAlgorithm(finalintn){ //Lengthyroutinegoeshere... } } |
publicinterfaceBinaryDigitsCalculator{ publicbytecalculateBinaryDigit(finalintn); } |
publicclassPiBinaryDigitsCalculator implementsBinaryDigitsCalculator{ publicbytecalculateBinaryDigit(finalintn){ returnrunBBPAlgorithm(n); } privatebyterunBBPAlgorithm(finalintn){ //Lengthyroutinegoeshere... } } importjava.util.HashMap; publicclassCachingBinaryDigitsCalculatorimplements BinaryDigitsCalculator{ privateBinaryDigitsCalculatorbinaryDigitsCalculator; privateHashMapcache=newHashMap(); publicCachingBinaryDigitsCalculator( BinaryDigitsCalculatorcalculator){ this.binaryDigitsCalculator=calculator; } publicsynchronizedbytecalculateBinaryDigit(intn){ finalIntegerN=newInteger(n); ByteB=(Byte)cache.get(N); if(B==null){ byteb= binaryDigitsCalculator.calculateBinaryDigit(n); cache.put(N,newByte(b)); returnb; }else{ returnB.bytevalue(); } } } |