4520 shaares
1 résultat
taggé
example
Remarque :
- Le concept de cache ici pose problème puisque des méthodes qui transforment (à base de verbes) retournent en réalité quelque chose.
- Les méthodes avec des boolean ont des verbes, c'est une exception à la Yegorification du code.
// Usage :
object Main {
fun regularUseCase() {
// Given
val christmas = LocalDate(2019, 12, 25)
val calendar:HolidaysCalendar = HolidaysCalendarForYear(Year(2019))
// When
val daysOff:DateSet = calendar.daysOff()
// Then
println(daysOffs.contains(christmas)) // print true
}
fun cachedUseCase() {
// Given
val christmas = LocalDate(2019, 12, 25)
val cache:Cache = HolidaysCalendarCache()
// When
val daysOff2:HolidaysCalendar = cache.value(Year(2019))
val daysOff3:HolidaysCalendar = cache.value(Year(2019))
// Then
println(daysOff2.contains(christmas)) // print true
println(daysOff2 === daysOff3) // print true (same instance)
}
@JvmStatic
fun main() {
val main = Main()
main.regularUseCase()
main.cachedUseCase()
}
}
// Interfaces
interface DateSet {
/**
* Determine whether or not the specified date is in this set.
*
* @param date
* The date to research.
*
* @return true if the date exists in this set, false otherwise.
*/
fun contains(date:LocalDate):Boolean
/**
* Determine whether or not the specified date is in this set.
*
* @param date
* The date to research.
*
* @return true if the date exists in this set, false otherwise.
*/
fun contains(date:Calendar):Boolean
/**
* Return the current set of date as an iterable collection.
*
* @return A collection having all the date stored in this set.
*/
fun asCollection():Collection<LocalDate>
}
interface HolidaysCalendar {
/**
* Return a set of holiday dates.
*
* @return All holidays for a period (see implementation for more detail).
*/
fun daysOff():DateSet
/**
* The period covered by this calendar.
*
* @return Something in the CalenadrPeriod enumeration.
*/
fun range():CalenadrPeriod
}
class Cache<K, V> {
/**
* Determine whether or not the specified value exists in this cache.
*
* @param value
* The value to search.
*
* @return true if the value has been found, false otherwise.
*/
fun contains(value:V):Boolean
/**
* Determine whether or not a key exists in this cache.
*
* @param key
* The key to search.
*
* @return true if the key exists, false otherwise (reminder: a key cannot exists if linked to nothing).
*/
fun containsKey(key:K):Boolean
/**
* Retrieve the value related to the specified key.
*
* @param key
* The key related to the researched value.
*
* @return The value related to the given key.
*
* @throw UnexistingEntryException
* When the subsystem cached by this object is not able to restitute a value using the specified key.
*/
fun value(key:K):V
/**
* The list of keys used by this cache.
*
* @return The list of keys used by this cache.
*/
fun keys():List<K>
/**
* Remove the specified key in order to force an update.
*
* @param key
* The key of the cache entry to remove.
*/
fun remove(key:K)
/**
* Clear all entry is the current cache.
*/
fun reset()
}
// Implementation of HolidaysCalendar
class HolidaysCalendarForYear(private val year:Year):HolidaysCalendar
class HolidaysCalendarForMonth(private val year:Year, private val month:Month):HolidaysCalendar
// Implementation of Cache
class HolidaysCalendarCache:Cache<Year, HolidaysCalendar>