Resolves checkstyle errors for remaining p (#1091)
* Reduces checkstyle errors in page-object * Reduces checkstyle errors in partial-response * Reduces checkstyle errors in pipeline * Reduces checkstyle errors in poison-pill * Reduces checkstyle errors in priority-queue * Reduces checkstyle errors in private-class-data * Reduces checkstyle errors in property * Reduces checkstyle errors in prototype * Reduces checkstyle errors in proxy
This commit is contained in:
committed by
Ilkka Seppälä
parent
1fdc650545
commit
271d7ae9bd
@ -26,63 +26,60 @@ package com.iluwatar.pageobject;
|
|||||||
import java.awt.Desktop;
|
import java.awt.Desktop;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Page Object pattern wraps an UI component with an application specific API allowing you to
|
* Page Object pattern wraps an UI component with an application specific API allowing you to
|
||||||
* manipulate the UI elements without having to dig around with the underlying UI technology used. This is
|
* manipulate the UI elements without having to dig around with the underlying UI technology used.
|
||||||
* especially useful for testing as it means your tests will be less brittle. Your tests can concentrate on
|
* This is especially useful for testing as it means your tests will be less brittle. Your tests can
|
||||||
* the actual test cases where as the manipulation of the UI can be left to the internals of the page object
|
* concentrate on the actual test cases where as the manipulation of the UI can be left to the
|
||||||
* itself.
|
* internals of the page object itself.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>Due to this reason, it has become very popular within the test automation community. In
|
||||||
* Due to this reason, it has become very popular within the test automation community.
|
* particular, it is very common in that the page object is used to represent the html pages of a
|
||||||
* In particular, it is very common in that the page object is used to represent the html pages of a
|
* web application that is under test. This web application is referred to as AUT (Application Under
|
||||||
* web application that is under test. This web application is referred to as AUT (Application Under Test).
|
* Test). A web browser automation tool/framework like Selenium for instance, is then used to drive
|
||||||
* A web browser automation tool/framework like Selenium for instance, is then used to drive the automating
|
* the automating of the browser navigation and user actions journeys through this web application.
|
||||||
* of the browser navigation and user actions journeys through this web application. Your test class would
|
* Your test class would therefore only be responsible for particular test cases and page object
|
||||||
* therefore only be responsible for particular test cases and page object would be used by the test class
|
* would be used by the test class for UI manipulation required for the tests.
|
||||||
* for UI manipulation required for the tests.
|
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>In this implementation rather than using Selenium, the HtmlUnit library is used as a
|
||||||
* In this implementation rather than using Selenium, the HtmlUnit library is used as a replacement to
|
* replacement to represent the specific html elements and to drive the browser. The purpose of this
|
||||||
* represent the specific html elements and to drive the browser. The purpose of this example is just to
|
* example is just to provide a simple version that showcase the intentions of this pattern and how
|
||||||
* provide a simple version that showcase the intentions of this pattern and how this pattern is used
|
* this pattern is used in order to understand it.
|
||||||
* in order to understand it.
|
|
||||||
*/
|
*/
|
||||||
public final class App {
|
public final class App {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
private App() {
|
private App() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application entry point
|
* Application entry point
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>The application under development is a web application. Normally you would probably have a
|
||||||
* The application under development is a web application. Normally you would probably have a
|
* backend that is probably implemented in an object-oriented language (e.g. Java) that serves the
|
||||||
* backend that is probably implemented in an object-oriented language (e.g. Java) that serves
|
* frontend which comprises of a series of HTML, CSS, JS etc...
|
||||||
* the frontend which comprises of a series of HTML, CSS, JS etc...
|
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>For illustrations purposes only, a very simple static html app is used here. This main
|
||||||
* For illustrations purposes only, a very simple static html app is used here. This main method
|
* method just fires up this simple web app in a default browser.
|
||||||
* just fires up this simple web app in a default browser.
|
|
||||||
*
|
*
|
||||||
* @param args arguments
|
* @param args arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File applicationFile = new File(App.class.getClassLoader().getResource("sample-ui/login.html").getPath());
|
File applicationFile =
|
||||||
|
new File(App.class.getClassLoader().getResource("sample-ui/login.html").getPath());
|
||||||
|
|
||||||
// should work for unix like OS (mac, unix etc...)
|
// should work for unix like OS (mac, unix etc...)
|
||||||
if (Desktop.isDesktopSupported()) {
|
if (Desktop.isDesktopSupported()) {
|
||||||
Desktop.getDesktop().open(applicationFile);
|
Desktop.getDesktop().open(applicationFile);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// java Desktop not supported - above unlikely to work for Windows so try following instead...
|
// java Desktop not supported - above unlikely to work for Windows so try instead...
|
||||||
Runtime.getRuntime().exec("cmd.exe start " + applicationFile);
|
Runtime.getRuntime().exec("cmd.exe start " + applicationFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,25 +29,23 @@ import java.io.IOException;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Page Object pattern wraps an UI component with an application specific API allowing you to
|
* Page Object pattern wraps an UI component with an application specific API allowing you to
|
||||||
* manipulate the UI elements without having to dig around with the underlying UI technology used. This is
|
* manipulate the UI elements without having to dig around with the underlying UI technology used.
|
||||||
* especially useful for testing as it means your tests will be less brittle. Your tests can concentrate on
|
* This is especially useful for testing as it means your tests will be less brittle. Your tests can
|
||||||
* the actual test cases where as the manipulation of the UI can be left to the internals of the page object
|
* concentrate on the actual test cases where as the manipulation of the UI can be left to the
|
||||||
* itself.
|
* internals of the page object itself.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>Due to this reason, it has become very popular within the test automation community. In
|
||||||
* Due to this reason, it has become very popular within the test automation community.
|
* particular, it is very common in that the page object is used to represent the html pages of a
|
||||||
* In particular, it is very common in that the page object is used to represent the html pages of a
|
* web application that is under test. This web application is referred to as AUT (Application Under
|
||||||
* web application that is under test. This web application is referred to as AUT (Application Under Test).
|
* Test). A web browser automation tool/framework like Selenium for instance, is then used to drive
|
||||||
* A web browser automation tool/framework like Selenium for instance, is then used to drive the automating
|
* the automating of the browser navigation and user actions journeys through this web application.
|
||||||
* of the browser navigation and user actions journeys through this web application. Your test class would
|
* Your test class would therefore only be responsible for particular test cases and page object
|
||||||
* therefore only be responsible for particular test cases and page object would be used by the test class
|
* would be used by the test class for UI manipulation required for the tests.
|
||||||
* for UI manipulation required for the tests.
|
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>In this implementation rather than using Selenium, the HtmlUnit library is used as a
|
||||||
* In this implementation rather than using Selenium, the HtmlUnit library is used as a replacement to
|
* replacement to represent the specific html elements and to drive the browser. The purpose of this
|
||||||
* represent the specific html elements and to drive the browser. The purpose of this example is just to
|
* example is just to provide a simple version that showcase the intentions of this pattern and how
|
||||||
* provide a simple version that showcase the intentions of this pattern and how this pattern is used
|
* this pattern is used in order to understand it.
|
||||||
* in order to understand it.
|
|
||||||
*/
|
*/
|
||||||
public final class App {
|
public final class App {
|
||||||
|
|
||||||
@ -57,21 +55,20 @@ public final class App {
|
|||||||
/**
|
/**
|
||||||
* Application entry point
|
* Application entry point
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>The application under development is a web application. Normally you would probably have a
|
||||||
* The application under development is a web application. Normally you would probably have a
|
* backend that is probably implemented in an object-oriented language (e.g. Java) that serves the
|
||||||
* backend that is probably implemented in an object-oriented language (e.g. Java) that serves
|
* frontend which comprises of a series of HTML, CSS, JS etc...
|
||||||
* the frontend which comprises of a series of HTML, CSS, JS etc...
|
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>For illustrations purposes only, a very simple static html app is used here. This main
|
||||||
* For illustrations purposes only, a very simple static html app is used here. This main method
|
* method just fires up this simple web app in a default browser.
|
||||||
* just fires up this simple web app in a default browser.
|
|
||||||
*
|
*
|
||||||
* @param args arguments
|
* @param args arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File applicationFile = new File(App.class.getClassLoader().getResource("sample-ui/login.html").getPath());
|
File applicationFile =
|
||||||
|
new File(App.class.getClassLoader().getResource("sample-ui/login.html").getPath());
|
||||||
|
|
||||||
// should work for unix like OS (mac, unix etc...)
|
// should work for unix like OS (mac, unix etc...)
|
||||||
if (Desktop.isDesktopSupported()) {
|
if (Desktop.isDesktopSupported()) {
|
||||||
|
@ -26,10 +26,8 @@ package com.iluwatar.pageobject;
|
|||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -46,7 +44,7 @@ public class AlbumListPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public AlbumListPage(WebClient webClient) {
|
public AlbumListPage(WebClient webClient) {
|
||||||
super(webClient);
|
super(webClient);
|
||||||
@ -54,7 +52,7 @@ public class AlbumListPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Navigates to the Album List Page
|
* Navigates to the Album List Page.
|
||||||
*
|
*
|
||||||
* @return {@link AlbumListPage}
|
* @return {@link AlbumListPage}
|
||||||
*/
|
*/
|
||||||
@ -76,7 +74,7 @@ public class AlbumListPage extends Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selects an album by the given album title
|
* Selects an album by the given album title.
|
||||||
*
|
*
|
||||||
* @param albumTitle the title of the album to click
|
* @param albumTitle the title of the album to click
|
||||||
* @return the album page
|
* @return the album page
|
||||||
|
@ -23,11 +23,6 @@
|
|||||||
|
|
||||||
package com.iluwatar.pageobject;
|
package com.iluwatar.pageobject;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlNumberInput;
|
import com.gargoylesoftware.htmlunit.html.HtmlNumberInput;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlOption;
|
import com.gargoylesoftware.htmlunit.html.HtmlOption;
|
||||||
@ -35,6 +30,9 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
|||||||
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
|
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Page Object encapsulating the Album Page (album-page.html)
|
* Page Object encapsulating the Album Page (album-page.html)
|
||||||
@ -49,7 +47,7 @@ public class AlbumPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public AlbumPage(WebClient webClient) {
|
public AlbumPage(WebClient webClient) {
|
||||||
super(webClient);
|
super(webClient);
|
||||||
@ -57,7 +55,7 @@ public class AlbumPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Navigates to the album page
|
* Navigates to the album page.
|
||||||
*
|
*
|
||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
*/
|
*/
|
||||||
@ -81,7 +79,7 @@ public class AlbumPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the album title input text field
|
* Sets the album title input text field.
|
||||||
*
|
*
|
||||||
* @param albumTitle the new album title value to set
|
* @param albumTitle the new album title value to set
|
||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
@ -94,7 +92,7 @@ public class AlbumPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the artist input text field
|
* Sets the artist input text field.
|
||||||
*
|
*
|
||||||
* @param artist the new artist value to set
|
* @param artist the new artist value to set
|
||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
@ -107,7 +105,7 @@ public class AlbumPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selects the select's option value based on the year value given
|
* Selects the select's option value based on the year value given.
|
||||||
*
|
*
|
||||||
* @param year the new year value to set
|
* @param year the new year value to set
|
||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
@ -121,7 +119,7 @@ public class AlbumPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the album rating input text field
|
* Sets the album rating input text field.
|
||||||
*
|
*
|
||||||
* @param albumRating the new album rating value to set
|
* @param albumRating the new album rating value to set
|
||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
@ -133,20 +131,21 @@ public class AlbumPage extends Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the number of songs number input field
|
* Sets the number of songs number input field.
|
||||||
*
|
*
|
||||||
* @param numberOfSongs the new number of songs value to be set
|
* @param numberOfSongs the new number of songs value to be set
|
||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
*/
|
*/
|
||||||
public AlbumPage changeNumberOfSongs(int numberOfSongs) {
|
public AlbumPage changeNumberOfSongs(int numberOfSongs) {
|
||||||
HtmlNumberInput numberOfSongsNumberField = (HtmlNumberInput) page.getElementById("numberOfSongs");
|
HtmlNumberInput numberOfSongsNumberField =
|
||||||
|
(HtmlNumberInput) page.getElementById("numberOfSongs");
|
||||||
numberOfSongsNumberField.setText(Integer.toString(numberOfSongs));
|
numberOfSongsNumberField.setText(Integer.toString(numberOfSongs));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cancel changes made by clicking the cancel button
|
* Cancel changes made by clicking the cancel button.
|
||||||
*
|
*
|
||||||
* @return {@link AlbumListPage}
|
* @return {@link AlbumListPage}
|
||||||
*/
|
*/
|
||||||
@ -162,7 +161,7 @@ public class AlbumPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves changes made by clicking the save button
|
* Saves changes made by clicking the save button.
|
||||||
*
|
*
|
||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
*/
|
*/
|
||||||
|
@ -29,7 +29,6 @@ import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
|
|||||||
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -45,7 +44,7 @@ public class LoginPage extends Page {
|
|||||||
private HtmlPage page;
|
private HtmlPage page;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param webClient {@link WebClient}
|
* @param webClient {@link WebClient}
|
||||||
*/
|
*/
|
||||||
@ -54,7 +53,7 @@ public class LoginPage extends Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Navigates to the Login page
|
* Navigates to the Login page.
|
||||||
*
|
*
|
||||||
* @return {@link LoginPage}
|
* @return {@link LoginPage}
|
||||||
*/
|
*/
|
||||||
@ -77,7 +76,7 @@ public class LoginPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enters the username into the username input text field
|
* Enters the username into the username input text field.
|
||||||
*
|
*
|
||||||
* @param username the username to enter
|
* @param username the username to enter
|
||||||
* @return {@link LoginPage}
|
* @return {@link LoginPage}
|
||||||
@ -90,23 +89,24 @@ public class LoginPage extends Page {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enters the password into the password input password field
|
* Enters the password into the password input password field.
|
||||||
*
|
*
|
||||||
* @param password the password to enter
|
* @param password the password to enter
|
||||||
* @return {@link LoginPage}
|
* @return {@link LoginPage}
|
||||||
*/
|
*/
|
||||||
public LoginPage enterPassword(String password) {
|
public LoginPage enterPassword(String password) {
|
||||||
HtmlPasswordInput passwordInputPasswordField = (HtmlPasswordInput) page.getElementById("password");
|
HtmlPasswordInput passwordInputPasswordField =
|
||||||
|
(HtmlPasswordInput) page.getElementById("password");
|
||||||
passwordInputPasswordField.setText(password);
|
passwordInputPasswordField.setText(password);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clicking on the login button to 'login'
|
* Clicking on the login button to 'login'.
|
||||||
*
|
*
|
||||||
* @return {@link AlbumListPage}
|
* @return {@link AlbumListPage} - this is the page that user gets navigated to once successfully
|
||||||
* - this is the page that user gets navigated to once successfully logged in
|
* logged in
|
||||||
*/
|
*/
|
||||||
public AlbumListPage login() {
|
public AlbumListPage login() {
|
||||||
HtmlSubmitInput loginButton = (HtmlSubmitInput) page.getElementById("loginButton");
|
HtmlSubmitInput loginButton = (HtmlSubmitInput) page.getElementById("loginButton");
|
||||||
|
@ -26,20 +26,19 @@ package com.iluwatar.pageobject;
|
|||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encapsulation for a generic 'Page'
|
* Encapsulation for a generic 'Page'.
|
||||||
*/
|
*/
|
||||||
public abstract class Page {
|
public abstract class Page {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application Under Test path
|
* Application Under Test path This directory location is where html web pages are located.
|
||||||
* This directory location is where html web pages are located
|
|
||||||
*/
|
*/
|
||||||
public static final String AUT_PATH = "../sample-application/src/main/resources/sample-ui/";
|
public static final String AUT_PATH = "../sample-application/src/main/resources/sample-ui/";
|
||||||
|
|
||||||
protected final WebClient webClient;
|
protected final WebClient webClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param webClient {@link WebClient}
|
* @param webClient {@link WebClient}
|
||||||
*/
|
*/
|
||||||
@ -48,7 +47,7 @@ public abstract class Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks that the current page is actually the page this page object represents
|
* Checks that the current page is actually the page this page object represents.
|
||||||
*
|
*
|
||||||
* @return true if so, otherwise false
|
* @return true if so, otherwise false
|
||||||
*/
|
*/
|
||||||
|
@ -23,18 +23,16 @@
|
|||||||
|
|
||||||
package com.iluwatar.partialresponse;
|
package com.iluwatar.partialresponse;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Partial response pattern is a design pattern in which client specifies fields to fetch to serve.
|
* The Partial response pattern is a design pattern in which client specifies fields to fetch to
|
||||||
* Here {@link App} is playing as client for {@link VideoResource} server.
|
* serve. Here {@link App} is playing as client for {@link VideoResource} server. Client ask for
|
||||||
* Client ask for specific fields information in video to server.
|
* specific fields information in video to server.
|
||||||
* <p>
|
*
|
||||||
* <p>
|
* <p>{@link VideoResource} act as server to serve video information.
|
||||||
* {@link VideoResource} act as server to serve video information.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
@ -47,12 +45,12 @@ public class App {
|
|||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Map<Integer, Video> videos = Map.of(
|
Map<Integer, Video> videos = Map.of(
|
||||||
1, new Video(1, "Avatar", 178, "epic science fiction film",
|
1, new Video(1, "Avatar", 178, "epic science fiction film",
|
||||||
"James Cameron", "English"),
|
"James Cameron", "English"),
|
||||||
2, new Video(2, "Godzilla Resurgence", 120, "Action & drama movie|",
|
2, new Video(2, "Godzilla Resurgence", 120, "Action & drama movie|",
|
||||||
"Hideaki Anno", "Japanese"),
|
"Hideaki Anno", "Japanese"),
|
||||||
3, new Video(3, "Interstellar", 169, "Adventure & Sci-Fi",
|
3, new Video(3, "Interstellar", 169, "Adventure & Sci-Fi",
|
||||||
"Christopher Nolan", "English"));
|
"Christopher Nolan", "English"));
|
||||||
VideoResource videoResource = new VideoResource(new FieldJsonMapper(), videos);
|
VideoResource videoResource = new VideoResource(new FieldJsonMapper(), videos);
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,11 +26,13 @@ package com.iluwatar.partialresponse;
|
|||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map a video to json
|
* Map a video to json.
|
||||||
*/
|
*/
|
||||||
public class FieldJsonMapper {
|
public class FieldJsonMapper {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Gets json of required fields from video.
|
||||||
|
*
|
||||||
* @param video object containing video information
|
* @param video object containing video information
|
||||||
* @param fields fields information to get
|
* @param fields fields information to get
|
||||||
* @return json of required fields from video
|
* @return json of required fields from video
|
||||||
|
@ -24,8 +24,7 @@
|
|||||||
package com.iluwatar.partialresponse;
|
package com.iluwatar.partialresponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Video} is a entity to serve from server.It contains all video related information..
|
* {@link Video} is a entity to serve from server.It contains all video related information.
|
||||||
* <p>
|
|
||||||
*/
|
*/
|
||||||
public class Video {
|
public class Video {
|
||||||
private final Integer id;
|
private final Integer id;
|
||||||
@ -36,23 +35,27 @@ public class Video {
|
|||||||
private final String language;
|
private final String language;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param id video unique id
|
* Constructor.
|
||||||
* @param title video title
|
*
|
||||||
* @param length video length in minutes
|
* @param id video unique id
|
||||||
* @param description video description by publisher
|
* @param title video title
|
||||||
* @param director video director name
|
* @param len video length in minutes
|
||||||
* @param language video language {private, public}
|
* @param desc video description by publisher
|
||||||
|
* @param director video director name
|
||||||
|
* @param lang video language {private, public}
|
||||||
*/
|
*/
|
||||||
public Video(Integer id, String title, Integer length, String description, String director, String language) {
|
public Video(Integer id, String title, Integer len, String desc, String director, String lang) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.length = length;
|
this.length = len;
|
||||||
this.description = description;
|
this.description = desc;
|
||||||
this.director = director;
|
this.director = director;
|
||||||
this.language = language;
|
this.language = lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* ToString.
|
||||||
|
*
|
||||||
* @return json representaion of video
|
* @return json representaion of video
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -26,14 +26,16 @@ package com.iluwatar.partialresponse;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The resource class which serves video information.
|
* The resource class which serves video information. This class act as server in the demo. Which
|
||||||
* This class act as server in the demo. Which has all video details.
|
* has all video details.
|
||||||
*/
|
*/
|
||||||
public class VideoResource {
|
public class VideoResource {
|
||||||
private FieldJsonMapper fieldJsonMapper;
|
private FieldJsonMapper fieldJsonMapper;
|
||||||
private Map<Integer, Video> videos;
|
private Map<Integer, Video> videos;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
* @param fieldJsonMapper map object to json.
|
* @param fieldJsonMapper map object to json.
|
||||||
* @param videos initialize resource with existing videos. Act as database.
|
* @param videos initialize resource with existing videos. Act as database.
|
||||||
*/
|
*/
|
||||||
@ -43,6 +45,8 @@ public class VideoResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Get Details.
|
||||||
|
*
|
||||||
* @param id video id
|
* @param id video id
|
||||||
* @param fields fields to get information about
|
* @param fields fields to get information about
|
||||||
* @return full response if no fields specified else partial response for given field.
|
* @return full response if no fields specified else partial response for given field.
|
||||||
|
@ -24,21 +24,20 @@
|
|||||||
package com.iluwatar.pipeline;
|
package com.iluwatar.pipeline;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Pipeline pattern uses ordered stages to process a sequence of input values.
|
* The Pipeline pattern uses ordered stages to process a sequence of input values. Each implemented
|
||||||
* Each implemented task is represented by a stage of the pipeline. You can think of
|
* task is represented by a stage of the pipeline. You can think of pipelines as similar to assembly
|
||||||
* pipelines as similar to assembly lines in a factory, where each item in the assembly
|
* lines in a factory, where each item in the assembly line is constructed in stages. The partially
|
||||||
* line is constructed in stages. The partially assembled item is passed from one assembly
|
* assembled item is passed from one assembly stage to another. The outputs of the assembly line
|
||||||
* stage to another. The outputs of the assembly line occur in the same order as that of the
|
* occur in the same order as that of the inputs.
|
||||||
* inputs.
|
|
||||||
*
|
*
|
||||||
* Classes used in this example are suffixed with "Handlers", and synonymously refers to the
|
* <p>Classes used in this example are suffixed with "Handlers", and synonymously refers to the
|
||||||
* "stage".
|
* "stage".
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
/**
|
/**
|
||||||
* Specify the initial input type for the first stage handler and the expected output type
|
* Specify the initial input type for the first stage handler and the expected output type of the
|
||||||
* of the last stage handler as type parameters for Pipeline. Use the fluent builder by
|
* last stage handler as type parameters for Pipeline. Use the fluent builder by calling
|
||||||
* calling addHandler to add more stage handlers on the pipeline.
|
* addHandler to add more stage handlers on the pipeline.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
/*
|
/*
|
||||||
|
@ -23,11 +23,10 @@
|
|||||||
|
|
||||||
package com.iluwatar.pipeline;
|
package com.iluwatar.pipeline;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stage handler that converts an input String to its char[] array counterpart.
|
* Stage handler that converts an input String to its char[] array counterpart.
|
||||||
*/
|
*/
|
||||||
@ -38,8 +37,10 @@ class ConvertToCharArrayHandler implements Handler<String, char[]> {
|
|||||||
@Override
|
@Override
|
||||||
public char[] process(String input) {
|
public char[] process(String input) {
|
||||||
char[] characters = input.toCharArray();
|
char[] characters = input.toCharArray();
|
||||||
LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
LOGGER
|
||||||
ConvertToCharArrayHandler.class, input, String.class, Arrays.toString(characters), Character[].class));
|
.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
||||||
|
ConvertToCharArrayHandler.class, input, String.class, Arrays
|
||||||
|
.toString(characters), Character[].class));
|
||||||
|
|
||||||
return characters;
|
return characters;
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,9 @@
|
|||||||
package com.iluwatar.pipeline;
|
package com.iluwatar.pipeline;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Forms a contract to all stage handlers to accept a certain type of input and return a processed output.
|
* Forms a contract to all stage handlers to accept a certain type of input and return a processed
|
||||||
|
* output.
|
||||||
|
*
|
||||||
* @param <I> the input type of the handler
|
* @param <I> the input type of the handler
|
||||||
* @param <O> the processed output type of the handler
|
* @param <O> the processed output type of the handler
|
||||||
*/
|
*/
|
||||||
|
@ -24,8 +24,9 @@
|
|||||||
package com.iluwatar.pipeline;
|
package com.iluwatar.pipeline;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main Pipeline class that initially sets the current handler. Processed output
|
* Main Pipeline class that initially sets the current handler. Processed output of the initial
|
||||||
* of the initial handler is then passed as the input to the next stage handlers.
|
* handler is then passed as the input to the next stage handlers.
|
||||||
|
*
|
||||||
* @param <I> the type of the input for the first stage handler
|
* @param <I> the type of the input for the first stage handler
|
||||||
* @param <O> the final stage handler's output type
|
* @param <O> the final stage handler's output type
|
||||||
*/
|
*/
|
||||||
|
@ -27,7 +27,8 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stage handler that returns a new instance of String without the alphabet characters of the input string.
|
* Stage handler that returns a new instance of String without the alphabet characters of the input
|
||||||
|
* string.
|
||||||
*/
|
*/
|
||||||
class RemoveAlphabetsHandler implements Handler<String, String> {
|
class RemoveAlphabetsHandler implements Handler<String, String> {
|
||||||
|
|
||||||
@ -47,8 +48,13 @@ class RemoveAlphabetsHandler implements Handler<String, String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String inputWithoutAlphabetsStr = inputWithoutAlphabets.toString();
|
String inputWithoutAlphabetsStr = inputWithoutAlphabets.toString();
|
||||||
LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
LOGGER.info(
|
||||||
RemoveAlphabetsHandler.class, input, String.class, inputWithoutAlphabetsStr, String.class));
|
String.format(
|
||||||
|
"Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
||||||
|
RemoveAlphabetsHandler.class, input,
|
||||||
|
String.class, inputWithoutAlphabetsStr, String.class
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
return inputWithoutAlphabetsStr;
|
return inputWithoutAlphabetsStr;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,8 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stage handler that returns a new instance of String without the digit characters of the input string.
|
* Stage handler that returns a new instance of String without the digit characters of the input
|
||||||
|
* string.
|
||||||
*/
|
*/
|
||||||
class RemoveDigitsHandler implements Handler<String, String> {
|
class RemoveDigitsHandler implements Handler<String, String> {
|
||||||
|
|
||||||
@ -47,8 +48,9 @@ class RemoveDigitsHandler implements Handler<String, String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String inputWithoutDigitsStr = inputWithoutDigits.toString();
|
String inputWithoutDigitsStr = inputWithoutDigits.toString();
|
||||||
LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
LOGGER
|
||||||
RemoveDigitsHandler.class, input, String.class, inputWithoutDigitsStr, String.class));
|
.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s",
|
||||||
|
RemoveDigitsHandler.class, input, String.class, inputWithoutDigitsStr, String.class));
|
||||||
|
|
||||||
return inputWithoutDigitsStr;
|
return inputWithoutDigitsStr;
|
||||||
}
|
}
|
||||||
|
@ -30,16 +30,15 @@ package com.iluwatar.poison.pill;
|
|||||||
* Pill will stop reading messages from the queue. You must also ensure that the Poison Pill will be
|
* Pill will stop reading messages from the queue. You must also ensure that the Poison Pill will be
|
||||||
* the last message that will be read from the queue (if you have prioritized queue then this can be
|
* the last message that will be read from the queue (if you have prioritized queue then this can be
|
||||||
* tricky).
|
* tricky).
|
||||||
* <p>
|
*
|
||||||
* In simple cases the Poison Pill can be just a null-reference, but holding a unique separate
|
* <p>In simple cases the Poison Pill can be just a null-reference, but holding a unique separate
|
||||||
* shared object-marker (with name "Poison" or "Poison Pill") is more clear and self describing.
|
* shared object-marker (with name "Poison" or "Poison Pill") is more clear and self describing.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -28,10 +28,10 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class responsible for receiving and handling submitted to the queue messages
|
* Class responsible for receiving and handling submitted to the queue messages.
|
||||||
*/
|
*/
|
||||||
public class Consumer {
|
public class Consumer {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(Consumer.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(Consumer.class);
|
||||||
|
|
||||||
private final MqSubscribePoint queue;
|
private final MqSubscribePoint queue;
|
||||||
@ -43,7 +43,7 @@ public class Consumer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Consume message
|
* Consume message.
|
||||||
*/
|
*/
|
||||||
public void consume() {
|
public void consume() {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -65,7 +65,7 @@ public interface Message {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enumeration of Type of Headers
|
* Enumeration of Type of Headers.
|
||||||
*/
|
*/
|
||||||
enum Headers {
|
enum Headers {
|
||||||
DATE, SENDER
|
DATE, SENDER
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
package com.iluwatar.poison.pill;
|
package com.iluwatar.poison.pill;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents abstraction of channel (or pipe) that bounds {@link Producer} and {@link Consumer}
|
* Represents abstraction of channel (or pipe) that bounds {@link Producer} and {@link Consumer}.
|
||||||
*/
|
*/
|
||||||
public interface MessageQueue extends MqPublishPoint, MqSubscribePoint {
|
public interface MessageQueue extends MqPublishPoint, MqSubscribePoint {
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
package com.iluwatar.poison.pill;
|
package com.iluwatar.poison.pill;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Endpoint to publish {@link Message} to queue
|
* Endpoint to publish {@link Message} to queue.
|
||||||
*/
|
*/
|
||||||
public interface MqPublishPoint {
|
public interface MqPublishPoint {
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
package com.iluwatar.poison.pill;
|
package com.iluwatar.poison.pill;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Endpoint to retrieve {@link Message} from queue
|
* Endpoint to retrieve {@link Message} from queue.
|
||||||
*/
|
*/
|
||||||
public interface MqSubscribePoint {
|
public interface MqSubscribePoint {
|
||||||
|
|
||||||
|
@ -23,15 +23,14 @@
|
|||||||
|
|
||||||
package com.iluwatar.poison.pill;
|
package com.iluwatar.poison.pill;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import com.iluwatar.poison.pill.Message.Headers;
|
import com.iluwatar.poison.pill.Message.Headers;
|
||||||
|
import java.util.Date;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class responsible for producing unit of work that can be expressed as message and submitted to
|
* Class responsible for producing unit of work that can be expressed as message and submitted to
|
||||||
* queue
|
* queue.
|
||||||
*/
|
*/
|
||||||
public class Producer {
|
public class Producer {
|
||||||
|
|
||||||
@ -42,7 +41,7 @@ public class Producer {
|
|||||||
private boolean isStopped;
|
private boolean isStopped;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public Producer(String name, MqPublishPoint queue) {
|
public Producer(String name, MqPublishPoint queue) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -51,7 +50,7 @@ public class Producer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send message to queue
|
* Send message to queue.
|
||||||
*/
|
*/
|
||||||
public void send(String body) {
|
public void send(String body) {
|
||||||
if (isStopped) {
|
if (isStopped) {
|
||||||
@ -72,7 +71,7 @@ public class Producer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop system by sending poison pill
|
* Stop system by sending poison pill.
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
isStopped = true;
|
isStopped = true;
|
||||||
|
@ -28,7 +28,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Message} basic implementation
|
* {@link Message} basic implementation.
|
||||||
*/
|
*/
|
||||||
public class SimpleMessage implements Message {
|
public class SimpleMessage implements Message {
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ import java.util.concurrent.ArrayBlockingQueue;
|
|||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bounded blocking queue wrapper
|
* Bounded blocking queue wrapper.
|
||||||
*/
|
*/
|
||||||
public class SimpleMessageQueue implements MessageQueue {
|
public class SimpleMessageQueue implements MessageQueue {
|
||||||
|
|
||||||
|
@ -25,16 +25,15 @@ package com.iluwatar.priority.queue;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Prioritize requests sent to services so that requests with a higher priority are received and
|
* Prioritize requests sent to services so that requests with a higher priority are received and
|
||||||
* processed more quickly than those of a lower priority.
|
* processed more quickly than those of a lower priority. This pattern is useful in applications
|
||||||
* This pattern is useful in applications that offer different service level guarantees
|
* that offer different service level guarantees to individual clients. Example :Send multiple
|
||||||
* to individual clients.
|
* message with different priority to worker queue. Worker execute higher priority message first
|
||||||
* Example :Send multiple message with different priority to worker queue.
|
*
|
||||||
* Worker execute higher priority message first
|
|
||||||
* @see "https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn589794(v=pandp.10)"
|
* @see "https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn589794(v=pandp.10)"
|
||||||
*/
|
*/
|
||||||
public class Application {
|
public class Application {
|
||||||
/**
|
/**
|
||||||
* main entry
|
* main entry.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
package com.iluwatar.priority.queue;
|
package com.iluwatar.priority.queue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Message bean
|
* Message bean.
|
||||||
*/
|
*/
|
||||||
public class Message implements Comparable<Message> {
|
public class Message implements Comparable<Message> {
|
||||||
private final String message;
|
private final String message;
|
||||||
|
@ -23,11 +23,11 @@
|
|||||||
|
|
||||||
package com.iluwatar.priority.queue;
|
package com.iluwatar.priority.queue;
|
||||||
|
|
||||||
|
import static java.util.Arrays.copyOf;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import static java.util.Arrays.copyOf;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keep high Priority message on top using maxHeap.
|
* Keep high Priority message on top using maxHeap.
|
||||||
*
|
*
|
||||||
@ -50,14 +50,14 @@ public class PriorityMessageQueue<T extends Comparable> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove top message from queue
|
* Remove top message from queue.
|
||||||
*/
|
*/
|
||||||
public T remove() {
|
public T remove() {
|
||||||
if (isEmpty()) {
|
if (isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
T root = queue[0];
|
final T root = queue[0];
|
||||||
queue[0] = queue[size - 1];
|
queue[0] = queue[size - 1];
|
||||||
size--;
|
size--;
|
||||||
maxHeapifyDown();
|
maxHeapifyDown();
|
||||||
@ -65,7 +65,7 @@ public class PriorityMessageQueue<T extends Comparable> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add message to queue
|
* Add message to queue.
|
||||||
*/
|
*/
|
||||||
public void add(T t) {
|
public void add(T t) {
|
||||||
ensureCapacity();
|
ensureCapacity();
|
||||||
@ -75,7 +75,7 @@ public class PriorityMessageQueue<T extends Comparable> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check queue size
|
* Check queue size.
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return size == 0;
|
return size == 0;
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
package com.iluwatar.priority.queue;
|
package com.iluwatar.priority.queue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manage priority queue
|
* Manage priority queue.
|
||||||
*/
|
*/
|
||||||
public class QueueManager {
|
public class QueueManager {
|
||||||
/*
|
/*
|
||||||
@ -37,7 +37,7 @@ public class QueueManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Publish message to queue
|
* Publish message to queue.
|
||||||
*/
|
*/
|
||||||
public void publishMessage(Message message) {
|
public void publishMessage(Message message) {
|
||||||
messagePriorityMessageQueue.add(message);
|
messagePriorityMessageQueue.add(message);
|
||||||
@ -45,7 +45,7 @@ public class QueueManager {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* recive message from queue
|
* recive message from queue.
|
||||||
*/
|
*/
|
||||||
public Message receiveMessage() {
|
public Message receiveMessage() {
|
||||||
if (messagePriorityMessageQueue.isEmpty()) {
|
if (messagePriorityMessageQueue.isEmpty()) {
|
||||||
|
@ -27,7 +27,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Message Worker
|
* Message Worker.
|
||||||
*/
|
*/
|
||||||
public class Worker {
|
public class Worker {
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ public class Worker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keep checking queue for message
|
* Keep checking queue for message.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("squid:S2189")
|
@SuppressWarnings("squid:S2189")
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
@ -56,7 +56,7 @@ public class Worker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process message
|
* Process message.
|
||||||
*/
|
*/
|
||||||
private void processMessage(Message message) {
|
private void processMessage(Message message) {
|
||||||
LOGGER.info(message.toString());
|
LOGGER.info(message.toString());
|
||||||
|
@ -24,26 +24,24 @@
|
|||||||
package com.iluwatar.privateclassdata;
|
package com.iluwatar.privateclassdata;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* The Private Class Data design pattern seeks to reduce exposure of attributes by limiting their
|
* The Private Class Data design pattern seeks to reduce exposure of attributes by limiting their
|
||||||
* visibility. It reduces the number of class attributes by encapsulating them in single data
|
* visibility. It reduces the number of class attributes by encapsulating them in single data
|
||||||
* object. It allows the class designer to remove write privilege of attributes that are intended to
|
* object. It allows the class designer to remove write privilege of attributes that are intended to
|
||||||
* be set only during construction, even from methods of the target class.
|
* be set only during construction, even from methods of the target class.
|
||||||
* <p>
|
|
||||||
* In the example we have normal {@link Stew} class with some ingredients given in constructor. Then
|
|
||||||
* we have methods to enumerate the ingredients and to taste the stew. The method for tasting the
|
|
||||||
* stew alters the private members of the {@link Stew} class.
|
|
||||||
*
|
|
||||||
* The problem is solved with the Private Class Data pattern. We introduce {@link ImmutableStew}
|
|
||||||
* class that contains {@link StewData}. The private data members of {@link Stew} are now in
|
|
||||||
* {@link StewData} and cannot be altered by {@link ImmutableStew} methods.
|
|
||||||
*
|
*
|
||||||
|
* <p>In the example we have normal {@link Stew} class with some ingredients given in constructor.
|
||||||
|
* Then we have methods to enumerate the ingredients and to taste the stew. The method for tasting
|
||||||
|
* the stew alters the private members of the {@link Stew} class.
|
||||||
|
*
|
||||||
|
* <p>The problem is solved with the Private Class Data pattern. We introduce {@link ImmutableStew}
|
||||||
|
* class that contains {@link StewData}. The private data members of {@link Stew} are now in {@link
|
||||||
|
* StewData} and cannot be altered by {@link ImmutableStew} methods.
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Immutable stew class, protected with Private Class Data pattern.
|
||||||
* Immutable stew class, protected with Private Class Data pattern
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ImmutableStew {
|
public class ImmutableStew {
|
||||||
|
|
||||||
@ -42,10 +40,11 @@ public class ImmutableStew {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mix the stew
|
* Mix the stew.
|
||||||
*/
|
*/
|
||||||
public void mix() {
|
public void mix() {
|
||||||
LOGGER.info("Mixing the immutable stew we find: {} potatoes, {} carrots, {} meat and {} peppers",
|
LOGGER
|
||||||
data.getNumPotatoes(), data.getNumCarrots(), data.getNumMeat(), data.getNumPeppers());
|
.info("Mixing the immutable stew we find: {} potatoes, {} carrots, {} meat and {} peppers",
|
||||||
|
data.getNumPotatoes(), data.getNumCarrots(), data.getNumMeat(), data.getNumPeppers());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Mutable stew class.
|
||||||
* Mutable stew class
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Stew {
|
public class Stew {
|
||||||
|
|
||||||
@ -41,7 +39,7 @@ public class Stew {
|
|||||||
private int numPeppers;
|
private int numPeppers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public Stew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
public Stew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
||||||
this.numPotatoes = numPotatoes;
|
this.numPotatoes = numPotatoes;
|
||||||
@ -51,7 +49,7 @@ public class Stew {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mix the stew
|
* Mix the stew.
|
||||||
*/
|
*/
|
||||||
public void mix() {
|
public void mix() {
|
||||||
LOGGER.info("Mixing the stew we find: {} potatoes, {} carrots, {} meat and {} peppers",
|
LOGGER.info("Mixing the stew we find: {} potatoes, {} carrots, {} meat and {} peppers",
|
||||||
@ -59,7 +57,7 @@ public class Stew {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Taste the stew
|
* Taste the stew.
|
||||||
*/
|
*/
|
||||||
public void taste() {
|
public void taste() {
|
||||||
LOGGER.info("Tasting the stew");
|
LOGGER.info("Tasting the stew");
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.privateclassdata;
|
package com.iluwatar.privateclassdata;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Stew ingredients.
|
||||||
* Stew ingredients
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class StewData {
|
public class StewData {
|
||||||
|
|
||||||
@ -36,7 +34,7 @@ public class StewData {
|
|||||||
private int numPeppers;
|
private int numPeppers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public StewData(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
public StewData(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
|
||||||
this.numPotatoes = numPotatoes;
|
this.numPotatoes = numPotatoes;
|
||||||
|
@ -28,25 +28,23 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* The Property pattern is also known as Prototype inheritance.
|
* The Property pattern is also known as Prototype inheritance.
|
||||||
* <p>
|
*
|
||||||
* In prototype inheritance instead of classes, as opposite to Java class inheritance, objects are
|
* <p>In prototype inheritance instead of classes, as opposite to Java class inheritance, objects
|
||||||
* used to create another objects and object hierarchies. Hierarchies are created using prototype
|
* are used to create another objects and object hierarchies. Hierarchies are created using
|
||||||
* chain through delegation: every object has link to parent object. Any base (parent) object can be
|
* prototype chain through delegation: every object has link to parent object. Any base (parent)
|
||||||
* amended at runtime (by adding or removal of some property), and all child objects will be
|
* object can be amended at runtime (by adding or removal of some property), and all child objects
|
||||||
* affected as result.
|
* will be affected as result.
|
||||||
* <p>
|
*
|
||||||
* In this example we demonstrate {@link Character} instantiation using the Property pattern.
|
* <p>In this example we demonstrate {@link Character} instantiation using the Property pattern.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -32,7 +32,7 @@ import java.util.Map;
|
|||||||
public class Character implements Prototype {
|
public class Character implements Prototype {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enumeration of Character types
|
* Enumeration of Character types.
|
||||||
*/
|
*/
|
||||||
public enum Type {
|
public enum Type {
|
||||||
WARRIOR, MAGE, ROGUE
|
WARRIOR, MAGE, ROGUE
|
||||||
@ -45,26 +45,28 @@ public class Character implements Prototype {
|
|||||||
private Type type;
|
private Type type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public Character() {
|
public Character() {
|
||||||
this.prototype = new Prototype() { // Null-value object
|
this.prototype = new Prototype() { // Null-value object
|
||||||
@Override
|
@Override
|
||||||
public Integer get(Stats stat) {
|
public Integer get(Stats stat) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean has(Stats stat) {
|
public boolean has(Stats stat) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void set(Stats stat, Integer val) {}
|
public void set(Stats stat, Integer val) {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Stats stat) {}
|
public void remove(Stats stat) {
|
||||||
};
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public Character(Type type, Prototype prototype) {
|
public Character(Type type, Prototype prototype) {
|
||||||
@ -73,7 +75,7 @@ public class Character implements Prototype {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public Character(String name, Character prototype) {
|
public Character(String name, Character prototype) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
package com.iluwatar.property;
|
package com.iluwatar.property;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface for prototype inheritance
|
* Interface for prototype inheritance.
|
||||||
*/
|
*/
|
||||||
public interface Prototype {
|
public interface Prototype {
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
package com.iluwatar.property;
|
package com.iluwatar.property;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All possible attributes that Character can have
|
* All possible attributes that Character can have.
|
||||||
*/
|
*/
|
||||||
public enum Stats {
|
public enum Stats {
|
||||||
|
|
||||||
|
@ -27,41 +27,43 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* The Prototype pattern is a creational design pattern in software development. It is used when the
|
* The Prototype pattern is a creational design pattern in software development. It is used when the
|
||||||
* type of objects to create is determined by a prototypical instance, which is cloned to produce
|
* type of objects to create is determined by a prototypical instance, which is cloned to produce
|
||||||
* new objects. This pattern is used to: - avoid subclasses of an object creator in the client
|
* new objects. This pattern is used to: - avoid subclasses of an object creator in the client
|
||||||
* application, like the abstract factory pattern does. - avoid the inherent cost of creating a new
|
* application, like the abstract factory pattern does. - avoid the inherent cost of creating a new
|
||||||
* object in the standard way (e.g., using the 'new' keyword)
|
* object in the standard way (e.g., using the 'new' keyword)
|
||||||
* <p>
|
*
|
||||||
* In this example we have a factory class ({@link HeroFactoryImpl}) producing objects by cloning
|
* <p>In this example we have a factory class ({@link HeroFactoryImpl}) producing objects by
|
||||||
* the existing ones. The factory's prototype objects are given as constructor parameters.
|
* cloning the existing ones. The factory's prototype objects are given as constructor parameters.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
HeroFactory factory;
|
HeroFactory factory = new HeroFactoryImpl(
|
||||||
Mage mage;
|
new ElfMage("cooking"),
|
||||||
Warlord warlord;
|
new ElfWarlord("cleaning"),
|
||||||
Beast beast;
|
new ElfBeast("protecting")
|
||||||
|
);
|
||||||
factory = new HeroFactoryImpl(new ElfMage("cooking"), new ElfWarlord("cleaning"), new ElfBeast("protecting"));
|
Mage mage = factory.createMage();
|
||||||
mage = factory.createMage();
|
Warlord warlord = factory.createWarlord();
|
||||||
warlord = factory.createWarlord();
|
Beast beast = factory.createBeast();
|
||||||
beast = factory.createBeast();
|
|
||||||
LOGGER.info(mage.toString());
|
LOGGER.info(mage.toString());
|
||||||
LOGGER.info(warlord.toString());
|
LOGGER.info(warlord.toString());
|
||||||
LOGGER.info(beast.toString());
|
LOGGER.info(beast.toString());
|
||||||
|
|
||||||
factory = new HeroFactoryImpl(new OrcMage("axe"), new OrcWarlord("sword"), new OrcBeast("laser"));
|
factory =
|
||||||
|
new HeroFactoryImpl(
|
||||||
|
new OrcMage("axe"),
|
||||||
|
new OrcWarlord("sword"),
|
||||||
|
new OrcBeast("laser")
|
||||||
|
);
|
||||||
mage = factory.createMage();
|
mage = factory.createMage();
|
||||||
warlord = factory.createWarlord();
|
warlord = factory.createWarlord();
|
||||||
beast = factory.createBeast();
|
beast = factory.createBeast();
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Beast.
|
||||||
* Beast
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public abstract class Beast extends Prototype {
|
public abstract class Beast extends Prototype {
|
||||||
|
|
||||||
|
@ -24,12 +24,10 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* ElfBeast.
|
||||||
* ElfBeast
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ElfBeast extends Beast {
|
public class ElfBeast extends Beast {
|
||||||
|
|
||||||
private String helpType;
|
private String helpType;
|
||||||
|
|
||||||
public ElfBeast(String helpType) {
|
public ElfBeast(String helpType) {
|
||||||
|
@ -24,15 +24,13 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* ElfMage.
|
||||||
* ElfMage
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ElfMage extends Mage {
|
public class ElfMage extends Mage {
|
||||||
|
|
||||||
|
|
||||||
private String helpType;
|
private String helpType;
|
||||||
|
|
||||||
public ElfMage(String helpType) {
|
public ElfMage(String helpType) {
|
||||||
this.helpType = helpType;
|
this.helpType = helpType;
|
||||||
}
|
}
|
||||||
|
@ -24,14 +24,12 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* ElfWarlord.
|
||||||
* ElfWarlord
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ElfWarlord extends Warlord {
|
public class ElfWarlord extends Warlord {
|
||||||
|
|
||||||
private String helpType;
|
private String helpType;
|
||||||
|
|
||||||
public ElfWarlord(String helpType) {
|
public ElfWarlord(String helpType) {
|
||||||
this.helpType = helpType;
|
this.helpType = helpType;
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Interface for the factory class.
|
* Interface for the factory class.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface HeroFactory {
|
public interface HeroFactory {
|
||||||
|
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Concrete factory class.
|
* Concrete factory class.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class HeroFactoryImpl implements HeroFactory {
|
public class HeroFactoryImpl implements HeroFactory {
|
||||||
|
|
||||||
@ -35,7 +33,7 @@ public class HeroFactoryImpl implements HeroFactory {
|
|||||||
private Beast beast;
|
private Beast beast;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
|
public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
|
||||||
this.mage = mage;
|
this.mage = mage;
|
||||||
@ -44,7 +42,7 @@ public class HeroFactoryImpl implements HeroFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create mage
|
* Create mage.
|
||||||
*/
|
*/
|
||||||
public Mage createMage() {
|
public Mage createMage() {
|
||||||
try {
|
try {
|
||||||
@ -55,7 +53,7 @@ public class HeroFactoryImpl implements HeroFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create warlord
|
* Create warlord.
|
||||||
*/
|
*/
|
||||||
public Warlord createWarlord() {
|
public Warlord createWarlord() {
|
||||||
try {
|
try {
|
||||||
@ -66,7 +64,7 @@ public class HeroFactoryImpl implements HeroFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create beast
|
* Create beast.
|
||||||
*/
|
*/
|
||||||
public Beast createBeast() {
|
public Beast createBeast() {
|
||||||
try {
|
try {
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Mage.
|
||||||
* Mage
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public abstract class Mage extends Prototype {
|
public abstract class Mage extends Prototype {
|
||||||
|
|
||||||
|
@ -24,18 +24,16 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* OrcBeast.
|
||||||
* OrcBeast
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class OrcBeast extends Beast {
|
public class OrcBeast extends Beast {
|
||||||
|
|
||||||
private String weapon;
|
private String weapon;
|
||||||
|
|
||||||
public OrcBeast(String weapon) {
|
public OrcBeast(String weapon) {
|
||||||
this.weapon = weapon;
|
this.weapon = weapon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OrcBeast(OrcBeast orcBeast) {
|
public OrcBeast(OrcBeast orcBeast) {
|
||||||
this.weapon = orcBeast.weapon;
|
this.weapon = orcBeast.weapon;
|
||||||
}
|
}
|
||||||
@ -49,6 +47,6 @@ public class OrcBeast extends Beast {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "Orcish wolf attacks with " + weapon;
|
return "Orcish wolf attacks with " + weapon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* OrcMage.
|
||||||
* OrcMage
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class OrcMage extends Mage {
|
public class OrcMage extends Mage {
|
||||||
|
|
||||||
@ -35,7 +33,7 @@ public class OrcMage extends Mage {
|
|||||||
public OrcMage(String weapon) {
|
public OrcMage(String weapon) {
|
||||||
this.weapon = weapon;
|
this.weapon = weapon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OrcMage(OrcMage orcMage) {
|
public OrcMage(OrcMage orcMage) {
|
||||||
this.weapon = orcMage.weapon;
|
this.weapon = orcMage.weapon;
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* OrcWarlord.
|
||||||
* OrcWarlord
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class OrcWarlord extends Warlord {
|
public class OrcWarlord extends Warlord {
|
||||||
|
|
||||||
@ -35,7 +33,7 @@ public class OrcWarlord extends Warlord {
|
|||||||
public OrcWarlord(String weapon) {
|
public OrcWarlord(String weapon) {
|
||||||
this.weapon = weapon;
|
this.weapon = weapon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OrcWarlord(OrcWarlord orcWarlord) {
|
public OrcWarlord(OrcWarlord orcWarlord) {
|
||||||
this.weapon = orcWarlord.weapon;
|
this.weapon = orcWarlord.weapon;
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Prototype.
|
||||||
* Prototype
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public abstract class Prototype implements Cloneable {
|
public abstract class Prototype implements Cloneable {
|
||||||
|
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.prototype;
|
package com.iluwatar.prototype;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Warlord.
|
||||||
* Warlord
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public abstract class Warlord extends Prototype {
|
public abstract class Warlord extends Prototype {
|
||||||
|
|
||||||
|
@ -24,25 +24,23 @@
|
|||||||
package com.iluwatar.proxy;
|
package com.iluwatar.proxy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* A proxy, in its most general form, is a class functioning as an interface to something else. The
|
* A proxy, in its most general form, is a class functioning as an interface to something else. The
|
||||||
* proxy could interface to anything: a network connection, a large object in memory, a file, or
|
* proxy could interface to anything: a network connection, a large object in memory, a file, or
|
||||||
* some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper
|
* some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper
|
||||||
* or agent object that is being called by the client to access the real serving object behind the
|
* or agent object that is being called by the client to access the real serving object behind the
|
||||||
* scenes.
|
* scenes.
|
||||||
* <p>
|
*
|
||||||
* The Proxy design pattern allows you to provide an interface to other objects by creating a
|
* <p>The Proxy design pattern allows you to provide an interface to other objects by creating a
|
||||||
* wrapper class as the proxy. The wrapper class, which is the proxy, can add additional
|
* wrapper class as the proxy. The wrapper class, which is the proxy, can add additional
|
||||||
* functionality to the object of interest without changing the object's code.
|
* functionality to the object of interest without changing the object's code.
|
||||||
* <p>
|
*
|
||||||
* In this example the proxy ({@link WizardTowerProxy}) controls access to the actual object (
|
* <p>In this example the proxy ({@link WizardTowerProxy}) controls access to the actual object (
|
||||||
* {@link IvoryTower}).
|
* {@link IvoryTower}).
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* The object to be proxyed.
|
* The object to be proxyed.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class IvoryTower implements WizardTower {
|
public class IvoryTower implements WizardTower {
|
||||||
|
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.proxy;
|
package com.iluwatar.proxy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Wizard.
|
||||||
* Wizard
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Wizard {
|
public class Wizard {
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
package com.iluwatar.proxy;
|
package com.iluwatar.proxy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WizardTower interface
|
* WizardTower interface.
|
||||||
*/
|
*/
|
||||||
public interface WizardTower {
|
public interface WizardTower {
|
||||||
|
|
||||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* The proxy controlling access to the {@link IvoryTower}.
|
* The proxy controlling access to the {@link IvoryTower}.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class WizardTowerProxy implements WizardTower {
|
public class WizardTowerProxy implements WizardTower {
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user