Skip to content

Commit

Permalink
Resolves checkstyle errors for delegation dependency-injection dirty-…
Browse files Browse the repository at this point in the history
…flag double-buffer double-checked-locking double-dispatch (#1068)

* Reduces checkstyle errors in delegation

* Reduces checkstyle errors in dependency-injection

* Reduces checkstyle errors in dirty-flag

* Reduces checkstyle errors in double-buffer

* Reduces checkstyle errors in double-checked-locking

* Reduces checkstyle errors in double-dispatch
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Nov 10, 2019
1 parent 01e489c commit f2c91eb
Show file tree
Hide file tree
Showing 35 changed files with 154 additions and 215 deletions.
22 changes: 12 additions & 10 deletions delegation/src/main/java/com/iluwatar/delegation/simple/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@
import com.iluwatar.delegation.simple.printers.HpPrinter;

/**
* The delegate pattern provides a mechanism to abstract away the implementation and control of the desired action.
* The class being called in this case {@link PrinterController} is not responsible for the actual desired action,
* but is actually delegated to a helper class either {@link CanonPrinter}, {@link EpsonPrinter} or {@link HpPrinter}.
* The consumer does not have or require knowledge of the actual class carrying out the action, only the
* container on which they are calling.
* The delegate pattern provides a mechanism to abstract away the implementation and control of the
* desired action. The class being called in this case {@link PrinterController} is not responsible
* for the actual desired action, but is actually delegated to a helper class either {@link
* CanonPrinter}, {@link EpsonPrinter} or {@link HpPrinter}. The consumer does not have or require
* knowledge of the actual class carrying out the action, only the container on which they are
* calling.
*
* In this example the delegates are {@link EpsonPrinter}, {@link HpPrinter} and {@link CanonPrinter} they all implement
* {@link Printer}. The {@link PrinterController} class also implements {@link Printer}. However neither provide the
* functionality of {@link Printer} by printing to the screen, they actually call upon the instance of {@link Printer}
* that they were instantiated with. Therefore delegating the behaviour to another class.
* <p>In this example the delegates are {@link EpsonPrinter}, {@link HpPrinter} and {@link
* CanonPrinter} they all implement {@link Printer}. The {@link PrinterController} class also
* implements {@link Printer}. However neither provide the functionality of {@link Printer} by
* printing to the screen, they actually call upon the instance of {@link Printer} that they were
* instantiated with. Therefore delegating the behaviour to another class.
*/
public class App {

private static final String MESSAGE_TO_PRINT = "hello world";

/**
* Program entry point
* Program entry point.
*
* @param args command line args
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public interface Printer {

/**
* Method that takes a String to print to the screen. This will be implemented on both the
* controller and the delegate allowing the controller to call the same method on the delegate class.
* controller and the delegate allowing the controller to call the same method on the delegate
* class.
*
* @param message to be printed to the screen
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
package com.iluwatar.delegation.simple;

/**
* Delegator Class to delegate the implementation of the Printer.
* This ensures two things:
* - when the actual implementation of the Printer class changes the delegation will still be operational
* - the actual benefit is observed when there are more than one implementors and they share a delegation control
* Delegator Class to delegate the implementation of the Printer. This ensures two things: - when
* the actual implementation of the Printer class changes the delegation will still be operational -
* the actual benefit is observed when there are more than one implementors and they share a
* delegation control
*/
public class PrinterController implements Printer {

Expand All @@ -38,10 +38,10 @@ public PrinterController(Printer printer) {
}

/**
* This method is implemented from {@link Printer} however instead on providing an
* implementation, it instead calls upon the class passed through the constructor. This is the delegate,
* hence the pattern. Therefore meaning that the caller does not care of the implementing class only the owning
* controller.
* This method is implemented from {@link Printer} however instead on providing an implementation,
* it instead calls upon the class passed through the constructor. This is the delegate, hence the
* pattern. Therefore meaning that the caller does not care of the implementing class only the
* owning controller.
*
* @param message to be printed to the screen
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.slf4j.LoggerFactory;

/**
* Specialised Implementation of {@link Printer} for a Canon Printer, in
* this case the message to be printed is appended to "Canon Printer : "
* Specialised Implementation of {@link Printer} for a Canon Printer, in this case the message to be
* printed is appended to "Canon Printer : ".
*
* @see Printer
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.slf4j.LoggerFactory;

/**
* Specialised Implementation of {@link Printer} for a Epson Printer, in
* this case the message to be printed is appended to "Epson Printer : "
* Specialised Implementation of {@link Printer} for a Epson Printer, in this case the message to be
* printed is appended to "Epson Printer : ".
*
* @see Printer
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.slf4j.LoggerFactory;

/**
* Specialised Implementation of {@link Printer} for a HP Printer, in
* this case the message to be printed is appended to "HP Printer : "
* Specialised Implementation of {@link Printer} for a HP Printer, in this case the message to be
* printed is appended to "HP Printer : ".
*
* @see Printer
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,6 @@

package com.iluwatar.dependency.injection;

/**
* The MIT License
* Copyright (c) 2014-2017 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


/**
* AdvancedSorceress implements inversion of control. It depends on abstraction that can be injected
* through its setter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
package com.iluwatar.dependency.injection;

/**
*
* AdvancedWizard implements inversion of control. It depends on abstraction that can be injected
* through its constructor.
*
*/
public class AdvancedWizard implements Wizard {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,26 @@
* implements so called inversion of control principle. Inversion of control has two specific rules:
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
* - Abstractions should not depend on details. Details should depend on abstractions.
* <p>
* In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a
* naive implementation violating the inversion of control principle. It depends directly on a
*
* <p>In this example we show you three different wizards. The first one ({@link SimpleWizard}) is
* a naive implementation violating the inversion of control principle. It depends directly on a
* concrete implementation which cannot be changed.
* <p>
* The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more flexible.
* They do not depend on any concrete implementation but abstraction. They utilizes Dependency Injection
* pattern allowing their {@link Tobacco} dependency to be injected through constructor ({@link AdvancedWizard})
* or setter ({@link AdvancedSorceress}). This way, handling the dependency is no longer the wizard's
* responsibility. It is resolved outside the wizard class.
* <p>
* The fourth example takes the pattern a step further. It uses Guice framework for Dependency
*
* <p>The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more
* flexible. They do not depend on any concrete implementation but abstraction. They utilizes
* Dependency Injection pattern allowing their {@link Tobacco} dependency to be injected through
* constructor ({@link AdvancedWizard}) or setter ({@link AdvancedSorceress}). This way, handling
* the dependency is no longer the wizard's responsibility. It is resolved outside the wizard
* class.
*
* <p>The fourth example takes the pattern a step further. It uses Guice framework for Dependency
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
* used to create {@link GuiceWizard} object with correct dependencies.
*/
public class App {

/**
* Program entry point
* Program entry point.
*
* @param args command line args
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
import javax.inject.Inject;

/**
*
* GuiceWizard implements inversion of control. Its dependencies are injected through its
* constructor by Guice framework.
*
*/
public class GuiceWizard implements Wizard {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.dependency.injection;

/**
*
* OldTobyTobacco concrete {@link Tobacco} implementation
*
* OldTobyTobacco concrete {@link Tobacco} implementation.
*/
public class OldTobyTobacco extends Tobacco {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.dependency.injection;

/**
*
* RivendellTobacco concrete {@link Tobacco} implementation
*
* RivendellTobacco concrete {@link Tobacco} implementation.
*/
public class RivendellTobacco extends Tobacco {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.dependency.injection;

/**
*
* SecondBreakfastTobacco concrete {@link Tobacco} implementation
*
* SecondBreakfastTobacco concrete {@link Tobacco} implementation.
*/
public class SecondBreakfastTobacco extends Tobacco {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
package com.iluwatar.dependency.injection;

/**
*
* Naive Wizard implementation violating the inversion of control principle. It should depend on
* abstraction instead.
*
*/
public class SimpleWizard implements Wizard {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* Tobacco abstraction
*
* Tobacco abstraction.
*/
public abstract class Tobacco {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import com.google.inject.AbstractModule;

/**
*
* Guice module for binding certain concrete {@link Tobacco} implementation.
*
*/
public class TobaccoModule extends AbstractModule {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.dependency.injection;

/**
*
* Wizard interface
*
* Wizard interface.
*/
public interface Wizard {

Expand Down
51 changes: 27 additions & 24 deletions dirty-flag/src/main/java/com/iluwatar/dirtyflag/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,49 @@

package com.iluwatar.dirtyflag;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This application demonstrates the <b>Dirty Flag</b> pattern. The dirty flag behavioral pattern
* allows you to avoid expensive operations that would just need to be done again anyway. This is a
* simple pattern that really just explains how to add a bool value to your class that you can set
* anytime a property changes. This will let your class know that any results it may have previously
* calculated will need to be calculated again when they’re requested. Once the results are
* re-calculated, then the bool value can be cleared.
*
* This application demonstrates the <b>Dirty Flag</b> pattern. The dirty flag behavioral pattern allows you to avoid
* expensive operations that would just need to be done again anyway. This is a simple pattern that really just explains
* how to add a bool value to your class that you can set anytime a property changes. This will let your class know that
* any results it may have previously calculated will need to be calculated again when they’re requested. Once the
* results are re-calculated, then the bool value can be cleared.
*
* There are some points that need to be considered before diving into using this pattern:- there are some things you’ll
* need to consider:- (1) Do you need it? This design pattern works well when the results to be calculated are difficult
* or resource intensive to compute. You want to save them. You also don’t want to be calculating them several times in
* a row when only the last one counts. (2) When do you set the dirty flag? Make sure that you set the dirty flag within
* the class itself whenever an important property changes. This property should affect the result of the calculated
* result and by changing the property, that makes the last result invalid. (3) When do you clear the dirty flag? It
* might seem obvious that the dirty flag should be cleared whenever the result is calculated with up-to-date
* information but there are other times when you might want to clear the flag.
* <p>There are some points that need to be considered before diving into using this pattern:-
* there are some things you’ll need to consider:- (1) Do you need it? This design pattern works
* well when the results to be calculated are difficult or resource intensive to compute. You want
* to save them. You also don’t want to be calculating them several times in a row when only the
* last one counts. (2) When do you set the dirty flag? Make sure that you set the dirty flag within
* the class itself whenever an important property changes. This property should affect the result
* of the calculated result and by changing the property, that makes the last result invalid. (3)
* When do you clear the dirty flag? It might seem obvious that the dirty flag should be cleared
* whenever the result is calculated with up-to-date information but there are other times when you
* might want to clear the flag.
*
* In this example, the {@link DataFetcher} holds the <i>dirty flag</i>. It fetches and re-fetches from <i>world.txt</i>
* when needed. {@link World} mainly serves the data to the front-end.
* <p>In this example, the {@link DataFetcher} holds the <i>dirty flag</i>. It fetches and
* re-fetches from <i>world.txt</i> when needed. {@link World} mainly serves the data to the
* front-end.
*/
public class App {

private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

/**
* Program execution point
* Program execution point.
*/
public void run() {

final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(new Runnable() {
final World world = new World();

@Override
public void run() {
List<String> countries = world.fetch();
Expand All @@ -74,10 +78,9 @@ public void run() {
}

/**
* Program entry point
* Program entry point.
*
* @param args
* command line args
* @param args command line args
*/
public static void main(String[] args) {
App app = new App();
Expand Down
11 changes: 4 additions & 7 deletions dirty-flag/src/main/java/com/iluwatar/dirtyflag/DataFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@

package com.iluwatar.dirtyflag;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.crypto.Data;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A mock database manager -- Fetches data from a raw file.
*
* @author swaisuan
*
* @author swaisuan
*/
public class DataFetcher {

Expand All @@ -61,7 +58,7 @@ private boolean isDirty(long fileLastModified) {

/**
* Fetches data/content from raw file.
*
*
* @return List of strings
*/
public List<String> fetch() {
Expand Down

0 comments on commit f2c91eb

Please sign in to comment.