DESIGN PATTERNS SAGA #5: REAL PROJECT SITUATIONS WITH ABSTRACT FACTORY

The problem: Sometimes we need to create complex objects based on abstractions and types. What can we do to deal with this problem? Create lots of “ifs”? No, we can use the Abstract Factory Pattern! Basically, with this Pattern we can create an abstraction of the abstraction. Although this Pattern is not commonly used on enterprise projects, it’s very important to know. One of the cons of this Pattern is that it is complex.

The diagram:

abstract_factory_diagram.PNG

The source code:
https://github.com/rafadelnero/design-patterns-saga.git

1 – The VirtualCoinFactory: Everything begins here in the VirtualCoinFactory class.

The getVirtualCoinFactory(double moneyToInvest) method controls what factory will be returned based on the amount of moneyToInvest. If it is 2000 or more, it will return either the BitcoinFactory or EtheriumFactory. You can see that it is a Factory of another Factory. This is the top class of the Factories.

The abstract getVirtualCoin(VirtualCoinType virtualCoinType) method must be implemented by the other Factory subclass, then we can get a specific class from any type of VirtualCoin.

 public abstract class VirtualCoinFactory {

	public static double BIT_COIN_VALUE = 2000;

	public static VirtualCoinFactory
             getVirtualCoinFactory(double moneyToInvest) {

		if(moneyToInvest >= BIT_COIN_VALUE) {
			return new BitcoinFactory();
		}
		else {
			return new EtheriumFactory();
		}
	}

	public abstract VirtualCoin getVirtualCoin
                        (VirtualCoinType virtualCoinType);

	public abstract Validator getValidator
                   (VirtualCoinType virtualCoinType);
}

2 – The BitcoinFactory: This is the class that extends the top class VirtualCoinFactory.

As a consequence, the getVirtualCoin(VirtualCoinType virtualCoinType) method has to be implemented returning the Bitcoin based on the type, Integer or Fractioned.

To do this process we have to use the polymorphism concept and return a generic type. We have to create this class:

public class VirtualCoin {

	protected double volume;

	protected int buyOrderAmount;
	protected int sellOrderAmount;

	// Getters and setters omitted
}

In the Bitcoin classes, we have to extend VirtualCoin.

public class IntegerBitcoin extends VirtualCoin {

}

public class FractionedBitcoin extends VirtualCoin {

}

The getValidator(VirtualCoinType virtualCoinType) method is used to validate something in the Bitcoin. You can add any method you find suitable for your business requirements here. We also use polymorphism here.

public class BitcoinFactory extends VirtualCoinFactory {

	@Override
	public VirtualCoin getVirtualCoin(VirtualCoinType virtualCoinType) {
		switch(virtualCoinType) {

		case FRACTIONED:
			return new FractionedBitcoin();

		case INTEGER:
			return new IntegerBitcoin();
		default:
			throw new
                         IllegalArgumentException("Card type not found.");
		}
	}

	public Validator getValidator(VirtualCoinType virtualCoinType) {
		switch(virtualCoinType) {

		case FRACTIONED:
			return new FractionedBitcoinValidator();

		case INTEGER:
			return new IntegerBitcoinValidator();
		default:
			throw new
                         IllegalArgumentException("Card type not found.");
		}
	}

}

3 – The EtheriumFactory: There is no new concept here, just the flow of Etherium.

public class EtheriumFactory extends VirtualCoinFactory {

	@Override
	public VirtualCoin getVirtualCoin(VirtualCoinType virtualCoinType) {
		switch (virtualCoinType) {
		case FRACTIONED:
			return new FractionedEtherium();
		case INTEGER:
			return new IntegerEtherium();
		}

		throw new IllegalArgumentException
                 (virtualCoinType + " not found");
	}

	@Override
	public Validator getValidator(VirtualCoinType virtualCoinType) {
		return new EtheriumValidator();
	}

}

4 – Unitary Tests: Now we are going to see if the classes created from the Abstract Factory will be correct.

On the checkBitcoinCreationTest() method we get the instance of the VirtualCoin based on the value 2000, returning the BitcoinFactory. Having the BitcoinFactory we can get the IntegerBitcoin or the FractionedBitcoin based on the type we pass.

Then we check if the returned classes are correct. We use the Reflection methods getClass().getSimpleName() to check if the instances are correct. We do the same process with Etherium.

