Resolves checkstyle errors for feature-toggle fluentinterface flux flyweight front-controller (#1078)
* Reduces checkstyle errors in feature-toggle * Reduces checkstyle errors in fluentinterface * Reduces checkstyle errors in flux * Reduces checkstyle errors in flyweight * Reduces checkstyle errors in front-controller
This commit is contained in:
parent
c954a436ad
commit
37599eb48f
@ -28,46 +28,47 @@ import com.iluwatar.featuretoggle.pattern.propertiesversion.PropertiesFeatureTog
|
||||
import com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion;
|
||||
import com.iluwatar.featuretoggle.user.User;
|
||||
import com.iluwatar.featuretoggle.user.UserGroup;
|
||||
import java.util.Properties;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* The Feature Toggle pattern allows for complete code executions to be turned on or off with ease. This allows features
|
||||
* to be controlled by either dynamic methods just as {@link User} information or by {@link Properties}. In the App
|
||||
* below there are two examples. Firstly the {@link Properties} version of the feature toggle, where the enhanced
|
||||
* version of the welcome message which is personalised is turned either on or off at instance creation. This method
|
||||
* is not as dynamic as the {@link User} driven version where the feature of the personalised welcome message is
|
||||
* The Feature Toggle pattern allows for complete code executions to be turned on or off with ease.
|
||||
* This allows features to be controlled by either dynamic methods just as {@link User} information
|
||||
* or by {@link Properties}. In the App below there are two examples. Firstly the {@link Properties}
|
||||
* version of the feature toggle, where the enhanced version of the welcome message which is
|
||||
* personalised is turned either on or off at instance creation. This method is not as dynamic as
|
||||
* the {@link User} driven version where the feature of the personalised welcome message is
|
||||
* dependant on the {@link UserGroup} the {@link User} is in. So if the user is a memeber of the
|
||||
* {@link UserGroup#isPaid(User)} then they get an ehanced version of the welcome message.
|
||||
*
|
||||
* Note that this pattern can easily introduce code complexity, and if not kept in check can result in redundant
|
||||
* unmaintained code within the codebase.
|
||||
*
|
||||
* <p>Note that this pattern can easily introduce code complexity, and if not kept in check can
|
||||
* result in redundant unmaintained code within the codebase.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Block 1 shows the {@link PropertiesFeatureToggleVersion} being run with {@link Properties} setting the feature
|
||||
* toggle to enabled.
|
||||
* Block 1 shows the {@link PropertiesFeatureToggleVersion} being run with {@link Properties}
|
||||
* setting the feature toggle to enabled.
|
||||
*
|
||||
* Block 2 shows the {@link PropertiesFeatureToggleVersion} being run with {@link Properties} setting the feature
|
||||
* toggle to disabled. Notice the difference with the printed welcome message the username is not included.
|
||||
* <p>Block 2 shows the {@link PropertiesFeatureToggleVersion} being run with {@link Properties}
|
||||
* setting the feature toggle to disabled. Notice the difference with the printed welcome message
|
||||
* the username is not included.
|
||||
*
|
||||
* Block 3 shows the {@link com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion} being
|
||||
* set up with two users on who is on the free level, while the other is on the paid level. When the
|
||||
* {@link Service#getWelcomeMessage(User)} is called with the paid {@link User} note that the welcome message
|
||||
* contains their username, while the same service call with the free tier user is more generic. No username is
|
||||
* printed.
|
||||
* <p>Block 3 shows the {@link
|
||||
* com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion} being set up with
|
||||
* two users on who is on the free level, while the other is on the paid level. When the {@link
|
||||
* Service#getWelcomeMessage(User)} is called with the paid {@link User} note that the welcome
|
||||
* message contains their username, while the same service call with the free tier user is more
|
||||
* generic. No username is printed.
|
||||
*
|
||||
* @see User
|
||||
* @see UserGroup
|
||||
* @see Service
|
||||
* @see PropertiesFeatureToggleVersion
|
||||
* @see com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion
|
||||
* @see User
|
||||
* @see UserGroup
|
||||
* @see Service
|
||||
* @see PropertiesFeatureToggleVersion
|
||||
* @see com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
@ -82,11 +83,12 @@ public class App {
|
||||
final Properties turnedOff = new Properties();
|
||||
turnedOff.put("enhancedWelcome", false);
|
||||
Service turnedOffService = new PropertiesFeatureToggleVersion(turnedOff);
|
||||
final String welcomeMessageturnedOff = turnedOffService.getWelcomeMessage(new User("Jamie No Code"));
|
||||
final String welcomeMessageturnedOff =
|
||||
turnedOffService.getWelcomeMessage(new User("Jamie No Code"));
|
||||
LOGGER.info(welcomeMessageturnedOff);
|
||||
|
||||
// --------------------------------------------
|
||||
|
||||
|
||||
Service service2 = new TieredFeatureToggleVersion();
|
||||
|
||||
final User paidUser = new User("Jamie Coder");
|
||||
|
@ -26,10 +26,11 @@ package com.iluwatar.featuretoggle.pattern;
|
||||
import com.iluwatar.featuretoggle.user.User;
|
||||
|
||||
/**
|
||||
* Simple interfaces to allow the calling of the method to generate the welcome message for a given user. While there is
|
||||
* a helper method to gather the the status of the feature toggle. In some cases there is no need for the
|
||||
* {@link Service#isEnhanced()} in {@link com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion}
|
||||
* where the toggle is determined by the actual {@link User}.
|
||||
* Simple interfaces to allow the calling of the method to generate the welcome message for a given
|
||||
* user. While there is a helper method to gather the the status of the feature toggle. In some
|
||||
* cases there is no need for the {@link Service#isEnhanced()} in {@link
|
||||
* com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion} where the toggle is
|
||||
* determined by the actual {@link User}.
|
||||
*
|
||||
* @see com.iluwatar.featuretoggle.pattern.propertiesversion.PropertiesFeatureToggleVersion
|
||||
* @see com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion
|
||||
|
@ -25,16 +25,16 @@ package com.iluwatar.featuretoggle.pattern.propertiesversion;
|
||||
|
||||
import com.iluwatar.featuretoggle.pattern.Service;
|
||||
import com.iluwatar.featuretoggle.user.User;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* This example of the Feature Toogle pattern is less dynamic version than
|
||||
* {@link com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion} where the feature is turned on
|
||||
* or off at the time of creation of the service. This example uses simple Java {@link Properties} however it could as
|
||||
* easily be done with an external configuration file loaded by Spring and so on. A good example of when to use this
|
||||
* version of the feature toggle is when new features are being developed. So you could have a configuration property
|
||||
* boolean named development or some sort of system environment variable.
|
||||
* This example of the Feature Toogle pattern is less dynamic version than {@link
|
||||
* com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion} where the feature is
|
||||
* turned on or off at the time of creation of the service. This example uses simple Java {@link
|
||||
* Properties} however it could as easily be done with an external configuration file loaded by
|
||||
* Spring and so on. A good example of when to use this version of the feature toggle is when new
|
||||
* features are being developed. So you could have a configuration property boolean named
|
||||
* development or some sort of system environment variable.
|
||||
*
|
||||
* @see Service
|
||||
* @see com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion
|
||||
@ -45,9 +45,10 @@ public class PropertiesFeatureToggleVersion implements Service {
|
||||
private boolean isEnhanced;
|
||||
|
||||
/**
|
||||
* Creates an instance of {@link PropertiesFeatureToggleVersion} using the passed {@link Properties} to determine,
|
||||
* the status of the feature toggle {@link PropertiesFeatureToggleVersion#isEnhanced()}. There is also some defensive
|
||||
* code to ensure the {@link Properties} passed are as expected.
|
||||
* Creates an instance of {@link PropertiesFeatureToggleVersion} using the passed {@link
|
||||
* Properties} to determine, the status of the feature toggle {@link
|
||||
* PropertiesFeatureToggleVersion#isEnhanced()}. There is also some defensive code to ensure the
|
||||
* {@link Properties} passed are as expected.
|
||||
*
|
||||
* @param properties {@link Properties} used to configure the service and toggle features.
|
||||
* @throws IllegalArgumentException when the passed {@link Properties} is not as expected
|
||||
@ -66,14 +67,14 @@ public class PropertiesFeatureToggleVersion implements Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a welcome message based on the user being passed and the status of the feature toggle. If the enhanced
|
||||
* version is enabled, then the message will be personalised with the name of the passed {@link User}. However if
|
||||
* disabled then a generic version fo the message is returned.
|
||||
* Generate a welcome message based on the user being passed and the status of the feature toggle.
|
||||
* If the enhanced version is enabled, then the message will be personalised with the name of the
|
||||
* passed {@link User}. However if disabled then a generic version fo the message is returned.
|
||||
*
|
||||
* @param user the {@link User} to be displayed in the message if the enhanced version is enabled see
|
||||
* {@link PropertiesFeatureToggleVersion#isEnhanced()}. If the enhanced version is enabled, then the
|
||||
* message will be personalised with the name of the passed {@link User}. However if disabled then a
|
||||
* generic version fo the message is returned.
|
||||
* @param user the {@link User} to be displayed in the message if the enhanced version is enabled
|
||||
* see {@link PropertiesFeatureToggleVersion#isEnhanced()}. If the enhanced version is
|
||||
* enabled, then the message will be personalised with the name of the passed {@link
|
||||
* User}. However if disabled then a generic version fo the message is returned.
|
||||
* @return Resulting welcome message.
|
||||
* @see User
|
||||
*/
|
||||
@ -88,9 +89,9 @@ public class PropertiesFeatureToggleVersion implements Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that checks if the welcome message to be returned is the enhanced venison or not. For this service it will
|
||||
* see the value of the boolean that was set in the constructor
|
||||
* {@link PropertiesFeatureToggleVersion#PropertiesFeatureToggleVersion(Properties)}
|
||||
* Method that checks if the welcome message to be returned is the enhanced venison or not. For
|
||||
* this service it will see the value of the boolean that was set in the constructor {@link
|
||||
* PropertiesFeatureToggleVersion#PropertiesFeatureToggleVersion(Properties)}
|
||||
*
|
||||
* @return Boolean value {@code true} if enhanced.
|
||||
*/
|
||||
|
@ -28,11 +28,12 @@ import com.iluwatar.featuretoggle.user.User;
|
||||
import com.iluwatar.featuretoggle.user.UserGroup;
|
||||
|
||||
/**
|
||||
* This example of the Feature Toogle pattern shows how it could be implemented based on a {@link User}. Therefore
|
||||
* showing its use within a tiered application where the paying users get access to different content or
|
||||
* better versions of features. So in this instance a {@link User} is passed in and if they are found to be
|
||||
* on the {@link UserGroup#isPaid(User)} they are welcomed with a personalised message. While the other is more
|
||||
* generic. However this pattern is limited to simple examples such as the one below.
|
||||
* This example of the Feature Toogle pattern shows how it could be implemented based on a {@link
|
||||
* User}. Therefore showing its use within a tiered application where the paying users get access to
|
||||
* different content or better versions of features. So in this instance a {@link User} is passed in
|
||||
* and if they are found to be on the {@link UserGroup#isPaid(User)} they are welcomed with a
|
||||
* personalised message. While the other is more generic. However this pattern is limited to simple
|
||||
* examples such as the one below.
|
||||
*
|
||||
* @see Service
|
||||
* @see User
|
||||
@ -42,12 +43,13 @@ import com.iluwatar.featuretoggle.user.UserGroup;
|
||||
public class TieredFeatureToggleVersion implements Service {
|
||||
|
||||
/**
|
||||
* Generates a welcome message from the passed {@link User}. The resulting message depends on the group of the
|
||||
* {@link User}. So if the {@link User} is in the {@link UserGroup#paidGroup} then the enhanced version of the
|
||||
* welcome message will be returned where the username is displayed.
|
||||
* Generates a welcome message from the passed {@link User}. The resulting message depends on the
|
||||
* group of the {@link User}. So if the {@link User} is in the {@link UserGroup#paidGroup} then
|
||||
* the enhanced version of the welcome message will be returned where the username is displayed.
|
||||
*
|
||||
* @param user the {@link User} to generate the welcome message for, different messages are displayed if the user is
|
||||
* in the {@link UserGroup#isPaid(User)} or {@link UserGroup#freeGroup}
|
||||
* @param user the {@link User} to generate the welcome message for, different messages are
|
||||
* displayed if the user is in the {@link UserGroup#isPaid(User)} or {@link
|
||||
* UserGroup#freeGroup}
|
||||
* @return Resulting welcome message.
|
||||
* @see User
|
||||
* @see UserGroup
|
||||
@ -62,9 +64,9 @@ public class TieredFeatureToggleVersion implements Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that checks if the welcome message to be returned is the enhanced version. For this instance as the logic
|
||||
* is driven by the user group. This method is a little redundant. However can be used to show that there is an
|
||||
* enhanced version available.
|
||||
* Method that checks if the welcome message to be returned is the enhanced version. For this
|
||||
* instance as the logic is driven by the user group. This method is a little redundant. However
|
||||
* can be used to show that there is an enhanced version available.
|
||||
*
|
||||
* @return Boolean value {@code true} if enhanced.
|
||||
*/
|
||||
|
@ -24,7 +24,8 @@
|
||||
package com.iluwatar.featuretoggle.user;
|
||||
|
||||
/**
|
||||
* Used to demonstrate the purpose of the feature toggle. This class actually has nothing to do with the pattern.
|
||||
* Used to demonstrate the purpose of the feature toggle. This class actually has nothing to do with
|
||||
* the pattern.
|
||||
*/
|
||||
public class User {
|
||||
|
||||
@ -41,7 +42,9 @@ public class User {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @return The {@link String} representation of the User, in this case just return the name of the user.
|
||||
*
|
||||
* @return The {@link String} representation of the User, in this case just return the name of the
|
||||
* user.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
|
@ -27,8 +27,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Contains the lists of users of different groups paid and free. Used to demonstrate the tiered example of feature
|
||||
* toggle. Allowing certain features to be available to only certain groups of users.
|
||||
* Contains the lists of users of different groups paid and free. Used to demonstrate the tiered
|
||||
* example of feature toggle. Allowing certain features to be available to only certain groups of
|
||||
* users.
|
||||
*
|
||||
* @see User
|
||||
*/
|
||||
@ -76,7 +77,6 @@ public class UserGroup {
|
||||
* Method to take a {@link User} to determine if the user is in the {@link UserGroup#paidGroup}.
|
||||
*
|
||||
* @param user {@link User} to check if they are in the {@link UserGroup#paidGroup}
|
||||
*
|
||||
* @return true if the {@link User} is in {@link UserGroup#paidGroup}
|
||||
*/
|
||||
public static boolean isPaid(User user) {
|
||||
|
@ -23,39 +23,37 @@
|
||||
|
||||
package com.iluwatar.fluentinterface.app;
|
||||
|
||||
import static java.lang.String.valueOf;
|
||||
|
||||
import com.iluwatar.fluentinterface.fluentiterable.FluentIterable;
|
||||
import com.iluwatar.fluentinterface.fluentiterable.lazy.LazyFluentIterable;
|
||||
import com.iluwatar.fluentinterface.fluentiterable.simple.SimpleFluentIterable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static java.lang.String.valueOf;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The Fluent Interface pattern is useful when you want to provide an easy readable, flowing API.
|
||||
* Those interfaces tend to mimic domain specific languages, so they can nearly be read as human
|
||||
* languages.
|
||||
* <p>
|
||||
* In this example two implementations of a {@link FluentIterable} interface are given. The
|
||||
*
|
||||
* <p>In this example two implementations of a {@link FluentIterable} interface are given. The
|
||||
* {@link SimpleFluentIterable} evaluates eagerly and would be too costly for real world
|
||||
* applications. The {@link LazyFluentIterable} is evaluated on termination. Their usage is
|
||||
* demonstrated with a simple number list that is filtered, transformed and collected. The result is
|
||||
* printed afterwards.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* Program entry point.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
@ -90,16 +88,16 @@ public class App {
|
||||
List<String> lastTwoOfFirstFourStringMapped =
|
||||
LazyFluentIterable.from(integerList).filter(positives()).first(4).last(2)
|
||||
.map(number -> "String[" + valueOf(number) + "]").asList();
|
||||
prettyPrint(
|
||||
"The lazy list contains the last two of the first four positive numbers mapped to Strings: ",
|
||||
lastTwoOfFirstFourStringMapped);
|
||||
prettyPrint("The lazy list contains the last two of the first four positive numbers "
|
||||
+ "mapped to Strings: ", lastTwoOfFirstFourStringMapped);
|
||||
|
||||
LazyFluentIterable
|
||||
.from(integerList)
|
||||
.filter(negatives())
|
||||
.first(2)
|
||||
.last()
|
||||
.ifPresent(lastOfFirstTwo -> LOGGER.info("The last of the first two negatives is: {}", lastOfFirstTwo));
|
||||
.ifPresent(lastOfFirstTwo -> LOGGER
|
||||
.info("The last of the first two negatives is: {}", lastOfFirstTwo));
|
||||
}
|
||||
|
||||
private static Function<Integer, String> transformToString() {
|
||||
@ -119,7 +117,7 @@ public class App {
|
||||
}
|
||||
|
||||
private static <E> void prettyPrint(String delimiter, String prefix,
|
||||
Iterable<E> iterable) {
|
||||
Iterable<E> iterable) {
|
||||
StringJoiner joiner = new StringJoiner(delimiter, prefix, ".");
|
||||
Iterator<E> iterator = iterable.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
|
@ -34,7 +34,7 @@ import java.util.function.Predicate;
|
||||
* The FluentIterable is a more convenient implementation of the common iterable interface based on
|
||||
* the fluent interface design pattern. This interface defines common operations, but doesn't aim to
|
||||
* be complete. It was inspired by Guava's com.google.common.collect.FluentIterable.
|
||||
*
|
||||
*
|
||||
* @param <E> is the class of objects the iterable contains
|
||||
*/
|
||||
public interface FluentIterable<E> extends Iterable<E> {
|
||||
@ -42,9 +42,9 @@ public interface FluentIterable<E> extends Iterable<E> {
|
||||
/**
|
||||
* Filters the contents of Iterable using the given predicate, leaving only the ones which satisfy
|
||||
* the predicate.
|
||||
*
|
||||
*
|
||||
* @param predicate the condition to test with for the filtering. If the test is negative, the
|
||||
* tested object is removed by the iterator.
|
||||
* tested object is removed by the iterator.
|
||||
* @return a filtered FluentIterable
|
||||
*/
|
||||
FluentIterable<E> filter(Predicate<? super E> predicate);
|
||||
@ -52,53 +52,53 @@ public interface FluentIterable<E> extends Iterable<E> {
|
||||
/**
|
||||
* Returns an Optional containing the first element of this iterable if present, else returns
|
||||
* Optional.empty().
|
||||
*
|
||||
*
|
||||
* @return the first element after the iteration is evaluated
|
||||
*/
|
||||
Optional<E> first();
|
||||
|
||||
/**
|
||||
* Evaluates the iteration and leaves only the count first elements.
|
||||
*
|
||||
*
|
||||
* @return the first count elements as an Iterable
|
||||
*/
|
||||
FluentIterable<E> first(int count);
|
||||
|
||||
/**
|
||||
* Evaluates the iteration and returns the last element. This is a terminating operation.
|
||||
*
|
||||
*
|
||||
* @return the last element after the iteration is evaluated
|
||||
*/
|
||||
Optional<E> last();
|
||||
|
||||
/**
|
||||
* Evaluates the iteration and leaves only the count last elements.
|
||||
*
|
||||
*
|
||||
* @return the last counts elements as an Iterable
|
||||
*/
|
||||
FluentIterable<E> last(int count);
|
||||
|
||||
/**
|
||||
* Transforms this FluentIterable into a new one containing objects of the type T.
|
||||
*
|
||||
*
|
||||
* @param function a function that transforms an instance of E into an instance of T
|
||||
* @param <T> the target type of the transformation
|
||||
* @param <T> the target type of the transformation
|
||||
* @return a new FluentIterable of the new type
|
||||
*/
|
||||
<T> FluentIterable<T> map(Function<? super E, T> function);
|
||||
|
||||
/**
|
||||
* Returns the contents of this Iterable as a List.
|
||||
*
|
||||
*
|
||||
* @return a List representation of this Iterable
|
||||
*/
|
||||
List<E> asList();
|
||||
|
||||
/**
|
||||
* Utility method that iterates over iterable and adds the contents to a list.
|
||||
*
|
||||
*
|
||||
* @param iterable the iterable to collect
|
||||
* @param <E> the type of the objects to iterate
|
||||
* @param <E> the type of the objects to iterate
|
||||
* @return a list with all objects of the given iterator
|
||||
*/
|
||||
static <E> List<E> copyToList(Iterable<E> iterable) {
|
||||
|
@ -28,6 +28,7 @@ import java.util.Iterator;
|
||||
/**
|
||||
* This class is used to realize LazyFluentIterables. It decorates a given iterator. Does not
|
||||
* support consecutive hasNext() calls.
|
||||
*
|
||||
* @param <E> Iterable Collection of Elements of Type E
|
||||
*/
|
||||
public abstract class DecoratingIterator<E> implements Iterator<E> {
|
||||
|
@ -23,6 +23,7 @@
|
||||
|
||||
package com.iluwatar.fluentinterface.fluentiterable.lazy;
|
||||
|
||||
import com.iluwatar.fluentinterface.fluentiterable.FluentIterable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -30,12 +31,10 @@ import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.iluwatar.fluentinterface.fluentiterable.FluentIterable;
|
||||
|
||||
/**
|
||||
* This is a lazy implementation of the FluentIterable interface. It evaluates all chained
|
||||
* operations when a terminating operation is applied.
|
||||
*
|
||||
*
|
||||
* @param <E> the type of the objects the iteration is about
|
||||
*/
|
||||
public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
@ -44,7 +43,7 @@ public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* This constructor creates a new LazyFluentIterable. It wraps the given iterable.
|
||||
*
|
||||
*
|
||||
* @param iterable the iterable this FluentIterable works on.
|
||||
*/
|
||||
protected LazyFluentIterable(Iterable<E> iterable) {
|
||||
@ -61,9 +60,9 @@ public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
/**
|
||||
* Filters the contents of Iterable using the given predicate, leaving only the ones which satisfy
|
||||
* the predicate.
|
||||
*
|
||||
*
|
||||
* @param predicate the condition to test with for the filtering. If the test is negative, the
|
||||
* tested object is removed by the iterator.
|
||||
* tested object is removed by the iterator.
|
||||
* @return a new FluentIterable object that decorates the source iterable
|
||||
*/
|
||||
@Override
|
||||
@ -90,7 +89,7 @@ public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Can be used to collect objects from the iteration. Is a terminating operation.
|
||||
*
|
||||
*
|
||||
* @return an Optional containing the first object of this Iterable
|
||||
*/
|
||||
@Override
|
||||
@ -101,10 +100,10 @@ public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Can be used to collect objects from the iteration.
|
||||
*
|
||||
*
|
||||
* @param count defines the number of objects to return
|
||||
* @return the same FluentIterable with a collection decimated to a maximum of 'count' first
|
||||
* objects.
|
||||
* objects.
|
||||
*/
|
||||
@Override
|
||||
public FluentIterable<E> first(int count) {
|
||||
@ -130,7 +129,7 @@ public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Can be used to collect objects from the iteration. Is a terminating operation.
|
||||
*
|
||||
*
|
||||
* @return an Optional containing the last object of this Iterable
|
||||
*/
|
||||
@Override
|
||||
@ -143,10 +142,10 @@ public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
* Can be used to collect objects from the Iterable. Is a terminating operation. This operation is
|
||||
* memory intensive, because the contents of this Iterable are collected into a List, when the
|
||||
* next object is requested.
|
||||
*
|
||||
*
|
||||
* @param count defines the number of objects to return
|
||||
* @return the same FluentIterable with a collection decimated to a maximum of 'count' last
|
||||
* objects
|
||||
* objects
|
||||
*/
|
||||
@Override
|
||||
public FluentIterable<E> last(int count) {
|
||||
@ -193,9 +192,9 @@ public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Transforms this FluentIterable into a new one containing objects of the type T.
|
||||
*
|
||||
*
|
||||
* @param function a function that transforms an instance of E into an instance of T
|
||||
* @param <T> the target type of the transformation
|
||||
* @param <T> the target type of the transformation
|
||||
* @return a new FluentIterable of the new type
|
||||
*/
|
||||
@Override
|
||||
@ -222,7 +221,7 @@ public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Collects all remaining objects of this iteration into a list.
|
||||
*
|
||||
*
|
||||
* @return a list with all remaining objects of this iteration
|
||||
*/
|
||||
@Override
|
||||
@ -241,9 +240,11 @@ public class LazyFluentIterable<E> implements FluentIterable<E> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructors FluentIterable from given iterable.
|
||||
*
|
||||
* @return a FluentIterable from a given iterable. Calls the LazyFluentIterable constructor.
|
||||
*/
|
||||
public static final <E> FluentIterable<E> from(Iterable<E> iterable) {
|
||||
public static <E> FluentIterable<E> from(Iterable<E> iterable) {
|
||||
return new LazyFluentIterable<>(iterable);
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
|
||||
package com.iluwatar.fluentinterface.fluentiterable.simple;
|
||||
|
||||
import com.iluwatar.fluentinterface.fluentiterable.FluentIterable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -32,12 +33,10 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.iluwatar.fluentinterface.fluentiterable.FluentIterable;
|
||||
|
||||
/**
|
||||
* This is a simple implementation of the FluentIterable interface. It evaluates all chained
|
||||
* operations eagerly. This implementation would be costly to be utilized in real applications.
|
||||
*
|
||||
*
|
||||
* @param <E> the type of the objects the iteration is about
|
||||
*/
|
||||
public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
@ -46,7 +45,7 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* This constructor creates a copy of a given iterable's contents.
|
||||
*
|
||||
*
|
||||
* @param iterable the iterable this interface copies to work on.
|
||||
*/
|
||||
protected SimpleFluentIterable(Iterable<E> iterable) {
|
||||
@ -56,9 +55,9 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
/**
|
||||
* Filters the contents of Iterable using the given predicate, leaving only the ones which satisfy
|
||||
* the predicate.
|
||||
*
|
||||
*
|
||||
* @param predicate the condition to test with for the filtering. If the test is negative, the
|
||||
* tested object is removed by the iterator.
|
||||
* tested object is removed by the iterator.
|
||||
* @return the same FluentIterable with a filtered collection
|
||||
*/
|
||||
@Override
|
||||
@ -75,7 +74,7 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Can be used to collect objects from the Iterable. Is a terminating operation.
|
||||
*
|
||||
*
|
||||
* @return an option of the first object of the Iterable
|
||||
*/
|
||||
@Override
|
||||
@ -86,10 +85,10 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Can be used to collect objects from the Iterable. Is a terminating operation.
|
||||
*
|
||||
*
|
||||
* @param count defines the number of objects to return
|
||||
* @return the same FluentIterable with a collection decimated to a maximum of 'count' first
|
||||
* objects.
|
||||
* objects.
|
||||
*/
|
||||
@Override
|
||||
public final FluentIterable<E> first(int count) {
|
||||
@ -107,7 +106,7 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Can be used to collect objects from the Iterable. Is a terminating operation.
|
||||
*
|
||||
*
|
||||
* @return an option of the last object of the Iterable
|
||||
*/
|
||||
@Override
|
||||
@ -121,10 +120,10 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Can be used to collect objects from the Iterable. Is a terminating operation.
|
||||
*
|
||||
*
|
||||
* @param count defines the number of objects to return
|
||||
* @return the same FluentIterable with a collection decimated to a maximum of 'count' last
|
||||
* objects
|
||||
* objects
|
||||
*/
|
||||
@Override
|
||||
public final FluentIterable<E> last(int count) {
|
||||
@ -144,9 +143,9 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Transforms this FluentIterable into a new one containing objects of the type T.
|
||||
*
|
||||
*
|
||||
* @param function a function that transforms an instance of E into an instance of T
|
||||
* @param <T> the target type of the transformation
|
||||
* @param <T> the target type of the transformation
|
||||
* @return a new FluentIterable of the new type
|
||||
*/
|
||||
@Override
|
||||
@ -161,7 +160,7 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Collects all remaining objects of this Iterable into a list.
|
||||
*
|
||||
*
|
||||
* @return a list with all remaining objects of this Iterable
|
||||
*/
|
||||
@Override
|
||||
@ -170,6 +169,8 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs FluentIterable from iterable.
|
||||
*
|
||||
* @return a FluentIterable from a given iterable. Calls the SimpleFluentIterable constructor.
|
||||
*/
|
||||
public static <E> FluentIterable<E> from(Iterable<E> iterable) {
|
||||
@ -198,6 +199,8 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the count of remaining objects of current iterable.
|
||||
*
|
||||
* @return the count of remaining objects of the current Iterable
|
||||
*/
|
||||
public final int getRemainingElementsCount() {
|
||||
@ -212,7 +215,7 @@ public class SimpleFluentIterable<E> implements FluentIterable<E> {
|
||||
|
||||
/**
|
||||
* Collects the remaining objects of the given iterator into a List.
|
||||
*
|
||||
*
|
||||
* @return a new List with the remaining objects.
|
||||
*/
|
||||
public static <E> List<E> toList(Iterator<E> iterator) {
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.flux.action;
|
||||
|
||||
/**
|
||||
*
|
||||
* Action is the data payload dispatched to the stores when something happens.
|
||||
*
|
||||
*/
|
||||
public abstract class Action {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.flux.action;
|
||||
|
||||
/**
|
||||
*
|
||||
* Types of actions.
|
||||
*
|
||||
*/
|
||||
public enum ActionType {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.flux.action;
|
||||
|
||||
/**
|
||||
*
|
||||
* Content items.
|
||||
*
|
||||
*/
|
||||
public enum Content {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.flux.action;
|
||||
|
||||
/**
|
||||
*
|
||||
* ContentAction is a concrete action.
|
||||
*
|
||||
*/
|
||||
public class ContentAction extends Action {
|
||||
|
||||
|
@ -25,9 +25,7 @@ package com.iluwatar.flux.action;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* MenuAction is a concrete action.
|
||||
*
|
||||
*/
|
||||
public class MenuAction extends Action {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.flux.action;
|
||||
|
||||
/**
|
||||
*
|
||||
* Menu items.
|
||||
*
|
||||
*/
|
||||
public enum MenuItem {
|
||||
|
||||
|
@ -31,26 +31,24 @@ import com.iluwatar.flux.view.ContentView;
|
||||
import com.iluwatar.flux.view.MenuView;
|
||||
|
||||
/**
|
||||
*
|
||||
* Flux is the application architecture that Facebook uses for building client-side web
|
||||
* applications. Flux eschews MVC in favor of a unidirectional data flow. When a user interacts with
|
||||
* a React view, the view propagates an action through a central dispatcher, to the various stores
|
||||
* that hold the application's data and business logic, which updates all of the views that are
|
||||
* affected.
|
||||
* <p>
|
||||
* This example has two views: menu and content. They represent typical main menu and content area
|
||||
* of a web page. When menu item is clicked it triggers events through the dispatcher. The events
|
||||
* are received and handled by the stores updating their data as needed. The stores then notify the
|
||||
* views that they should rerender themselves.
|
||||
* <p>
|
||||
* http://facebook.github.io/flux/docs/overview.html
|
||||
*
|
||||
* <p>This example has two views: menu and content. They represent typical main menu and content
|
||||
* area of a web page. When menu item is clicked it triggers events through the dispatcher. The
|
||||
* events are received and handled by the stores updating their data as needed. The stores then
|
||||
* notify the views that they should rerender themselves.
|
||||
*
|
||||
* <p>http://facebook.github.io/flux/docs/overview.html
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -23,20 +23,17 @@
|
||||
|
||||
package com.iluwatar.flux.dispatcher;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.iluwatar.flux.action.Action;
|
||||
import com.iluwatar.flux.action.Content;
|
||||
import com.iluwatar.flux.action.ContentAction;
|
||||
import com.iluwatar.flux.action.MenuAction;
|
||||
import com.iluwatar.flux.action.MenuItem;
|
||||
import com.iluwatar.flux.store.Store;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Dispatcher sends Actions to registered Stores.
|
||||
*
|
||||
*/
|
||||
public final class Dispatcher {
|
||||
|
||||
@ -44,7 +41,8 @@ public final class Dispatcher {
|
||||
|
||||
private List<Store> stores = new LinkedList<>();
|
||||
|
||||
private Dispatcher() {}
|
||||
private Dispatcher() {
|
||||
}
|
||||
|
||||
public static Dispatcher getInstance() {
|
||||
return instance;
|
||||
@ -55,7 +53,7 @@ public final class Dispatcher {
|
||||
}
|
||||
|
||||
/**
|
||||
* Menu item selected handler
|
||||
* Menu item selected handler.
|
||||
*/
|
||||
public void menuItemSelected(MenuItem menuItem) {
|
||||
dispatchAction(new MenuAction(menuItem));
|
||||
|
@ -29,9 +29,7 @@ import com.iluwatar.flux.action.Content;
|
||||
import com.iluwatar.flux.action.ContentAction;
|
||||
|
||||
/**
|
||||
*
|
||||
* ContentStore is a concrete store.
|
||||
*
|
||||
*/
|
||||
public class ContentStore extends Store {
|
||||
|
||||
|
@ -29,9 +29,7 @@ import com.iluwatar.flux.action.MenuAction;
|
||||
import com.iluwatar.flux.action.MenuItem;
|
||||
|
||||
/**
|
||||
*
|
||||
* MenuStore is a concrete store.
|
||||
*
|
||||
*/
|
||||
public class MenuStore extends Store {
|
||||
|
||||
|
@ -23,16 +23,13 @@
|
||||
|
||||
package com.iluwatar.flux.store;
|
||||
|
||||
import com.iluwatar.flux.action.Action;
|
||||
import com.iluwatar.flux.view.View;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.iluwatar.flux.action.Action;
|
||||
import com.iluwatar.flux.view.View;
|
||||
|
||||
/**
|
||||
*
|
||||
* Store is a data model.
|
||||
*
|
||||
*/
|
||||
public abstract class Store {
|
||||
|
||||
|
@ -30,9 +30,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* ContentView is a concrete view.
|
||||
*
|
||||
*/
|
||||
public class ContentView implements View {
|
||||
|
||||
|
@ -31,9 +31,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* MenuView is a concrete view.
|
||||
*
|
||||
*/
|
||||
public class MenuView implements View {
|
||||
|
||||
|
@ -26,9 +26,7 @@ package com.iluwatar.flux.view;
|
||||
import com.iluwatar.flux.store.Store;
|
||||
|
||||
/**
|
||||
*
|
||||
* Views define the representation of data.
|
||||
*
|
||||
*/
|
||||
public interface View {
|
||||
|
||||
|
@ -23,16 +23,13 @@
|
||||
|
||||
package com.iluwatar.flyweight;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* AlchemistShop holds potions on its shelves. It uses PotionFactory to provide the potions.
|
||||
*
|
||||
*/
|
||||
public class AlchemistShop {
|
||||
|
||||
@ -42,31 +39,31 @@ public class AlchemistShop {
|
||||
private List<Potion> bottomShelf;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public AlchemistShop() {
|
||||
PotionFactory factory = new PotionFactory();
|
||||
topShelf = List.of(
|
||||
factory.createPotion(PotionType.INVISIBILITY),
|
||||
factory.createPotion(PotionType.INVISIBILITY),
|
||||
factory.createPotion(PotionType.STRENGTH),
|
||||
factory.createPotion(PotionType.HEALING),
|
||||
factory.createPotion(PotionType.INVISIBILITY),
|
||||
factory.createPotion(PotionType.STRENGTH),
|
||||
factory.createPotion(PotionType.HEALING),
|
||||
factory.createPotion(PotionType.HEALING)
|
||||
factory.createPotion(PotionType.INVISIBILITY),
|
||||
factory.createPotion(PotionType.INVISIBILITY),
|
||||
factory.createPotion(PotionType.STRENGTH),
|
||||
factory.createPotion(PotionType.HEALING),
|
||||
factory.createPotion(PotionType.INVISIBILITY),
|
||||
factory.createPotion(PotionType.STRENGTH),
|
||||
factory.createPotion(PotionType.HEALING),
|
||||
factory.createPotion(PotionType.HEALING)
|
||||
);
|
||||
bottomShelf = List.of(
|
||||
factory.createPotion(PotionType.POISON),
|
||||
factory.createPotion(PotionType.POISON),
|
||||
factory.createPotion(PotionType.POISON),
|
||||
factory.createPotion(PotionType.HOLY_WATER),
|
||||
factory.createPotion(PotionType.HOLY_WATER)
|
||||
factory.createPotion(PotionType.POISON),
|
||||
factory.createPotion(PotionType.POISON),
|
||||
factory.createPotion(PotionType.POISON),
|
||||
factory.createPotion(PotionType.HOLY_WATER),
|
||||
factory.createPotion(PotionType.HOLY_WATER)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a read-only list of all the items on the top shelf
|
||||
* Get a read-only list of all the items on the top shelf.
|
||||
*
|
||||
* @return The top shelf potions
|
||||
*/
|
||||
@ -75,7 +72,7 @@ public class AlchemistShop {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a read-only list of all the items on the bottom shelf
|
||||
* Get a read-only list of all the items on the bottom shelf.
|
||||
*
|
||||
* @return The bottom shelf potions
|
||||
*/
|
||||
@ -84,7 +81,7 @@ public class AlchemistShop {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerate potions
|
||||
* Enumerate potions.
|
||||
*/
|
||||
public void enumerate() {
|
||||
|
||||
|
@ -24,24 +24,22 @@
|
||||
package com.iluwatar.flyweight;
|
||||
|
||||
/**
|
||||
*
|
||||
* Flyweight pattern is useful when the program needs a huge amount of objects. It provides means to
|
||||
* decrease resource usage by sharing object instances.
|
||||
* <p>
|
||||
* In this example {@link AlchemistShop} has great amount of potions on its shelves. To fill the
|
||||
*
|
||||
* <p>In this example {@link AlchemistShop} has great amount of potions on its shelves. To fill the
|
||||
* shelves {@link AlchemistShop} uses {@link PotionFactory} (which represents the Flyweight in this
|
||||
* example). Internally {@link PotionFactory} holds a map of the potions and lazily creates new ones
|
||||
* when requested.
|
||||
* <p>
|
||||
* To enable safe sharing, between clients and threads, Flyweight objects must be immutable.
|
||||
*
|
||||
* <p>To enable safe sharing, between clients and threads, Flyweight objects must be immutable.
|
||||
* Flyweight objects are by definition value objects.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* HealingPotion
|
||||
*
|
||||
* HealingPotion.
|
||||
*/
|
||||
public class HealingPotion implements Potion {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* HolyWaterPotion
|
||||
*
|
||||
* HolyWaterPotion.
|
||||
*/
|
||||
public class HolyWaterPotion implements Potion {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* InvisibilityPotion
|
||||
*
|
||||
* InvisibilityPotion.
|
||||
*/
|
||||
public class InvisibilityPotion implements Potion {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* PoisonPotion
|
||||
*
|
||||
* PoisonPotion.
|
||||
*/
|
||||
public class PoisonPotion implements Potion {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.flyweight;
|
||||
|
||||
/**
|
||||
*
|
||||
* Interface for Potions.
|
||||
*
|
||||
*/
|
||||
public interface Potion {
|
||||
|
||||
|
@ -27,11 +27,9 @@ import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* PotionFactory is the Flyweight in this example. It minimizes memory use by sharing object
|
||||
* instances. It holds a map of potion instances and new potions are created only when none of the
|
||||
* type already exists.
|
||||
*
|
||||
*/
|
||||
public class PotionFactory {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.flyweight;
|
||||
|
||||
/**
|
||||
*
|
||||
* Enumeration for potion types.
|
||||
*
|
||||
*/
|
||||
public enum PotionType {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* StrengthPotion
|
||||
*
|
||||
* StrengthPotion.
|
||||
*/
|
||||
public class StrengthPotion implements Potion {
|
||||
|
||||
|
@ -24,30 +24,27 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Front Controller is a presentation tier pattern. Essentially it defines a controller that
|
||||
* handles all requests for a web site.
|
||||
* <p>
|
||||
* The Front Controller pattern consolidates request handling through a single handler object (
|
||||
*
|
||||
* <p>The Front Controller pattern consolidates request handling through a single handler object (
|
||||
* {@link FrontController}). This object can carry out the common the behavior such as
|
||||
* authorization, request logging and routing requests to corresponding views.
|
||||
* <p>
|
||||
* Typically the requests are mapped to command objects ({@link Command}) which then display the
|
||||
* correct view ({@link View}).
|
||||
* <p>
|
||||
* In this example we have implemented two views: {@link ArcherView} and {@link CatapultView}. These
|
||||
* are displayed by sending correct request to the {@link FrontController} object. For example, the
|
||||
* {@link ArcherView} gets displayed when {@link FrontController} receives request "Archer". When
|
||||
* the request is unknown, we display the error view ({@link ErrorView}).
|
||||
*
|
||||
* <p>Typically the requests are mapped to command objects ({@link Command}) which then display the
|
||||
* correct view ({@link View}).
|
||||
*
|
||||
* <p>In this example we have implemented two views: {@link ArcherView} and {@link CatapultView}.
|
||||
* These are displayed by sending correct request to the {@link FrontController} object. For
|
||||
* example, the {@link ArcherView} gets displayed when {@link FrontController} receives request
|
||||
* "Archer". When the request is unknown, we display the error view ({@link ErrorView}).
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args
|
||||
* command line args
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
FrontController controller = new FrontController();
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Custom exception type
|
||||
*
|
||||
* Custom exception type.
|
||||
*/
|
||||
public class ApplicationException extends RuntimeException {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Command for archers.
|
||||
*
|
||||
*/
|
||||
public class ArcherCommand implements Command {
|
||||
|
||||
|
@ -27,12 +27,10 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* View for archers.
|
||||
*
|
||||
*/
|
||||
public class ArcherView implements View {
|
||||
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ArcherView.class);
|
||||
|
||||
@Override
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Command for catapults.
|
||||
*
|
||||
*/
|
||||
public class CatapultCommand implements Command {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* View for catapults.
|
||||
*
|
||||
*/
|
||||
public class CatapultView implements View {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Commands are the intermediary between requests and views.
|
||||
*
|
||||
*/
|
||||
public interface Command {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* View for errors.
|
||||
*
|
||||
*/
|
||||
public class ErrorView implements View {
|
||||
|
||||
|
@ -24,10 +24,8 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* FrontController is the handler class that takes in all the requests and renders the correct
|
||||
* response.
|
||||
*
|
||||
*/
|
||||
public class FrontController {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Default command in case the mapping is not successful.
|
||||
*
|
||||
*/
|
||||
public class UnknownCommand implements Command {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Views are the representations rendered for the user.
|
||||
*
|
||||
*/
|
||||
public interface View {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user