Skip to content

Commit 9371794

Browse files
auraspherepivovarit
authored andcommitted
Bael 1496 flyweight (eugenp#3598)
* Added flyweight pattern example. * Refactored VehicleFactory to use computeIfAbsent method.
1 parent 0094a87 commit 9371794

5 files changed

Lines changed: 232 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.designpatterns.flyweight;
2+
3+
import java.awt.Color;
4+
5+
import javax.annotation.concurrent.Immutable;
6+
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
/**
11+
* Represents a car. This class is immutable.
12+
*
13+
* @author Donato Rimenti
14+
*/
15+
@Immutable
16+
public class Car implements Vehicle {
17+
18+
/**
19+
* Logger.
20+
*/
21+
private final static Logger LOG = LoggerFactory.getLogger(Car.class);
22+
23+
/**
24+
* The car's engine.
25+
*/
26+
private Engine engine;
27+
28+
/**
29+
* The car's color.
30+
*/
31+
private Color color;
32+
33+
/**
34+
* Instantiates a new Car.
35+
*
36+
* @param engine
37+
* the {@link #engine}
38+
* @param color
39+
* the {@link #color}
40+
*/
41+
public Car(Engine engine, Color color) {
42+
this.engine = engine;
43+
this.color = color;
44+
45+
// Building a new car is a very expensive operation!
46+
try {
47+
Thread.sleep(2000);
48+
} catch (InterruptedException e) {
49+
LOG.error("Error while creating a new car", e);
50+
}
51+
}
52+
53+
/*
54+
* (non-Javadoc)
55+
*
56+
* @see com.baeldung.designpatterns.flyweight.Vehicle#start()
57+
*/
58+
@Override
59+
public void start() {
60+
LOG.info("Car is starting!");
61+
engine.start();
62+
}
63+
64+
/*
65+
* (non-Javadoc)
66+
*
67+
* @see com.baeldung.designpatterns.flyweight.Vehicle#stop()
68+
*/
69+
@Override
70+
public void stop() {
71+
LOG.info("Car is stopping!");
72+
engine.stop();
73+
}
74+
75+
/*
76+
* (non-Javadoc)
77+
*
78+
* @see com.baeldung.designpatterns.flyweight.Vehicle#getColor()
79+
*/
80+
@Override
81+
public Color getColor() {
82+
return this.color;
83+
}
84+
85+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.designpatterns.flyweight;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
/**
7+
* Engine for a vehicle.
8+
*
9+
* @author Donato Rimenti
10+
*/
11+
public class Engine {
12+
13+
/**
14+
* Logger.
15+
*/
16+
private final static Logger LOG = LoggerFactory.getLogger(Engine.class);
17+
18+
/**
19+
* Starts the engine.
20+
*/
21+
public void start() {
22+
LOG.info("Engine is starting!");
23+
}
24+
25+
/**
26+
* Stops the engine.
27+
*/
28+
public void stop() {
29+
LOG.info("Engine is stopping!");
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.designpatterns.flyweight;
2+
3+
import java.awt.Color;
4+
5+
/**
6+
* Interface for a vehicle.
7+
*
8+
* @author Donato Rimenti
9+
*/
10+
public interface Vehicle {
11+
12+
/**
13+
* Starts the vehicle.
14+
*/
15+
public void start();
16+
17+
/**
18+
* Stops the vehicle.
19+
*/
20+
public void stop();
21+
22+
/**
23+
* Gets the color of the vehicle.
24+
*
25+
* @return the color of the vehicle
26+
*/
27+
public Color getColor();
28+
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.designpatterns.flyweight;
2+
3+
import java.awt.Color;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* Factory which implements the Flyweight pattern to return an existing vehicle
9+
* if present or a new one otherwise.
10+
*
11+
* @author Donato Rimenti
12+
*/
13+
public class VehicleFactory {
14+
15+
/**
16+
* Stores the already created vehicles.
17+
*/
18+
private static Map<Color, Vehicle> vehiclesCache = new HashMap<Color, Vehicle>();
19+
20+
/**
21+
* Private constructor to prevent this class instantiation.
22+
*/
23+
private VehicleFactory() {
24+
}
25+
26+
/**
27+
* Returns a vehicle of the same color passed as argument. If that vehicle
28+
* was already created by this factory, that vehicle is returned, otherwise
29+
* a new one is created and returned.
30+
*
31+
* @param color
32+
* the color of the vehicle to return
33+
* @return a vehicle of the specified color
34+
*/
35+
public static Vehicle createVehicle(Color color) {
36+
// Looks for the requested vehicle into the cache.
37+
// If the vehicle doesn't exist, a new one is created.
38+
Vehicle newVehicle = vehiclesCache.computeIfAbsent(color, newColor -> {
39+
// Creates the new car.
40+
Engine newEngine = new Engine();
41+
return new Car(newEngine, newColor);
42+
});
43+
return newVehicle;
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.designpatterns.flyweight;
2+
3+
import java.awt.Color;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
/**
9+
* Unit test for {@link VehicleFactory}.
10+
*
11+
* @author Donato Rimenti
12+
*/
13+
public class FlyweightUnitTest {
14+
15+
/**
16+
* Checks that when the {@link VehicleFactory} is asked to provide two
17+
* vehicles of different colors, the objects returned are different.
18+
*/
19+
@Test
20+
public void givenDifferentFlyweightObjects_whenEquals_thenFalse() {
21+
Vehicle blackVehicle = VehicleFactory.createVehicle(Color.BLACK);
22+
Vehicle blueVehicle = VehicleFactory.createVehicle(Color.BLUE);
23+
24+
Assert.assertNotNull("Object returned by the factory is null!", blackVehicle);
25+
Assert.assertNotNull("Object returned by the factory is null!", blueVehicle);
26+
Assert.assertNotEquals("Objects returned by the factory are equals!", blackVehicle, blueVehicle);
27+
}
28+
29+
/**
30+
* Checks that when the {@link VehicleFactory} is asked to provide two
31+
* vehicles of the same colors, the same object is returned twice.
32+
*/
33+
@Test
34+
public void givenSameFlyweightObjects_whenEquals_thenTrue() {
35+
Vehicle blackVehicle = VehicleFactory.createVehicle(Color.BLACK);
36+
Vehicle anotherBlackVehicle = VehicleFactory.createVehicle(Color.BLACK);
37+
38+
Assert.assertNotNull("Object returned by the factory is null!", blackVehicle);
39+
Assert.assertNotNull("Object returned by the factory is null!", anotherBlackVehicle);
40+
Assert.assertEquals("Objects returned by the factory are not equals!", blackVehicle, anotherBlackVehicle);
41+
}
42+
}

0 commit comments

Comments
 (0)