public class AbstractFactoryTest {

	public static final double BITCOIN_VALUE = 2000;
	public static final double ETHERIUM_VALUE = 1000;

	@Test
	public void checkBitcoinCreationTest() {
		VirtualCoinFactory abstractFactory = VirtualCoinFactory.
                       getVirtualCoinFactory(BITCOIN_VALUE);

		VirtualCoin integerBitcoin = abstractFactory.
                   getVirtualCoin(VirtualCoinType.INTEGER);
		VirtualCoin fractionedBitcoin = abstractFactory.
                  getVirtualCoin(VirtualCoinType.FRACTIONED);

		Assert.assertEquals("IntegerBitcoin", integerBitcoin.
                  getClass().getSimpleName());
		Assert.assertEquals("FractionedBitcoin", fractionedBitcoin.
                  getClass().getSimpleName());
	}

	@Test
	public void checkEtheriumCreationTest() {
		VirtualCoinFactory abstractFactory = VirtualCoinFactory.
                 getVirtualCoinFactory(ETHERIUM_VALUE);

		VirtualCoin integerEtherium = abstractFactory.
                 getVirtualCoin(VirtualCoinType.INTEGER);
		VirtualCoin fractionedEtherium = abstractFactory.
                 getVirtualCoin(VirtualCoinType.FRACTIONED);

		Assert.assertEquals("IntegerEtherium", integerEtherium.
                 getClass().getSimpleName());
		Assert.assertEquals("FractionedEtherium", fractionedEtherium.
                 getClass().getSimpleName());
	}

}

Summary of actions:

1 – Created the top Abstract Factory class.
2 – Created the generic method that returns the sub Factory.
3 – Created a generic method on the sub Factory class that returns the final class.

To practice the Abstract Factory Pattern you can create another sub Factory, it can be another VirtualCoin, and make it return the correct object! Try to use TDD (Test Driven Development). Start the development from the test.

DESIGN PATTERNS SAGA #5: REAL PROJECT SITUATIONS WITH ABSTRACT FACTORY

Design Patterns SAGA #4: Real Project Situations With Prototype

The Problem: We could solve the Prototype problem in an easy way, but we would have to write several setters and create a converter that passes all the information from getter to setter. For example:

newContract.setId(contract.getId());
newContract.setName(contract.getName());

We can’t assign the object reference because both variables will be pointing to the same object. If I change one of the references both references will be identical.

contract = newContract;
contract.setName("It's the same object reference!");

Both references will be the same value. The reference variable newContract.getName() will return “It’s the same object reference!”.

What would be a real world situation in which the Prototype Pattern could be used? We can use it for logging, for example. In order to keep the old state and get the new state of the object, we must use the Prototype Pattern. We can’t copy the reference and use the same object. Another situation would be when we must copy information from an entity and save the same information using JPA.

So, how do you prefer doing the object copy? By manual setters? By using Reflection methods? Or using the clone method from Java API? I will show you the possible ways to solve this problem. I prefer the clone() method!

The diagram:

prototype_diagram.PNG

Get the source code:
https://github.com/rafadelnero/design-patterns-saga.git

1 – The Contract POJO: The first thing we have to realize is that this class is implementing Cloneable. The second thing is that we are overriding the clone() method. We are also changing clone() visibility to “public“.

On the clone() method, we reference the super class cloning the object like this:

clonedContract = (Contract) super.clone();

Unfortunately, the interface Cloneable was implemented in Java 1.0 where generics didn’t exist yet. As a consequence, we need to use class casting.

Shallow Copy vs. Deep Copy: On the below example, we are using Deep Copy. This means we are making a copy with all the related objects inside the Contract class. See that we are doing it manually:

clonedContract.contractComplement = (ContractComplement) contractComplement.clone();

We are copying also the ContractComplement class. An example of Shallow Copy is pretty simple. We just have to copy the Contract class.

public class Contract implements Cloneable {

	private long id;

	private String name;

	private ContractComplement contractComplement;

	private BigDecimal price;

	public Contract(long id, String name,
			ContractComplement contractComplement, BigDecimal price) {
		this.id = id;
		this.name = name;
		this.contractComplement = contractComplement;
		this.price = price;
	}

	public Contract() {
		super();
	}

