Bael 1496 flyweight (#3598)

* Added flyweight pattern example.

* Refactored VehicleFactory to use computeIfAbsent method.
This commit is contained in:
Donato Rimenti
2018-02-24 08:21:36 +01:00
committed by Grzegorz Piwowarek
parent 0094a87f9a
commit 9371794370
5 changed files with 232 additions and 0 deletions
@@ -0,0 +1,42 @@
package com.baeldung.designpatterns.flyweight;
import java.awt.Color;
import org.junit.Assert;
import org.junit.Test;
/**
* Unit test for {@link VehicleFactory}.
*
* @author Donato Rimenti
*/
public class FlyweightUnitTest {
/**
* Checks that when the {@link VehicleFactory} is asked to provide two
* vehicles of different colors, the objects returned are different.
*/
@Test
public void givenDifferentFlyweightObjects_whenEquals_thenFalse() {
Vehicle blackVehicle = VehicleFactory.createVehicle(Color.BLACK);
Vehicle blueVehicle = VehicleFactory.createVehicle(Color.BLUE);
Assert.assertNotNull("Object returned by the factory is null!", blackVehicle);
Assert.assertNotNull("Object returned by the factory is null!", blueVehicle);
Assert.assertNotEquals("Objects returned by the factory are equals!", blackVehicle, blueVehicle);
}
/**
* Checks that when the {@link VehicleFactory} is asked to provide two
* vehicles of the same colors, the same object is returned twice.
*/
@Test
public void givenSameFlyweightObjects_whenEquals_thenTrue() {
Vehicle blackVehicle = VehicleFactory.createVehicle(Color.BLACK);
Vehicle anotherBlackVehicle = VehicleFactory.createVehicle(Color.BLACK);
Assert.assertNotNull("Object returned by the factory is null!", blackVehicle);
Assert.assertNotNull("Object returned by the factory is null!", anotherBlackVehicle);
Assert.assertEquals("Objects returned by the factory are not equals!", blackVehicle, anotherBlackVehicle);
}
}