Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
* tiers. By using the pattern we gain loose coupling between the tiers. The Business Delegate
* encapsulates knowledge about how to locate, connect to, and interact with the business objects
* that make up the application.
*
*
* <p>Some of the services the Business Delegate uses are instantiated directly, and some can be
* retrieved through service lookups. The Business Delegate itself may contain business logic too
* potentially tying together multiple service calls, exception handling, retrying etc.
*
*
* <p>In this example the client ({@link Client}) utilizes a business delegate (
* {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate
* service and makes the service call.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package com.iluwatar.business.delegate;

/**
* BusinessDelegate separates the presentation and business tiers
* BusinessDelegate separates the presentation and business tiers.
*/
public class BusinessDelegate {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class BusinessLookup {
private JmsService jmsService;

/**
* Gets service instance based on service type.
*
* @param serviceType Type of service instance to be returned.
* @return Service instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package com.iluwatar.business.delegate;

/**
*
* Interface for service implementations
*
* Interface for service implementations.
*/
public interface BusinessService {

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

/**
*
* Client utilizes BusinessDelegate to call the business tier
*
* Client utilizes BusinessDelegate to call the business tier.
*/
public class Client {

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

/**
*
* Service EJB implementation
*
* Service EJB implementation.
*/
public class EjbService implements BusinessService {

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

/**
*
* Service JMS implementation
*
* Service JMS implementation.
*/
public class JmsService implements BusinessService {

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

/**
*
* Enumeration for service types
*
* Enumeration for service types.
*/
public enum ServiceType {

Expand Down
54 changes: 28 additions & 26 deletions bytecode/src/main/java/com/iluwatar/bytecode/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,59 @@
package com.iluwatar.bytecode;

import com.iluwatar.bytecode.util.InstructionConverterUtil;
import java.util.Stack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The intention of Bytecode pattern is to give behavior the flexibility of data by encoding it as instructions
* for a virtual machine.
* An instruction set defines the low-level operations that can be performed. A series of instructions is encoded as
* a sequence of bytes. A virtual machine executes these instructions one at a time,
* using a stack for intermediate values. By combining instructions, complex high-level behavior can be defined.
*
* This pattern should be used when there is a need to define high number of behaviours and implementation engine
* is not a good choice because
* It is too lowe level
* Iterating on it takes too long due to slow compile times or other tooling issues.
* It has too much trust. If you want to ensure the behavior being defined can’t break the game,
* you need to sandbox it from the rest of the codebase.
* The intention of Bytecode pattern is to give behavior the flexibility of data by encoding it as
* instructions for a virtual machine. An instruction set defines the low-level operations that can
* be performed. A series of instructions is encoded as a sequence of bytes. A virtual machine
* executes these instructions one at a time, using a stack for intermediate values. By combining
* instructions, complex high-level behavior can be defined.
*
* <p>This pattern should be used when there is a need to define high number of behaviours and
* implementation engine is not a good choice because It is too lowe level Iterating on it takes too
* long due to slow compile times or other tooling issues. It has too much trust. If you want to
* ensure the behavior being defined can’t break the game, you need to sandbox it from the rest of
* the codebase.
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

/**
* Main app method
* Main app method.
*
* @param args command line args
*/
public static void main(String[] args) {
VirtualMachine vm = new VirtualMachine();

Wizard wizard = new Wizard();
wizard.setHealth(45);
wizard.setAgility(7);
wizard.setWisdom(11);

VirtualMachine vm = new VirtualMachine();
vm.getWizards()[0] = wizard;

interpretInstruction("LITERAL 0", vm);
interpretInstruction( "LITERAL 0", vm);
interpretInstruction( "GET_HEALTH", vm);
interpretInstruction( "LITERAL 0", vm);
interpretInstruction( "GET_AGILITY", vm);
interpretInstruction( "LITERAL 0", vm);
interpretInstruction( "GET_WISDOM ", vm);
interpretInstruction( "ADD", vm);
interpretInstruction( "LITERAL 2", vm);
interpretInstruction( "DIVIDE", vm);
interpretInstruction( "ADD", vm);
interpretInstruction( "SET_HEALTH", vm);
interpretInstruction("LITERAL 0", vm);
interpretInstruction("GET_HEALTH", vm);
interpretInstruction("LITERAL 0", vm);
interpretInstruction("GET_AGILITY", vm);
interpretInstruction("LITERAL 0", vm);
interpretInstruction("GET_WISDOM ", vm);
interpretInstruction("ADD", vm);
interpretInstruction("LITERAL 2", vm);
interpretInstruction("DIVIDE", vm);
interpretInstruction("ADD", vm);
interpretInstruction("SET_HEALTH", vm);
}

private static void interpretInstruction(String instruction, VirtualMachine vm) {
InstructionConverterUtil converter = new InstructionConverterUtil();
vm.execute(converter.convertToByteCode(instruction));
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "" ) + vm.getStack());
Stack<Integer> stack = vm.getStack();
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "") + stack);
}
}
5 changes: 3 additions & 2 deletions bytecode/src/main/java/com/iluwatar/bytecode/Instruction.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package com.iluwatar.bytecode;

/**
* Representation of instructions understandable by virtual machine
* Representation of instructions understandable by virtual machine.
*/
public enum Instruction {

Expand All @@ -51,7 +51,8 @@ public int getIntValue() {
}

/**
* Converts integer value to Instruction
* Converts integer value to Instruction.
*
* @param value value of instruction
* @return representation of the instruction
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Stack;

/**
* Implementation of virtual machine
* Implementation of virtual machine.
*/
public class VirtualMachine {

Expand All @@ -35,7 +35,7 @@ public class VirtualMachine {
private Wizard[] wizards = new Wizard[2];

/**
* Constructor
* Constructor.
*/
public VirtualMachine() {
for (int i = 0; i < wizards.length; i++) {
Expand All @@ -44,7 +44,8 @@ public VirtualMachine() {
}

/**
* Executes provided bytecode
* Executes provided bytecode.
*
* @param bytecode to execute
*/
public void execute(int[] bytecode) {
Expand Down
3 changes: 2 additions & 1 deletion bytecode/src/main/java/com/iluwatar/bytecode/Wizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import org.slf4j.LoggerFactory;

/**
* This class represent game objects which properties can be changed by instructions interpreted by virtual machine
* This class represent game objects which properties can be changed by instructions interpreted by
* virtual machine.
*/
public class Wizard {
private static final Logger LOGGER = LoggerFactory.getLogger(Wizard.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import com.iluwatar.bytecode.Instruction;

/**
* Utility class used for instruction validation and conversion
* Utility class used for instruction validation and conversion.
*/
public class InstructionConverterUtil {
/**
* Converts instructions represented as String
* Converts instructions represented as String.
*
* @param instructions to convert
* @return array of int representing bytecode
Expand All @@ -48,7 +48,8 @@ public static int[] convertToByteCode(String instructions) {
} else if (isValidInt(splitedInstructions[i])) {
bytecode[i] = Integer.valueOf(splitedInstructions[i]);
} else {
throw new IllegalArgumentException("Invalid instruction or number: " + splitedInstructions[i]);
String errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
throw new IllegalArgumentException(errorMessage);
}
}

Expand Down
22 changes: 10 additions & 12 deletions caching/src/main/java/com/iluwatar/caching/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.slf4j.LoggerFactory;

/**
*
* The Caching pattern describes how to avoid expensive re-acquisition of resources by not releasing
* the resources immediately after their use. The resources retain their identity, are kept in some
* fast-access storage, and are re-used to avoid having to acquire them again. There are four main
Expand All @@ -43,8 +42,8 @@
* should be written back to the backing store (i.e. Database) and help keep both data sources
* synchronized/up-to-date. This pattern can improve performance and also helps to maintain
* consistency between data held in the cache and the data in the underlying data store.
* <p>
* In this example, the user account ({@link UserAccount}) entity is used as the underlying
*
* <p>In this example, the user account ({@link UserAccount}) entity is used as the underlying
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The four
* strategies are individually tested. The testing of the cache is restricted towards saving and
Expand All @@ -60,23 +59,22 @@
* @see CacheStore
* @see LruCache
* @see CachingPolicy
*
*/
public class App {

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


/**
* Program entry point
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
AppManager.initDb(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
// and the App class to avoid Maven compilation errors. Set flag to
// true to run the tests with MongoDB (provided that MongoDB is
// installed and socket connection is open).
// and the App class to avoid Maven compilation errors. Set flag to
// true to run the tests with MongoDB (provided that MongoDB is
// installed and socket connection is open).
AppManager.initCacheCapacity(3);
App app = new App();
app.useReadAndWriteThroughStrategy();
Expand All @@ -86,7 +84,7 @@ public static void main(String[] args) {
}

/**
* Read-through and write-through
* Read-through and write-through.
*/
public void useReadAndWriteThroughStrategy() {
LOGGER.info("# CachingPolicy.THROUGH");
Expand All @@ -101,7 +99,7 @@ public void useReadAndWriteThroughStrategy() {
}

/**
* Read-through and write-around
* Read-through and write-around.
*/
public void useReadThroughAndWriteAroundStrategy() {
LOGGER.info("# CachingPolicy.AROUND");
Expand All @@ -123,7 +121,7 @@ public void useReadThroughAndWriteAroundStrategy() {
}

/**
* Read-through and write-behind
* Read-through and write-behind.
*/
public void useReadThroughAndWriteBehindStrategy() {
LOGGER.info("# CachingPolicy.BEHIND");
Expand All @@ -147,7 +145,7 @@ public void useReadThroughAndWriteBehindStrategy() {
}

/**
* Cache-Aside
* Cache-Aside.
*/
public void useCacheAsideStategy() {
LOGGER.info("# CachingPolicy.ASIDE");
Expand Down
Loading