From bd5cc6ff7a57dfea36586ab0a8a2987ce3a1ba90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Dyda?= Date: Thu, 27 Oct 2022 22:20:55 +0200 Subject: [PATCH] Strategy design pattern. --- .../demo/logic/ApartmentHousingStrategy.java | 20 +++++++++++++++++++ .../edu/demo/logic/HouseHousingStrategy.java | 11 ++++++++++ .../amu/edu/demo/logic/HousingStrategy.java | 15 ++++++++++++++ .../logic/WeWillStealYourFlatProduct.java | 20 +++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 demo/02/src/main/java/pl/amu/edu/demo/logic/ApartmentHousingStrategy.java create mode 100644 demo/02/src/main/java/pl/amu/edu/demo/logic/HouseHousingStrategy.java create mode 100644 demo/02/src/main/java/pl/amu/edu/demo/logic/HousingStrategy.java create mode 100644 demo/02/src/main/java/pl/amu/edu/demo/logic/WeWillStealYourFlatProduct.java diff --git a/demo/02/src/main/java/pl/amu/edu/demo/logic/ApartmentHousingStrategy.java b/demo/02/src/main/java/pl/amu/edu/demo/logic/ApartmentHousingStrategy.java new file mode 100644 index 0000000..d1f7422 --- /dev/null +++ b/demo/02/src/main/java/pl/amu/edu/demo/logic/ApartmentHousingStrategy.java @@ -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; + } +} diff --git a/demo/02/src/main/java/pl/amu/edu/demo/logic/HouseHousingStrategy.java b/demo/02/src/main/java/pl/amu/edu/demo/logic/HouseHousingStrategy.java new file mode 100644 index 0000000..9ccced4 --- /dev/null +++ b/demo/02/src/main/java/pl/amu/edu/demo/logic/HouseHousingStrategy.java @@ -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; + } +} diff --git a/demo/02/src/main/java/pl/amu/edu/demo/logic/HousingStrategy.java b/demo/02/src/main/java/pl/amu/edu/demo/logic/HousingStrategy.java new file mode 100644 index 0000000..c81c20d --- /dev/null +++ b/demo/02/src/main/java/pl/amu/edu/demo/logic/HousingStrategy.java @@ -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(); + } + +} diff --git a/demo/02/src/main/java/pl/amu/edu/demo/logic/WeWillStealYourFlatProduct.java b/demo/02/src/main/java/pl/amu/edu/demo/logic/WeWillStealYourFlatProduct.java new file mode 100644 index 0000000..c52ecf8 --- /dev/null +++ b/demo/02/src/main/java/pl/amu/edu/demo/logic/WeWillStealYourFlatProduct.java @@ -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); + } + +}