Removed some clutter

This commit is contained in:
danidemi 2016-11-06 16:30:50 +01:00
parent 9bd83d921c
commit dd1679f7b0
7 changed files with 42 additions and 64 deletions

View File

@ -1,9 +1,26 @@
package com.baeldung.mdc;
public interface BusinessService {
/**
* A fake {@link IBusinessService} simulating an actual one.
*/
public abstract class BusinessService implements IBusinessService {
/** Sample service transferring a given amount of money.
* @return {@code true} when the transfer complete successfully, {@code false} otherwise. */
boolean transfer(long amount);
public boolean transfer(long amount) {
beforeTransfer(amount);
// exchange messages with a remote system to transfer the money
try {
// let's pause randomly to properly simulate an actual system.
Thread.sleep((long) (500 + Math.random() * 500));
} catch (InterruptedException e) {
// should never happen
}
// let's simulate both failing and successful transfers
boolean outcome = Math.random() >= 0.25;
afterTransfer(amount, outcome);
return outcome;
}
abstract protected void beforeTransfer(long amount);
abstract protected void afterTransfer(long amount, boolean outcome);
}

View File

@ -1,19 +0,0 @@
package com.baeldung.mdc;
/**
* A fake {@link BusinessService} simulating an actual one.
*/
public class DefaultBusinessService implements BusinessService {
public boolean transfer(long amount) {
try {
// let's pause randomly to properly simulate an actual system.
Thread.sleep((long) (500 + Math.random()*500));
} catch (InterruptedException e) {
// should never happen
}
// let's simulate both failing and successful transfers
return Math.random() >= 0.25;
}
}

View File

@ -1,29 +0,0 @@
package com.baeldung.mdc;
public abstract class DelegateBusiness {
private BusinessService delegate = null;
public DelegateBusiness(BusinessService delegate) {
super();
this.delegate = delegate;
}
public DelegateBusiness() {
this( new DefaultBusinessService() );
}
public boolean transfer(long amount) {
preTransfer(amount);
boolean outcome = delegate.transfer(amount);
postTransfer(amount, outcome);
return outcome;
}
abstract protected void postTransfer(long amount, boolean outcome);
abstract protected void preTransfer(long amount);
}

View File

@ -0,0 +1,9 @@
package com.baeldung.mdc;
public interface IBusinessService {
/** Sample service transferring a given amount of money.
* @return {@code true} when the transfer complete successfully, {@code false} otherwise. */
boolean transfer(long amount);
}

View File

@ -3,19 +3,19 @@ package com.baeldung.mdc.log4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.baeldung.mdc.DelegateBusiness;
import com.baeldung.mdc.BusinessService;
final class Log4JBusinessService extends DelegateBusiness {
class Log4JBusinessService extends BusinessService {
private static final Logger logger = LogManager.getLogger();
@Override
protected void preTransfer(long amount) {
protected void beforeTransfer(long amount) {
logger.info("Preparing to transfer {}$.");
}
@Override
protected void postTransfer(long amount, boolean outcome) {
protected void afterTransfer(long amount, boolean outcome) {
logger.info("Has transfer of {}$ completed successfully ? {}.");
}

View File

@ -2,19 +2,19 @@ package com.baeldung.mdc.log4j2;
import org.apache.log4j.Logger;
import com.baeldung.mdc.DelegateBusiness;
import com.baeldung.mdc.BusinessService;
final class Log4J2BusinessService extends DelegateBusiness {
final class Log4J2BusinessService extends BusinessService {
private Logger logger = Logger.getLogger(Log4J2BusinessService.class);
@Override
protected void preTransfer(long amount) {
protected void beforeTransfer(long amount) {
logger .info("Preparing to transfer " + amount + "$.");
}
@Override
protected void postTransfer(long amount, boolean outcome) {
protected void afterTransfer(long amount, boolean outcome) {
logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + ".");
}

View File

@ -3,19 +3,19 @@ package com.baeldung.mdc.slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.mdc.DelegateBusiness;
import com.baeldung.mdc.BusinessService;
final class Slf4jBusinessService extends DelegateBusiness {
final class Slf4jBusinessService extends BusinessService {
private static final Logger logger = LoggerFactory.getLogger(Slf4jBusinessService.class);
@Override
protected void preTransfer(long amount) {
protected void beforeTransfer(long amount) {
logger.info("Preparing to transfer " + amount + "$.");
}
@Override
protected void postTransfer(long amount, boolean outcome) {
protected void afterTransfer(long amount, boolean outcome) {
logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + ".");
}