Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -5,53 +5,52 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* AlchemistShop holds potions on its shelves.
|
||||
* It uses PotionFactory to provide the potions.
|
||||
* AlchemistShop holds potions on its shelves. It uses PotionFactory to provide the potions.
|
||||
*
|
||||
*/
|
||||
public class AlchemistShop {
|
||||
|
||||
private List<Potion> topShelf;
|
||||
private List<Potion> bottomShelf;
|
||||
private List<Potion> topShelf;
|
||||
private List<Potion> bottomShelf;
|
||||
|
||||
public AlchemistShop() {
|
||||
topShelf = new ArrayList<>();
|
||||
bottomShelf = new ArrayList<>();
|
||||
fillShelves();
|
||||
}
|
||||
public AlchemistShop() {
|
||||
topShelf = new ArrayList<>();
|
||||
bottomShelf = new ArrayList<>();
|
||||
fillShelves();
|
||||
}
|
||||
|
||||
private void fillShelves() {
|
||||
private void fillShelves() {
|
||||
|
||||
PotionFactory factory = new PotionFactory();
|
||||
PotionFactory factory = new PotionFactory();
|
||||
|
||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||
|
||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
||||
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
||||
}
|
||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
||||
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
||||
}
|
||||
|
||||
public void enumerate() {
|
||||
public void enumerate() {
|
||||
|
||||
System.out.println("Enumerating top shelf potions\n");
|
||||
System.out.println("Enumerating top shelf potions\n");
|
||||
|
||||
for (Potion p : topShelf) {
|
||||
p.drink();
|
||||
}
|
||||
for (Potion p : topShelf) {
|
||||
p.drink();
|
||||
}
|
||||
|
||||
System.out.println("\nEnumerating bottom shelf potions\n");
|
||||
System.out.println("\nEnumerating bottom shelf potions\n");
|
||||
|
||||
for (Potion p : bottomShelf) {
|
||||
p.drink();
|
||||
}
|
||||
}
|
||||
for (Potion p : bottomShelf) {
|
||||
p.drink();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,26 +2,27 @@ package com.iluwatar.flyweight;
|
||||
|
||||
/**
|
||||
*
|
||||
* Flyweight pattern is useful when the program needs a huge amount of objects.
|
||||
* It provides means to decrease resource usage by sharing object instances.
|
||||
* Flyweight pattern is useful when the program needs a huge amount of objects. It provides means to
|
||||
* decrease resource usage by sharing object instances.
|
||||
* <p>
|
||||
* In this example {@link AlchemistShop} has great amount of potions on its shelves.
|
||||
* To fill the shelves {@link AlchemistShop} uses {@link PotionFactory} (which represents
|
||||
* the Flyweight in this example). Internally {@link PotionFactory} holds a map
|
||||
* of the potions and lazily creates new ones when requested.
|
||||
* In this example {@link AlchemistShop} has great amount of potions on its shelves. To fill the
|
||||
* shelves {@link AlchemistShop} uses {@link PotionFactory} (which represents the Flyweight in this
|
||||
* example). Internally {@link PotionFactory} holds a map of the potions and lazily creates new ones
|
||||
* when requested.
|
||||
* <p>
|
||||
* To enable safe sharing, between clients and threads, Flyweight objects must
|
||||
* be immutable. Flyweight objects are by definition value objects.
|
||||
* To enable safe sharing, between clients and threads, Flyweight objects must be immutable.
|
||||
* Flyweight objects are by definition value objects.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
AlchemistShop alchemistShop = new AlchemistShop();
|
||||
alchemistShop.enumerate();
|
||||
}
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
AlchemistShop alchemistShop = new AlchemistShop();
|
||||
alchemistShop.enumerate();
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,8 @@ package com.iluwatar.flyweight;
|
||||
*/
|
||||
public class HealingPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel healed. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel healed. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,8 @@ package com.iluwatar.flyweight;
|
||||
*/
|
||||
public class HolyWaterPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel blessed. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel blessed. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,8 @@ package com.iluwatar.flyweight;
|
||||
*/
|
||||
public class InvisibilityPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You become invisible. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You become invisible. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,8 @@ package com.iluwatar.flyweight;
|
||||
*/
|
||||
public class PoisonPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("Urgh! This is poisonous. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("Urgh! This is poisonous. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
}
|
||||
|
@ -7,5 +7,5 @@ package com.iluwatar.flyweight;
|
||||
*/
|
||||
public interface Potion {
|
||||
|
||||
void drink();
|
||||
void drink();
|
||||
}
|
||||
|
@ -5,48 +5,47 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* PotionFactory is the Flyweight in this example.
|
||||
* It minimizes memory use by sharing object instances.
|
||||
* It holds a map of potion instances and new potions
|
||||
* are created only when none of the type already exists.
|
||||
* PotionFactory is the Flyweight in this example. It minimizes memory use by sharing object
|
||||
* instances. It holds a map of potion instances and new potions are created only when none of the
|
||||
* type already exists.
|
||||
*
|
||||
*/
|
||||
public class PotionFactory {
|
||||
|
||||
private final Map<PotionType, Potion> potions;
|
||||
private final Map<PotionType, Potion> potions;
|
||||
|
||||
public PotionFactory() {
|
||||
potions = new EnumMap<>(PotionType.class);
|
||||
}
|
||||
public PotionFactory() {
|
||||
potions = new EnumMap<>(PotionType.class);
|
||||
}
|
||||
|
||||
Potion createPotion(PotionType type) {
|
||||
Potion potion = potions.get(type);
|
||||
if (potion == null) {
|
||||
switch (type) {
|
||||
case HEALING:
|
||||
potion = new HealingPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
case HOLY_WATER:
|
||||
potion = new HolyWaterPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
case INVISIBILITY:
|
||||
potion = new InvisibilityPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
case POISON:
|
||||
potion = new PoisonPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
case STRENGTH:
|
||||
potion = new StrengthPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return potion;
|
||||
}
|
||||
Potion createPotion(PotionType type) {
|
||||
Potion potion = potions.get(type);
|
||||
if (potion == null) {
|
||||
switch (type) {
|
||||
case HEALING:
|
||||
potion = new HealingPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
case HOLY_WATER:
|
||||
potion = new HolyWaterPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
case INVISIBILITY:
|
||||
potion = new InvisibilityPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
case POISON:
|
||||
potion = new PoisonPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
case STRENGTH:
|
||||
potion = new StrengthPotion();
|
||||
potions.put(type, potion);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return potion;
|
||||
}
|
||||
}
|
||||
|
@ -7,5 +7,5 @@ package com.iluwatar.flyweight;
|
||||
*/
|
||||
public enum PotionType {
|
||||
|
||||
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON
|
||||
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON
|
||||
}
|
||||
|
@ -7,9 +7,8 @@ package com.iluwatar.flyweight;
|
||||
*/
|
||||
public class StrengthPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel strong. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel strong. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.flyweight.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user