Strategy design pattern.

This commit is contained in:
Paweł Dyda 2022-10-27 22:20:55 +02:00
parent 5332703017
commit bd5cc6ff7a
4 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package pl.amu.edu.demo.logic;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import pl.amu.edu.demo.data.Housing;
import pl.amu.edu.demo.data.WeWillStealYourFlatCheckResult;
import pl.amu.edu.demo.providers.AverageQuoteProvider;
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class ApartmentHousingStrategy implements HousingStrategy {
AverageQuoteProvider provider;
@Override
public WeWillStealYourFlatCheckResult checkEligibility(Housing housing) {
return WeWillStealYourFlatCheckResult.ELIGIBLE;
}
}

View File

@ -0,0 +1,11 @@
package pl.amu.edu.demo.logic;
import pl.amu.edu.demo.data.Housing;
import pl.amu.edu.demo.data.WeWillStealYourFlatCheckResult;
public class HouseHousingStrategy implements HousingStrategy{
@Override
public WeWillStealYourFlatCheckResult checkEligibility(Housing housing) {
return WeWillStealYourFlatCheckResult.NOT_ELIGIBLE;
}
}

View File

@ -0,0 +1,15 @@
package pl.amu.edu.demo.logic;
import pl.amu.edu.demo.data.Housing;
import pl.amu.edu.demo.data.WeWillStealYourFlatCheckResult;
import pl.amu.edu.demo.providers.AverageQuoteProvider;
public interface HousingStrategy {
WeWillStealYourFlatCheckResult checkEligibility(Housing housing);
static HousingStrategy getInstance(Housing housing, AverageQuoteProvider provider) {
return housing.isApartment ? new ApartmentHousingStrategy(provider) : new HouseHousingStrategy();
}
}

View File

@ -0,0 +1,20 @@
package pl.amu.edu.demo.logic;
import pl.amu.edu.demo.data.Person;
import pl.amu.edu.demo.data.WeWillStealYourFlatCheckResult;
import pl.amu.edu.demo.providers.AverageQuoteProvider;
public class WeWillStealYourFlatProduct {
private final AverageQuoteProvider provider;
public WeWillStealYourFlatProduct(AverageQuoteProvider provider) {
this.provider = provider;
}
public WeWillStealYourFlatCheckResult check(Person person) {
var strategy = HousingStrategy.getInstance(person.housing, provider);
return strategy.checkEligibility(person.housing);
}
}