public int calculateTax(Person person, Year year)
{
return (int) person.getIncome(year) * 0.12
}
public int calculateTax(Person person, Year year)
{
if(person.isQualifiedForZeroTax()) {
return 0
}
if(person.getIncome(year) > TaxThreshold.Second) {
return TaxThreshold.Second * 0.12
+ (person.getIncome(year) - TaxThreshold.Second) * 0.32
}
return (int) person.getIncome(year) * 0.12
}
public int calculateTax(Person person, Year year)
{
int inFirstThreshold = TaxThreshold.First
int taxedIncome = person.getIncome(year) - TaxThreshold.First
int inSecondThreshold = person.isQualifiedForZeroTax() ? 0
: (taxedIncome > TaxThreshold.Second ? TaxThreshold.Second
: taxedIncome)
int overSecondThreshold = person.getIncome(year) - TaxThreshold.Second
int inThirdThreshold = overSecondThreshold > 0 ? overSecondThreshold : 0
return (int) (inFirstThreshold * 0
+ inSecondThreshold * 0.12
+ inThirdThreshold * 0.32)
}
class UserRegistrar
{
public UserRegistrar(Hasher hasher, EventDispatcher dispatcher, CacheManager cache)
public User register(String email, String password) throws UserAlreadyExistsException
{
User user = new User()
user.setName(email)
user.setPassword(hasher.argon(password))
user.createOrFail()
user.notify(new WelcomeMessage(), Channel.Email)
dispatcher.send(new UserRegistered())
cache.refresh(Cache.UserStatistics | Cache.RegisterStatistics)
}
}User ma metodę createOrFail(); czy na tym etapie musimy wiedzieć co tam tak naprawdę się dzieje? (nie, nie musimy!)AdministratorRegistrar poprzez dziedziczenie i ekstrakcję części głównej metody do metod chronionychregister()HttpRequest, ponieważ zepsuje to elastyczność takiego serwisu.
class DatabaseUsersRepository implements UsersRepositoryContract
{
public Collection<User> get()
{
return (new UserQuery()).limit(50).get().mapInto(User.class)
}
}
class CacheUsersRepository implements UsersRepositoryContract
{
public CacheUserRepository(Client redis)
public Collection<User> get()
{
return this.redis.getLatest(50).hydrate(User.class)
}
}
class DummyUsersRepository implements UsersRepositoryContract
{
public Collection<User> get()
{
// return new Collection(File.getJson("./users.json")).mapInto(User.class)
return new Collection(for n in 50 => new User(...getRandomUserData()))
}
}UsersRepositoryContract.
DatabaseUsersRepository zostanie użyty na środowisku deweloperskim, CacheUsersRepository na produkcyjnym, a DummyUsersRepository - podczsa testów.
NewsRepository z wieloma metodami takimi jak getLatest() czy getById(). A może lepiej przekazać mnóstwo parametrów do jednej metody, np. get(limit: 50, sort: publishedAt)? A może zrobić osobne repozytoria LatestNewsRepository i NewsRepository?
final class CommonDate
{
public static String date(Datetime datetime)
{
return datetime.locale("pl").timezone("UTC+1").format("Y-m-d")
}
public static String datetime(Datetime datetime)
{
return datetime.locale("pl").timezone("UTC+1").format("Y-m-d H:i:s")
}
}
enum ShippingStatus
{
case Draft
case Pending
case InTransit
case OutForDelivery
case Delivered
case Failed
}order.shipping.status = ShippingStatus.InTransit order.shipping.save()
class UserRegistrar
{
public User register(String email, String password) throws UserAlreadyExistsException
{
User user = new User()
user.setName(email)
user.setPassword((new Hasher(rounds: 12)).argon(password))
user.createOrFail()
user.notify(new WelcomeMessage(), Channel.Email)
(new EventDispatcher()).send(new UserRegistered())
CacheManager cache = new CacheManager(config("cache.connection_string"))
cache.refresh(Cache.UserStatistics | Cache.RegisterStatistics)
}
}
class UserRegistrar
{
public UserRegistrar(Hasher hasher, EventDispatcher dispatcher, CacheManager cache)
public User register(String email, String password) throws UserAlreadyExistsException
{
User user = new User()
user.setName(email)
user.setPassword(hasher.argon(password))
user.createOrFail()
user.notify(new WelcomeMessage(), Channel.Email)
dispatcher.send(new UserRegistered())
cache.refresh(Cache.UserStatistics | Cache.RegisterStatistics)
}
}# deklaracja routingu POST /register
router.post("/register", RegisterController.class, "get")new RegisterController()! Jedyne, co robimy, to deklaracja, że taki adres będzie kierował pod taką metodę takiego kontrolera.
class UserRegistrar
{
public UserRegistrar(Hasher hasher, EventDispatcher dispatcher, CacheManager cache)
public User register(String email, String password) throws UserAlreadyExistsException
{
User user = new User()
user.setName(email)
user.setPassword(hasher.argon(password))
user.createOrFail()
user.notify(new WelcomeMessage(), Channel.Email)
dispatcher.send(new UserRegistered())
cache.refresh(Cache.UserStatistics | Cache.RegisterStatistics)
}
}
(new UserQuery()).truncate()
UserRegistrar registrar = new UserRegistrar(
new Hasher(rounds: 1),
new FakeEventDispatcher(),
new FakeCacheManager(),
)
registrar.register("test@example.com", "password")
assert.count((new UserQuery()).get(), 1)