mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 16:14:58 +00:00
Commented flyweight example.
This commit is contained in:
1 parent
05d212959a
commit
05cb40897b
4 files changed
+22
No files matched your search
@@ -3,6 +3,11 @@ package com.iluwatar;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* The class that needs many objects.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class AlchemistShop {
|
public class AlchemistShop {
|
||||||
|
|
||||||
List<Potion> topShelf;
|
List<Potion> topShelf;
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Flyweight (PotionFactory) is useful when there is plethora of
|
||||||
|
* objects (Potion). It provides means to decrease resource usage
|
||||||
|
* by sharing object instances.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class App
|
public class App
|
||||||
{
|
{
|
||||||
public static void main( String[] args )
|
public static void main( String[] args )
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Interface for objects.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public interface Potion {
|
public interface Potion {
|
||||||
|
|
||||||
public void drink();
|
public void drink();
|
||||||
|
|||||||
@@ -2,6 +2,11 @@ package com.iluwatar;
|
|||||||
|
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Flyweight.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class PotionFactory {
|
public class PotionFactory {
|
||||||
|
|
||||||
private EnumMap<PotionType, Potion> potions;
|
private EnumMap<PotionType, Potion> potions;
|
||||||
|
|||||||
Reference in new issue
Block a user