mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-23 20:26:50 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -28,23 +28,23 @@ import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* The Collecting Parameter Design Pattern aims to return a result that is the collaborative result of several
|
||||
* methods. This design pattern uses a 'collecting parameter' that is passed to several functions, accumulating results
|
||||
* as it travels from method-to-method. This is different to the Composed Method design pattern, where a single
|
||||
* collection is modified via several methods.
|
||||
* The Collecting Parameter Design Pattern aims to return a result that is the collaborative result
|
||||
* of several methods. This design pattern uses a 'collecting parameter' that is passed to several
|
||||
* functions, accumulating results as it travels from method-to-method. This is different to the
|
||||
* Composed Method design pattern, where a single collection is modified via several methods.
|
||||
*
|
||||
* <p>This example is inspired by Kent Beck's example in his book, 'Smalltalk Best Practice Patterns'. The context for this
|
||||
* situation is that there is a single printer queue {@link PrinterQueue} that holds numerous print jobs
|
||||
* {@link PrinterItem} that must be distributed to various print centers.
|
||||
* Each print center has its own requirements and printing limitations. In this example, the following requirements are:
|
||||
* If an A4 document is coloured, it must also be single-sided. All other non-coloured A4 documents are accepted.
|
||||
* All A3 documents must be non-coloured and single sided. All A2 documents must be a single page, single sided, and
|
||||
* <p>This example is inspired by Kent Beck's example in his book, 'Smalltalk Best Practice
|
||||
* Patterns'. The context for this situation is that there is a single printer queue {@link
|
||||
* PrinterQueue} that holds numerous print jobs {@link PrinterItem} that must be distributed to
|
||||
* various print centers. Each print center has its own requirements and printing limitations. In
|
||||
* this example, the following requirements are: If an A4 document is coloured, it must also be
|
||||
* single-sided. All other non-coloured A4 documents are accepted. All A3 documents must be
|
||||
* non-coloured and single sided. All A2 documents must be a single page, single sided, and
|
||||
* non-coloured.
|
||||
*
|
||||
* <p>A collecting parameter (the result variable) is used to filter the global printer queue so that it meets the
|
||||
* requirements for this centre,
|
||||
**/
|
||||
|
||||
* <p>A collecting parameter (the result variable) is used to filter the global printer queue so
|
||||
* that it meets the requirements for this centre,
|
||||
*/
|
||||
public class App {
|
||||
static PrinterQueue printerQueue = PrinterQueue.getInstance();
|
||||
|
||||
@@ -75,16 +75,16 @@ public class App {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds A4 document jobs to the collecting parameter according to some policy that can be whatever the client
|
||||
* (the print center) wants.
|
||||
* Adds A4 document jobs to the collecting parameter according to some policy that can be whatever
|
||||
* the client (the print center) wants.
|
||||
*
|
||||
* @param printerItemsCollection the collecting parameter
|
||||
*/
|
||||
public static void addValidA4Papers(Queue<PrinterItem> printerItemsCollection) {
|
||||
/*
|
||||
Iterate through the printer queue, and add A4 papers according to the correct policy to the collecting parameter,
|
||||
which is 'printerItemsCollection' in this case.
|
||||
*/
|
||||
Iterate through the printer queue, and add A4 papers according to the correct policy to the collecting parameter,
|
||||
which is 'printerItemsCollection' in this case.
|
||||
*/
|
||||
for (PrinterItem nextItem : printerQueue.getPrinterQueue()) {
|
||||
if (nextItem.paperSize.equals(PaperSizes.A4)) {
|
||||
var isColouredAndSingleSided = nextItem.isColour && !nextItem.isDoubleSided;
|
||||
@@ -96,9 +96,9 @@ public class App {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds A3 document jobs to the collecting parameter according to some policy that can be whatever the client
|
||||
* (the print center) wants. The code is similar to the 'addA4Papers' method. The code can be changed to accommodate
|
||||
* the wants of the client.
|
||||
* Adds A3 document jobs to the collecting parameter according to some policy that can be whatever
|
||||
* the client (the print center) wants. The code is similar to the 'addA4Papers' method. The code
|
||||
* can be changed to accommodate the wants of the client.
|
||||
*
|
||||
* @param printerItemsCollection the collecting parameter
|
||||
*/
|
||||
@@ -106,7 +106,8 @@ public class App {
|
||||
for (PrinterItem nextItem : printerQueue.getPrinterQueue()) {
|
||||
if (nextItem.paperSize.equals(PaperSizes.A3)) {
|
||||
|
||||
// Encoding the policy into a Boolean: the A3 paper cannot be coloured and double-sided at the same time
|
||||
// Encoding the policy into a Boolean: the A3 paper cannot be coloured and double-sided at
|
||||
// the same time
|
||||
var isNotColouredAndSingleSided = !nextItem.isColour && !nextItem.isDoubleSided;
|
||||
if (isNotColouredAndSingleSided) {
|
||||
printerItemsCollection.add(nextItem);
|
||||
@@ -116,9 +117,9 @@ public class App {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds A2 document jobs to the collecting parameter according to some policy that can be whatever the client
|
||||
* (the print center) wants. The code is similar to the 'addA4Papers' method. The code can be changed to accommodate
|
||||
* the wants of the client.
|
||||
* Adds A2 document jobs to the collecting parameter according to some policy that can be whatever
|
||||
* the client (the print center) wants. The code is similar to the 'addA4Papers' method. The code
|
||||
* can be changed to accommodate the wants of the client.
|
||||
*
|
||||
* @param printerItemsCollection the collecting parameter
|
||||
*/
|
||||
@@ -126,9 +127,10 @@ public class App {
|
||||
for (PrinterItem nextItem : printerQueue.getPrinterQueue()) {
|
||||
if (nextItem.paperSize.equals(PaperSizes.A2)) {
|
||||
|
||||
// Encoding the policy into a Boolean: the A2 paper must be single page, single-sided, and non-coloured.
|
||||
var isNotColouredSingleSidedAndOnePage = nextItem.pageCount == 1 && !nextItem.isDoubleSided
|
||||
&& !nextItem.isColour;
|
||||
// Encoding the policy into a Boolean: the A2 paper must be single page, single-sided, and
|
||||
// non-coloured.
|
||||
var isNotColouredSingleSidedAndOnePage =
|
||||
nextItem.pageCount == 1 && !nextItem.isDoubleSided && !nextItem.isColour;
|
||||
if (isNotColouredSingleSidedAndOnePage) {
|
||||
printerItemsCollection.add(nextItem);
|
||||
}
|
||||
|
||||
+3
-8
@@ -26,18 +26,14 @@ package com.iluwatar.collectingparameter;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* This class represents a Print Item, that should be added to the queue.
|
||||
**/
|
||||
/** This class represents a Print Item, that should be added to the queue. */
|
||||
public class PrinterItem {
|
||||
PaperSizes paperSize;
|
||||
int pageCount;
|
||||
boolean isDoubleSided;
|
||||
boolean isColour;
|
||||
|
||||
/**
|
||||
* The {@link PrinterItem} constructor.
|
||||
**/
|
||||
/** The {@link PrinterItem} constructor. */
|
||||
public PrinterItem(PaperSizes paperSize, int pageCount, boolean isDoubleSided, boolean isColour) {
|
||||
if (!Objects.isNull(paperSize)) {
|
||||
this.paperSize = paperSize;
|
||||
@@ -53,6 +49,5 @@ public class PrinterItem {
|
||||
|
||||
this.isColour = isColour;
|
||||
this.isDoubleSided = isDoubleSided;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-10
@@ -29,15 +29,17 @@ import java.util.Objects;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* This class represents a singleton Printer Queue. It contains a queue that can be filled up with {@link PrinterItem}.
|
||||
**/
|
||||
* This class represents a singleton Printer Queue. It contains a queue that can be filled up with
|
||||
* {@link PrinterItem}.
|
||||
*/
|
||||
public class PrinterQueue {
|
||||
|
||||
static PrinterQueue currentInstance = null;
|
||||
private final Queue<PrinterItem> printerItemQueue;
|
||||
|
||||
/**
|
||||
* This class is a singleton. The getInstance method will ensure that only one instance exists at a time.
|
||||
* This class is a singleton. The getInstance method will ensure that only one instance exists at
|
||||
* a time.
|
||||
*/
|
||||
public static PrinterQueue getInstance() {
|
||||
if (Objects.isNull(currentInstance)) {
|
||||
@@ -46,16 +48,12 @@ public class PrinterQueue {
|
||||
return currentInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty the printer queue.
|
||||
*/
|
||||
/** Empty the printer queue. */
|
||||
public void emptyQueue() {
|
||||
currentInstance.getPrinterQueue().clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor prevents instantiation, unless using the getInstance() method.
|
||||
*/
|
||||
/** Private constructor prevents instantiation, unless using the getInstance() method. */
|
||||
private PrinterQueue() {
|
||||
printerItemQueue = new LinkedList<>();
|
||||
}
|
||||
@@ -72,5 +70,4 @@ public class PrinterQueue {
|
||||
public void addPrinterItem(PrinterItem printerItem) {
|
||||
currentInstance.getPrinterQueue().add(printerItem);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user