	@Override
	protected Object clone() {
		Contract clonedContract;
		try {
			clonedContract = (Contract) super.clone();
			clonedContract.contractComplement = (ContractComplement) contractComplement.clone();
			return clonedContract;
		} catch (CloneNotSupportedException e) {
			throw new RuntimeException(e);
		}
	}

	// Getters and Setters omitted

}

2 – The ContractComplement class: There is no new concept here. We just implemented the Cloneable interface and overrode the clone() method using the super.clone() method to clone this object.

public class ContractComplement implements Cloneable {

	private String complementName;

	private Integer specificProductPrice;

	public ContractComplement() {
		super();
	}

	public ContractComplement(String complementName, Integer specificProductPrice) {
		this.complementName = complementName;
		this.specificProductPrice = specificProductPrice;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	// Getters and Setters omitted

}

3 – The Unitary Test: Now we are going to check if the objects are really different and if the values are equal.

checkIfTheObjectWasClonedTest() method: Here we are testing the Prototype Pattern with the clone() method from the Java API. We are checking if the object was successfully cloned.

checkIfTheObjectWasCopiedByBeanUtilsTest: This is an alternative way to solve this problem. We can use the utility class BeanUtils from the Spring dependency. We have to add it to the pom.xml:
        org.springframework
        spring-beans
        4.3.9.RELEASE

There are some cons about using this class:

1 – We must create getters and setters. We can’t encapsulate them.
2 – The copy is done by Reflection. It’s very slow.
3 – We must copy the objects manually.

public class PrototypeTest {

	// Constants omitted.

	@Test
	public void checkIfTheObjectWasClonedTest() {
		Contract contract = mockContract();

		Contract contractPrototype = (Contract) contract.clone();

		checkIfTheObjectsAreCloned(contract, contractPrototype);
	}

	@Test
	public void checkIfTheObjectWasCopiedByBeanUtilsTest() throws IllegalAccessException, InvocationTargetException {
		Contract contract = mockContract();

		Contract contractToBeCopied = new Contract();
		ContractComplement complementContractToBeCopied = new ContractComplement();

		BeanUtils.copyProperties(contract, contractToBeCopied);
		BeanUtils.copyProperties(contract.getContractComplement(), complementContractToBeCopied);

		contractToBeCopied.setContractComplement(complementContractToBeCopied);

		checkIfTheObjectsAreCloned(contract, contractToBeCopied);
	}

	private Contract mockContract() {
		ContractComplement complement = new ContractComplement(COMPLEMENT_NAME, QTD_CUSTOMERS);

		Contract contract = new Contract(ID_CONTRACT, CONTRACT_NAME, complement, CONTRACT_VALUE);
		return contract;
	}

