iterator = data.iterator();
36 |
37 | while (iterator.hasNext()) {
38 |
39 | SensorData sensorData = iterator.next();
40 |
41 | for(Observer observer:observers) {
42 | observer.update(sensorData);
43 | }
44 |
45 | iterator.remove();
46 | }
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/state/AdminGreetingState.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.state;
2 |
3 | import java.util.Date;
4 |
5 | public class AdminGreetingState implements GreetingState {
6 |
7 | private static final String FOOTER_MESSAGE = "";
8 |
9 | private final String username;
10 | private final Date lastLogin;
11 |
12 | public AdminGreetingState(final String username, Date lastLogin) {
13 | this.username = username;
14 | this.lastLogin = lastLogin;
15 | }
16 |
17 |
18 | @Override
19 | public String create() {
20 | return String.format(FOOTER_MESSAGE,username,lastLogin);
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/state/AnonymousGreetingState.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.state;
2 |
3 | public class AnonymousGreetingState implements GreetingState {
4 |
5 | private static final String FOOTER_MESSAGE = "";
6 |
7 | @Override
8 | public String create() {
9 | return FOOTER_MESSAGE;
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/state/GreetingState.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.state;
2 |
3 | public interface GreetingState {
4 |
5 | String create();
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/state/LoggedInGreetingState.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.state;
2 |
3 | public class LoggedInGreetingState implements GreetingState {
4 |
5 | private static final String FOOTER_MESSAGE = "";
6 |
7 | private final String username;
8 |
9 | public LoggedInGreetingState(final String username) {
10 | this.username = username;
11 | }
12 |
13 | @Override
14 | public String create() {
15 | return String.format(FOOTER_MESSAGE,username);
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/state/StateMain.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.state;
2 |
3 | import java.io.PrintWriter;
4 | import java.util.Date;
5 |
6 | public class StateMain {
7 |
8 | public static void main(String[] args) {
9 |
10 | StateUIContext stateUIContext = new StateUIContext();
11 |
12 | try(PrintWriter printWriter = new PrintWriter(System.out)) {
13 | stateUIContext.setGreetingState(new AnonymousGreetingState());
14 | stateUIContext.create(printWriter);
15 | printWriter.write("\n");
16 | stateUIContext.setGreetingState(new LoggedInGreetingState("someone"));
17 | stateUIContext.create(printWriter);
18 | printWriter.write("\n");
19 | stateUIContext.setGreetingState(new AdminGreetingState("admin",new Date()));
20 | stateUIContext.create(printWriter);
21 | printWriter.write("\n");
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/state/StateUIContext.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.state;
2 |
3 | import java.io.PrintWriter;
4 |
5 | public class StateUIContext {
6 |
7 | private GreetingState greetingState;
8 |
9 | public void setGreetingState(GreetingState greetingState) {
10 | this.greetingState = greetingState;
11 | }
12 |
13 | public void create(PrintWriter printWriter) {
14 | printWriter.write(greetingState.create());
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/strategy/FourLaneSpeeding.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.strategy;
2 |
3 | public class FourLaneSpeeding implements Speeding {
4 |
5 | private static final Double upperLimit = 50d;
6 |
7 | @Override
8 | public Double adjustSpeed(Double currentSpeed) {
9 | if(currentSpeed>upperLimit) {
10 | currentSpeed = upperLimit;
11 | }
12 |
13 | System.out.println("Speed adjusted at "+currentSpeed);
14 |
15 | return currentSpeed;
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/strategy/Speeding.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.strategy;
2 |
3 | public interface Speeding {
4 |
5 | Double adjustSpeed(Double currentSpeed);
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/strategy/Strategy.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.strategy;
2 |
3 | public class Strategy {
4 |
5 | public static void main(String[] args) {
6 | Vehicle vehicle = new Vehicle();
7 |
8 | vehicle.setCurrentSpeed(70d);
9 |
10 | vehicle.drive();
11 |
12 | /**
13 | * Changed route
14 | */
15 |
16 | vehicle.setSpeeding(new FourLaneSpeeding());
17 |
18 | vehicle.drive();
19 |
20 | /**
21 | * Changed route
22 | */
23 |
24 | vehicle.setSpeeding(new UrbanAreaSpeeding());
25 |
26 | vehicle.drive();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/strategy/UrbanAreaSpeeding.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.strategy;
2 |
3 | public class UrbanAreaSpeeding implements Speeding {
4 |
5 | private static final Double upperLimit = 30d;
6 |
7 | @Override
8 | public Double adjustSpeed(Double currentSpeed) {
9 | if(currentSpeed>upperLimit) {
10 | currentSpeed = upperLimit;
11 | }
12 |
13 | System.out.println("Speed adjusted at "+currentSpeed);
14 |
15 | return currentSpeed;
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/strategy/Vehicle.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.strategy;
2 |
3 | public class Vehicle {
4 |
5 | private Speeding speeding;
6 | private Double currentSpeed;
7 |
8 | public void drive() {
9 |
10 | speeding.adjustSpeed(currentSpeed);
11 |
12 | /**
13 | * Driving related actions.
14 | */
15 | }
16 |
17 | public void setSpeeding(Speeding speeding) {
18 | this.speeding = speeding;
19 | }
20 |
21 | public void setCurrentSpeed(Double currentSpeed) {
22 | this.currentSpeed = currentSpeed;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/template/CoffeeMachineTemplate.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.template;
2 |
3 | public abstract class CoffeeMachineTemplate {
4 |
5 | protected abstract void processBeans();
6 |
7 | protected abstract void processMilk();
8 |
9 | protected abstract void boil();
10 |
11 | public void pourToCup() {
12 | /**
13 | * pour to various cups based on the size
14 | */
15 | }
16 |
17 | public void serve() {
18 | processBeans();
19 | boil();
20 | processMilk();
21 | pourToCup();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/template/EspressoMachine.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.template;
2 |
3 | public class EspressoMachine extends CoffeeMachineTemplate {
4 |
5 | @Override
6 | protected void processBeans() {
7 | /**
8 | * Gring the beans
9 | */
10 | }
11 |
12 | @Override
13 | protected void processMilk() {
14 | /**
15 | * Use milk to create leaf art
16 | */
17 | }
18 |
19 | @Override
20 | protected void boil() {
21 | /**
22 | * Mix water and beans
23 | */
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/visitor/LocationRequestExecutor.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.visitor;
2 |
3 | public class LocationRequestExecutor implements Visitable {
4 |
5 | private int successfulRequests = 0;
6 | private double requestsPerMinute = 0.0;
7 |
8 | public void executeRequest() {
9 | /**
10 | * Execute the request and change the successfulRequests and requestsPerMinute value
11 | */
12 | }
13 |
14 | @Override
15 | public void accept(LocationVisitor visitor) {
16 | visitor.visit(this);
17 | }
18 |
19 | public int getSuccessfulRequests() {
20 | return successfulRequests;
21 | }
22 |
23 | public double getRequestsPerMinute() {
24 | return requestsPerMinute;
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/visitor/LocationVisitor.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.visitor;
2 |
3 | public interface LocationVisitor extends Visitor {
4 |
5 | void visit(LocationRequestExecutor locationRequestExecutor);
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/visitor/RequestVisitor.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.visitor;
2 |
3 | public class RequestVisitor implements LocationVisitor, RouteVisitor {
4 |
5 | @Override
6 | public void visit(LocationRequestExecutor locationRequestExecutor) {
7 |
8 | }
9 |
10 | @Override
11 | public void visit(RouteRequestExecutor routeRequestExecutor) {
12 |
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/visitor/RouteRequestExecutor.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.visitor;
2 |
3 | public class RouteRequestExecutor implements Visitable {
4 |
5 | private int successfulRequests = 0;
6 | private double requestsPerMinute = 0.0;
7 |
8 | public void executeRequest() {
9 | /**
10 | * Execute the request and change the successfulRequests and requestsPerMinute value
11 | */
12 | }
13 |
14 | @Override
15 | public void accept(RouteVisitor visitor) {
16 | visitor.visit(this);
17 | }
18 |
19 | public int getSuccessfulRequests() {
20 | return successfulRequests;
21 | }
22 |
23 | public double getRequestsPerMinute() {
24 | return requestsPerMinute;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/visitor/RouteVisitor.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.visitor;
2 |
3 | public interface RouteVisitor extends Visitor {
4 |
5 | void visit(RouteRequestExecutor routeRequestExecutor);
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/visitor/Visitable.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.visitor;
2 |
3 | public interface Visitable {
4 |
5 | void accept(T visitor);
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/visitor/Visitor.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.visitor;
2 |
3 | public interface Visitor {
4 | }
5 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/behavioural/visitor/VisitorMain.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.behavioural.visitor;
2 |
3 | public class VisitorMain {
4 |
5 | public static void main(String[] args) {
6 | final LocationRequestExecutor locationRequestExecutor = new LocationRequestExecutor();
7 | final RouteRequestExecutor routeRequestExecutor = new RouteRequestExecutor();
8 | final RequestVisitor requestVisitor = new RequestVisitor();
9 |
10 | locationRequestExecutor.accept(requestVisitor);
11 | routeRequestExecutor.accept(requestVisitor);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/CanBody.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory;
2 |
3 | public interface CanBody {
4 |
5 | void fill();
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/CanTop.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory;
2 |
3 | public interface CanTop {
4 |
5 | void open();
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/CanningFactory.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory;
2 |
3 | public abstract class CanningFactory {
4 |
5 | public abstract CanTop createTop();
6 |
7 | public abstract CanBody createBody();
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/MovieFactory.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory;
2 |
3 | public interface MovieFactory {
4 | }
5 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/beer/BeerCanBody.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory.beer;
2 |
3 | import com.gkatzioura.design.creational.abstractfactory.CanBody;
4 |
5 | public class BeerCanBody implements CanBody {
6 |
7 | public void fill() {
8 |
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/beer/BeerCanTop.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory.beer;
2 |
3 | import com.gkatzioura.design.creational.abstractfactory.CanTop;
4 |
5 | public class BeerCanTop implements CanTop {
6 |
7 | public void open() {
8 |
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/beer/BeerCanningFactory.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory.beer;
2 |
3 | import com.gkatzioura.design.creational.abstractfactory.CanBody;
4 | import com.gkatzioura.design.creational.abstractfactory.CanTop;
5 | import com.gkatzioura.design.creational.abstractfactory.CanningFactory;
6 |
7 | public class BeerCanningFactory extends CanningFactory {
8 |
9 | public CanTop createTop() {
10 | return new BeerCanTop();
11 | }
12 |
13 | public CanBody createBody() {
14 | return new BeerCanBody();
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/food/FoodCanBody.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory.food;
2 |
3 | import com.gkatzioura.design.creational.abstractfactory.CanBody;
4 |
5 | public class FoodCanBody implements CanBody {
6 |
7 | public void fill() {
8 |
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/food/FoodCanTop.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory.food;
2 |
3 | import com.gkatzioura.design.creational.abstractfactory.CanTop;
4 |
5 | public class FoodCanTop implements CanTop {
6 |
7 | public void open() {
8 |
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/abstractfactory/food/FoodCanningFactory.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.abstractfactory.food;
2 |
3 | import com.gkatzioura.design.creational.abstractfactory.CanBody;
4 | import com.gkatzioura.design.creational.abstractfactory.CanTop;
5 | import com.gkatzioura.design.creational.abstractfactory.CanningFactory;
6 |
7 | public class FoodCanningFactory extends CanningFactory {
8 |
9 | public CanTop createTop() {
10 | return new FoodCanTop();
11 | }
12 |
13 | public CanBody createBody() {
14 | return new FoodCanBody();
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/builder/Email.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.builder;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 |
6 | public class Email {
7 |
8 | private final String title;
9 | private final String recipients;
10 | private final String message;
11 |
12 | private Email(String title, String recipients, String message) {
13 | this.title = title;
14 | this.recipients = recipients;
15 | this.message = message;
16 | }
17 |
18 | public String getTitle() {
19 | return title;
20 | }
21 |
22 | public String getRecipients() {
23 | return recipients;
24 | }
25 |
26 | public String getMessage() {
27 | return message;
28 | }
29 |
30 | public void send() {
31 |
32 | }
33 |
34 | public static class EmailBuilder {
35 |
36 | private Set recipients = new HashSet();
37 | private String title;
38 | private String greeting;
39 | private String mainText;
40 | private String closing;
41 |
42 | public EmailBuilder addRecipient(String recipient) {
43 | this.recipients.add(recipient);
44 | return this;
45 | }
46 |
47 | public EmailBuilder removeRecipient(String recipient) {
48 | this.recipients.remove(recipient);
49 | return this;
50 | }
51 |
52 | public EmailBuilder setTitle(String title) {
53 | this.title = title;
54 | return this;
55 | }
56 |
57 | public EmailBuilder setGreeting(String greeting) {
58 | this.greeting = greeting;
59 | return this;
60 | }
61 |
62 | public EmailBuilder setMainText(String mainText) {
63 | this.mainText = mainText;
64 | return this;
65 | }
66 |
67 | public EmailBuilder setClosing(String closing) {
68 | this.closing = closing;
69 | return this;
70 | }
71 |
72 | public Email build() {
73 |
74 | String message = greeting+"\n"+mainText+"\n"+closing;
75 | String recipientSection = commaSeparatedRecipients();
76 |
77 | return new Email(title,recipientSection,message);
78 | }
79 |
80 | private String commaSeparatedRecipients() {
81 |
82 | StringBuilder sb = new StringBuilder();
83 | for(String recipient:recipients) {
84 | sb.append(",").append(recipient);
85 | }
86 |
87 | return sb.toString().replaceFirst(",","");
88 | }
89 |
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/factory/ClothesVoucher.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.factory;
2 |
3 | import java.util.UUID;
4 |
5 | public class ClothesVoucher implements Voucher {
6 |
7 | private UUID code;
8 | private static final String htmlMessage = "Clothes Voucher
";
9 |
10 | public ClothesVoucher() {
11 | code = UUID.randomUUID();
12 | }
13 |
14 | public String code() {
15 | return code.toString();
16 | }
17 |
18 | public String htmlMessage() {
19 | return htmlMessage;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/factory/FoodVoucher.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.factory;
2 |
3 | import java.util.UUID;
4 |
5 | public class FoodVoucher implements Voucher {
6 |
7 | private UUID code;
8 | private static final String htmlMessage= "Food Voucher
";
9 |
10 | public FoodVoucher() {
11 | code = UUID.randomUUID();
12 | }
13 |
14 | public String code() {
15 | return code.toString();
16 | }
17 |
18 | public String htmlMessage() {
19 | return htmlMessage;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/factory/Voucher.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.factory;
2 |
3 | public interface Voucher {
4 |
5 | public String code();
6 |
7 | public String htmlMessage();
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/factory/VoucherFactory.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.factory;
2 |
3 | public class VoucherFactory {
4 |
5 | public Voucher create(Integer discountPoints) {
6 |
7 | if(discountPoints<=0) {
8 | throw new IllegalArgumentException("Invalid number of discount points!");
9 | }
10 |
11 | if(discountPoints<30) {
12 | return new FoodVoucher();
13 | } else {
14 | return new ClothesVoucher();
15 | }
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/prototype/FederatedSearchResult.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.prototype;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class FederatedSearchResult implements SearchResult {
7 |
8 | private List pages = new ArrayList();
9 |
10 | public FederatedSearchResult(List pages) {
11 | this.pages = pages;
12 | }
13 |
14 | @Override
15 | public SearchResult clone() {
16 |
17 | final List resultCopies = new ArrayList();
18 | pages.forEach(p->resultCopies.add(p));
19 | FederatedSearchResult federatedSearchResult = new FederatedSearchResult(resultCopies);
20 | return federatedSearchResult;
21 | }
22 |
23 | @Override
24 | public int totalEntries() {
25 | return pages.size();
26 | }
27 |
28 | @Override
29 | public String getPage(int page) {
30 | return pages.get(page);
31 | }
32 |
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/prototype/SearchResult.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.prototype;
2 |
3 | public interface SearchResult {
4 |
5 | SearchResult clone();
6 |
7 | int totalEntries();
8 |
9 | String getPage(int page);
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/singleton/cli/Messenger.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.singleton.cli;
2 |
3 | public class Messenger {
4 |
5 | private static Messenger messenger = new Messenger();
6 |
7 | private Messenger() {}
8 |
9 | public static Messenger getInstance() {
10 | return messenger;
11 | }
12 |
13 | public void send(String message) {
14 |
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/singleton/dcl/Messenger.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.singleton.dcl;
2 |
3 | public class Messenger {
4 |
5 | private static final Object lock = new Object();
6 | private static volatile Messenger messenger;
7 |
8 | private Messenger() {}
9 |
10 | public static Messenger getInstance() {
11 |
12 | if(messenger==null) {
13 | synchronized (lock) {
14 | if(messenger==null) {
15 | messenger = new Messenger();
16 | }
17 | }
18 | }
19 |
20 | return messenger;
21 | }
22 |
23 | public void send(String message) {
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/creational/singleton/lait/Messenger.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.creational.singleton.lait;
2 |
3 | public class Messenger {
4 |
5 | private static Messenger messenger;
6 |
7 | private Messenger() {}
8 |
9 | public synchronized static Messenger getInstance() {
10 |
11 | if(messenger==null) {
12 | messenger = new Messenger();
13 | }
14 |
15 | return messenger;
16 | }
17 |
18 | public void send(String message) {
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/adapter/MailNotifierAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.adapter;
2 |
3 | import java.util.List;
4 |
5 | public class MailNotifierAdapter implements Notifier {
6 |
7 | private static final String NOTIFICATION_TITLE = "System notification";
8 |
9 | public void notify(List recipients, String message) {
10 |
11 | MailSender mailSender = new MailSender(NOTIFICATION_TITLE,message,recipients);
12 | mailSender.sendMessage();
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/adapter/MailSender.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.adapter;
2 |
3 | import java.util.List;
4 |
5 | public class MailSender {
6 |
7 | private String title;
8 | private String message;
9 | private List recipients;
10 |
11 | public MailSender(String title, String message, List recipients) {
12 | this.title = title;
13 | this.message = message;
14 | this.recipients = recipients;
15 | }
16 |
17 | public void sendMessage() {
18 |
19 | }
20 |
21 | public void sendHtmlMessage(String htmlTemplate) {
22 |
23 | }
24 |
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/adapter/Notifier.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.adapter;
2 |
3 | import java.util.List;
4 |
5 | public interface Notifier {
6 |
7 | void notify(List recipients, String message);
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/bridge/AirToAirMissile.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.bridge;
2 |
3 | public class AirToAirMissile implements Missile {
4 |
5 | private Igniter igniter;
6 |
7 | public AirToAirMissile(Igniter igniter) {
8 | this.igniter = igniter;
9 | }
10 |
11 | public void explode() {
12 |
13 | //Actions relation to explosion
14 |
15 | igniter.ignite();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/bridge/Igniter.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.bridge;
2 |
3 | public interface Igniter {
4 |
5 | void ignite();
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/bridge/Missile.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.bridge;
2 |
3 | public interface Missile {
4 |
5 | void explode();
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/bridge/PyrogenIgniter.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.bridge;
2 |
3 | public class PyrogenIgniter implements Igniter {
4 |
5 | public void ignite() {
6 |
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/composite/CompositeScenario.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.composite;
2 |
3 | import java.util.Collections;
4 |
5 | public class CompositeScenario {
6 |
7 | public static void main(String[] args) {
8 |
9 | Private ryan = new Private();
10 | Lieutenant lieutenant = new Lieutenant(Collections.singletonList(ryan));
11 | Major major = new Major(Collections.singletonList(lieutenant));
12 | General general = new General();
13 | general.assignOrder(major);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/composite/General.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.composite;
2 |
3 | public class General implements Officer {
4 |
5 | @Override
6 | public void assignOrder(MilitaryPersonnel militaryPersonnel) {
7 | militaryPersonnel.executeOrder();
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/composite/Lieutenant.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.composite;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class Lieutenant implements MilitaryPersonnel, Officer {
7 |
8 | private List lowerRankersonel = new ArrayList<>();
9 |
10 | public Lieutenant(List lowerRankersonel) {
11 | this.lowerRankersonel = lowerRankersonel;
12 | }
13 |
14 | public void addPrivateUnderCommand(Private soldier) {
15 | lowerRankersonel.add(soldier);
16 | }
17 |
18 | @Override
19 | public void executeOrder() {
20 | System.out.println("Lieutenant executing order assigned");
21 |
22 | //other actions
23 | this.lowerRankersonel.forEach(lw->assignOrder(lw));
24 | //other actions
25 | }
26 |
27 | @Override
28 | public void assignOrder(MilitaryPersonnel militaryPersonnel) {
29 | militaryPersonnel.executeOrder();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/composite/Major.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.composite;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class Major implements MilitaryPersonnel, Officer {
7 |
8 | private List lowerRankersonel = new ArrayList<>();
9 |
10 | public Major(List lowerRankersonel) {
11 | this.lowerRankersonel = lowerRankersonel;
12 | }
13 |
14 | public void addPrivateUnderCommand(Private soldier) {
15 | lowerRankersonel.add(soldier);
16 | }
17 |
18 | public void addLieutenantUnderCommand(Lieutenant lieutenant) {
19 | lowerRankersonel.add(lieutenant);
20 | }
21 |
22 | @Override
23 | public void executeOrder() {
24 | System.out.println("Major executing order assigned");
25 | //other actions
26 | this.lowerRankersonel.forEach(lw->assignOrder(lw));
27 | //other actions
28 | }
29 |
30 | @Override
31 | public void assignOrder(MilitaryPersonnel militaryPersonnel) {
32 | militaryPersonnel.executeOrder();
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/composite/MilitaryPersonnel.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.composite;
2 |
3 | public interface MilitaryPersonnel {
4 |
5 | void executeOrder();
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/composite/Officer.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.composite;
2 |
3 | public interface Officer {
4 |
5 | void assignOrder(MilitaryPersonnel militaryPersonnel);
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/composite/Private.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.composite;
2 |
3 | public class Private implements MilitaryPersonnel {
4 |
5 | @Override
6 | public void executeOrder() {
7 | System.out.println("Private executing order assigned");
8 | }
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/decorator/DecoratorScenario.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.decorator;
2 |
3 | import java.math.BigDecimal;
4 |
5 | public class DecoratorScenario {
6 |
7 | public static void main(String args[]) {
8 |
9 | NewlyRegisteredDiscount newlyRegisteredDiscount = new NewlyRegisteredDiscount();
10 | ReferencedUserDiscount referencedUserDiscount = new ReferencedUserDiscount(newlyRegisteredDiscount);
11 | TwoYearPlanDiscount twoYearPlanDiscount = new TwoYearPlanDiscount(referencedUserDiscount);
12 | BigDecimal discountPrice = twoYearPlanDiscount.apply(new BigDecimal(100));
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/decorator/Discount.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.decorator;
2 |
3 | import java.math.BigDecimal;
4 |
5 | public interface Discount {
6 |
7 | BigDecimal apply(BigDecimal originalPrice);
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/decorator/DiscountDecorator.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.decorator;
2 |
3 | import java.math.BigDecimal;
4 |
5 | public class DiscountDecorator implements Discount {
6 |
7 | protected Discount discount;
8 |
9 | public DiscountDecorator(Discount discount) {
10 | this.discount = discount;
11 | }
12 |
13 | @Override
14 | public BigDecimal apply(BigDecimal originalPrice) {
15 | return discount.apply(originalPrice);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/decorator/NewlyRegisteredDiscount.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.decorator;
2 |
3 | import java.math.BigDecimal;
4 |
5 | public class NewlyRegisteredDiscount implements Discount {
6 |
7 | public static final BigDecimal SEVENTY_FIVE = new BigDecimal(75);
8 | public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);
9 |
10 | @Override
11 | public BigDecimal apply(BigDecimal originalPrice) {
12 |
13 | return originalPrice.multiply(SEVENTY_FIVE).divide(ONE_HUNDRED);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/decorator/ReferencedUserDiscount.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.decorator;
2 |
3 | import java.math.BigDecimal;
4 |
5 | public class ReferencedUserDiscount extends DiscountDecorator {
6 |
7 | public static final BigDecimal FIVE = new BigDecimal(5);
8 |
9 | public ReferencedUserDiscount(Discount discount) {
10 | super(discount);
11 | }
12 |
13 | @Override
14 | public BigDecimal apply(BigDecimal originalPrice) {
15 |
16 | BigDecimal discountedPrice = super.apply(originalPrice);
17 |
18 | if(discountedPrice.compareTo(FIVE)<=0) {
19 | return discountedPrice;
20 | }
21 |
22 | return discountedPrice.subtract(FIVE);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/decorator/TwoYearPlanDiscount.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.decorator;
2 |
3 | import java.math.BigDecimal;
4 |
5 | public class TwoYearPlanDiscount extends DiscountDecorator {
6 |
7 | public static final BigDecimal NINETY_NINE = new BigDecimal(95);
8 | public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);
9 |
10 | public TwoYearPlanDiscount(Discount discount) {
11 | super(discount);
12 | }
13 |
14 | @Override
15 | public BigDecimal apply(BigDecimal originalPrice) {
16 | return super.apply(originalPrice).multiply(NINETY_NINE).divide(ONE_HUNDRED);
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/CSVTimeSeriesReport.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Date;
5 | import java.util.List;
6 |
7 | public class CSVTimeSeriesReport implements TimeSeriesReport {
8 |
9 | @Override
10 | public List generate(Date start, Date end) {
11 |
12 | /**
13 | * retrieve the csv and iterate line by line with the time limits
14 | */
15 |
16 | return new ArrayList<>();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/GeolocationReport.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | import java.util.List;
4 |
5 | public interface GeolocationReport {
6 |
7 | List generate(Double lat, Double lng, Double distance);
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/LocationInformation.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | public class LocationInformation {
4 | }
5 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/SQLUsageReport.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.UUID;
6 |
7 | public class SQLUsageReport implements UsageReport {
8 |
9 | @Override
10 | public List report(UUID uuid) {
11 |
12 | return new ArrayList<>();
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/TimeInformation.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | public class TimeInformation {
4 | }
5 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/TimeSeriesReport.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | import java.util.Date;
4 | import java.util.List;
5 |
6 | public interface TimeSeriesReport {
7 |
8 | List generate(Date start, Date end);
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/Usage.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | public class Usage {
4 | }
5 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/UsageReport.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | import java.util.List;
4 | import java.util.UUID;
5 |
6 | public interface UsageReport {
7 |
8 | List report(UUID uuid);
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/UserUsage.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | public class UserUsage {
4 | }
5 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/UserUsageFacade.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | import java.util.Date;
4 | import java.util.List;
5 | import java.util.UUID;
6 |
7 | public interface UserUsageFacade {
8 |
9 | List usageOn(UUID user, Date from, Double lat, Double lng);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/UserUsageFacadeImpl.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Date;
5 | import java.util.List;
6 | import java.util.UUID;
7 |
8 | public class UserUsageFacadeImpl implements UserUsageFacade {
9 |
10 | private final GeolocationReport geolocationReport;
11 | private final TimeSeriesReport timeSeriesReport;
12 | private final UsageReport usageReport;
13 |
14 | private final double DEFAULT_DISTANCE = 20d;
15 | private final int DEFAULT_TIME_RANGE = 20;
16 |
17 | public UserUsageFacadeImpl(GeolocationReport geolocationReport, TimeSeriesReport timeSeriesReport, UsageReport usageReport) {
18 | this.geolocationReport = geolocationReport;
19 | this.timeSeriesReport = timeSeriesReport;
20 | this.usageReport = usageReport;
21 | }
22 |
23 | @Override
24 | public List usageOn(UUID user, Date from, Double lat, Double lng) {
25 |
26 | List locationInformationData = geolocationReport.generate(lat, lng, DEFAULT_DISTANCE);
27 | Date to = Date.from(from.toInstant().plusSeconds(DEFAULT_TIME_RANGE));
28 | List timeSetiesData = timeSeriesReport.generate(from, to);
29 | List usageData = usageReport.report(user);
30 |
31 | /**
32 | * Generate the report based on the data retrieved
33 | */
34 |
35 | return new ArrayList<>();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/facade/XMLGeolocationReport.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.facade;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class XMLGeolocationReport implements GeolocationReport {
7 |
8 | @Override
9 | public List generate(Double lat, Double lng, Double distance) {
10 |
11 | /**
12 | * http requests to retrieve the xml
13 | * iterate the xml using stax
14 | */
15 |
16 | return new ArrayList<>();
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/proxy/DataUploadService.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.proxy;
2 |
3 | public interface DataUploadService {
4 |
5 | void upload(String payload);
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/proxy/HttpDataUploadImpl.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.proxy;
2 |
3 | public class HttpDataUploadImpl implements DataUploadService {
4 |
5 | @Override
6 | public void upload(String payload) {
7 |
8 | }
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/proxy/HttpDataUploadProxy.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.proxy;
2 |
3 | import java.nio.charset.Charset;
4 | import java.time.Duration;
5 | import java.time.Instant;
6 |
7 | public class HttpDataUploadProxy implements DataUploadService {
8 |
9 | private final HttpDataUploadImpl httpDataUpload;
10 |
11 | public HttpDataUploadProxy(HttpDataUploadImpl httpDataUpload) {
12 | this.httpDataUpload = httpDataUpload;
13 | }
14 |
15 | @Override
16 | public void upload(String payload) {
17 |
18 | Instant start = Instant.now();
19 |
20 | httpDataUpload.upload(payload);
21 |
22 | Duration duration = Duration.between(start,Instant.now());
23 | int byteSize = payload.getBytes(Charset.defaultCharset()).length;
24 |
25 | /**
26 | * Log properly to splunk/cloudwatch etc
27 | */
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/gkatzioura/design/structural/proxy/HttpDataUploadQuotaProxy.java:
--------------------------------------------------------------------------------
1 | package com.gkatzioura.design.structural.proxy;
2 |
3 | public class HttpDataUploadQuotaProxy implements DataUploadService {
4 |
5 | private final HttpDataUploadImpl httpDataUpload;
6 |
7 | public HttpDataUploadQuotaProxy(HttpDataUploadImpl httpDataUpload) {
8 | this.httpDataUpload = httpDataUpload;
9 | }
10 |
11 | @Override
12 | public void upload(String payload) {
13 |
14 | if(quotaExceeded()) {
15 | throw new IllegalStateException("Quota exceeded cannot upload payload");
16 | }
17 |
18 | httpDataUpload.upload(payload);
19 | }
20 |
21 | private boolean quotaExceeded() {
22 |
23 | /**
24 | * Code that should check whether we exceeded our quota or not
25 | */
26 |
27 | return false;
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------