	private void checkIfTheObjectsAreCloned(Contract contract, Contract contractToBeCopied) {
		Assert.assertTrue(contract.getId() == contractToBeCopied.getId());
		Assert.assertTrue(contract.getName().equals(contractToBeCopied.getName()));
		Assert.assertTrue(contract.getPrice().equals(contractToBeCopied.getPrice()));

		ContractComplement complement = contract.getContractComplement();
		ContractComplement copiedComplement = contractToBeCopied.getContractComplement();

		Assert.assertTrue(complement.getComplementName().equals(copiedComplement.getComplementName()));
		Assert.assertTrue(complement.getSpecificProductPrice().equals(copiedComplement.getSpecificProductPrice()));

		Assert.assertFalse(contract.equals(contractToBeCopied));
		Assert.assertFalse(contract.getContractComplement().equals(contractToBeCopied.getContractComplement()));
	}

}

Summary of actions:

1 – Implemented the Cloneable interface.
2 – Overrode the clone() method.
3 – Changed the visibility of the clone() method to public.
4 – Invoked the super.clone() method.
5 – Made a copy of ContractComplement in the clone() method explicitly.

To practice the Prototype Pattern you can create another POJO, implement the Cloneable interface and make the Deep Copy! Try to use TDD (Test Driven Development). Start the development from the test.

Design Patterns SAGA #4: Real Project Situations With Prototype

Java Challenge #9: Tokenizer

It is so important to know that Tokenizer exists and to know what tools you have available to make your code powerful!

Try to solve this challenge before seeing the answer below.

tokenizer.jpg

Answer: This is an easy one. We created the object Scanner passing the String to be tokenized, separated by a pattern. At first, you’ve got to know what the symbols on the tokens mean.

This symbol is called Regex. Many languages use the same concept:
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

In simple words, this symbol “[^\\w*]” means “not a word” so it will tokenize by the comma “,“.

The result is:
ThisIsIt
theFinalString
NoBugsProject

Java Challenge #9: Tokenizer

Java Challenge #8: Enums Implementing Interfaces

Enums, do you really master them? Can you tell me if it’s going to compile? If not, explain why. If so, write the result.

Try to solve this challenge before seeing the answer below.

enumchallenge.jpg

Answer: The first thing to observe is the implementation of the interface Invoker on the Enum. It’s not very common to do this, but it’s possible. When we access the Enum reference of the reference it does not make a difference. The invokeShenLong() method will be invoked in the same way bringing all the Enums declared on DragonBalls. When we enter in the loop, we are not using brackets ({}) on the “if” command. This means that only one line will be executed.

Conclusion: Until the Enums are equaled, the looping will continue. In the end, the iterated Enums will be FIFTH and equal to one another.

The output will be:
Same dragon ball
FIFTH

Java Challenge #8: Enums Implementing Interfaces

JAVA CHALLENGE #7: Interfaces and Abstract classes

Who is stronger? Master Yoda or Darth Vader? What will the output be? Solve this challenge and find out!

Before seeing the answer below, try it out!

starwarschallenge.jpg

Answer: At first we create an anonymous inner class of Darth Vader, that means any class that implements DarthVader interface. We have only one getNumber() method.

With MasterYoda, things are different. We created an abstract class with the getNumber() method and another one in the inner class of MasterYoda. This is the same as DarthVader, it is an anonymous inner class. This challenge is pretty tricky because you can see that there is a method overloading both methods. Which one will be invoked? See that we are passing a new Integer(FORCE) for the getNumber() method. For this reason, the method from the abstract class will be invoked and MasterYoda will have the force of 20.

The answer is:
30
The stronger Jedi is: Master Yoda

May the force be with you!

JAVA CHALLENGE #7: Interfaces and Abstract classes

Java Challenge #6: Thread Start

Threads! We commonly don’t deal with them directly on day-to-day work. What will the wolverineAdrenaline be?

Try to solve this challenge before seeing the answer below.

wolverineChallenge.jpg

Answer: Remember, Threads execution is always uncertain and depends on the JVM. The order of execution is always random.

Conclusion: There is no exact result. The Threads will be executed randomly. wolverineAdrenaline could be 10, 11, 12, 13 or 14.

Java Challenge #6: Thread Start

Java Challenge #5: Logical and Bitwise Operators

Have you mastered logical operators?

Try to sum these numbers before seeing the answer below.

logical_operators.PNG

Answer: There are some important concepts here. The first concept is the bitwise operators. They will be checked wherever the result is true or false. For example, when we use logical operators like:

/* The second condition won't be executed,
because it's not necessary, once you are
using "&&", both conditions must be "true"
when one of them is false, JVM just ignores the rest. */
if (false && true) {

}

When we use bitwise operators:

/* The second condition will be checked,
even if the first condition is false,
 this can be useful when you want to
execute a command in every situation on your conditions. */
if (false & true) {

}

When we use the command “||” the same rule is applied here. See the example:

/* The second condition won't be
executed because once we are using "||",
if one of the conditions is "true" the
whole condition will be true, so JVM won't
check the next condition. */
if (true || false) {

}

/* When using the bitwise operator,
both conditions will be check,
even if the first condition is "true" */
if (true | false) {

}

We must know the incremental operator. When you use it before the variable it will increment in the line you used it. When you use the post-incremental operator it will increment the next time you use the variable. For example:

/* In the first condition, the variable
"i" will be 5 and on the other will be 6,
so both conditions will be true */
int i = 5;
if (i++ == 5 || i++ == 6) {}

/* In the first condition the variable
will be 6 and in the next will be 7,
so both conditions will be false */
int i = 5;
if (++i == 5 && ++i == 6) {}

Knowing all these concepts, now I can give the answer, it is 74! Try this challenge, debug it, and see the concepts in a practical way. This is how you master a programming language!

Java Challenge #5: Logical and Bitwise Operators