forked from echoTheLiar/JavaCodeAcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Liu Yuning
committed
May 21, 2017
1 parent
29bb528
commit fab9b5e
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package designpattern.flyweight; | ||
|
||
/** | ||
* 所有具体享元类的超类,接受并作用于外部状态 | ||
* | ||
* @author liu yuning | ||
* | ||
*/ | ||
public abstract class FlyWeight { | ||
|
||
public abstract void operation(int extrinsicState); | ||
|
||
} | ||
|
||
class ConcreteFlyWeight extends FlyWeight { | ||
|
||
@Override | ||
public void operation(int extrinsicState) { | ||
System.out.println("具体FlyWeight:" + extrinsicState); | ||
} | ||
|
||
} | ||
|
||
class UnsharedConcreteFlyWeight extends FlyWeight { | ||
|
||
@Override | ||
public void operation(int extrinsicState) { | ||
System.out.println("不共享的具体FlyWeight:" + extrinsicState); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package designpattern.flyweight; | ||
|
||
/** | ||
* 客户端 | ||
* | ||
* @author liu yuning | ||
* | ||
*/ | ||
public class FlyWeightClient { | ||
public static void main(String[] args) { | ||
int extrinsicState = 22; | ||
|
||
FlyWeightFactory f = new FlyWeightFactory(); | ||
|
||
FlyWeight fx = f.getFlyWeight("X"); | ||
fx.operation(--extrinsicState); | ||
|
||
FlyWeight fy = f.getFlyWeight("Y"); | ||
fy.operation(--extrinsicState); | ||
|
||
FlyWeight fz = f.getFlyWeight("Z"); | ||
fz.operation(--extrinsicState); | ||
|
||
FlyWeight uf = new UnsharedConcreteFlyWeight(); | ||
uf.operation(--extrinsicState); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package designpattern.flyweight; | ||
|
||
import java.util.HashMap; | ||
|
||
/** | ||
* 享元工厂 | ||
* | ||
* @author liu yuning | ||
* | ||
*/ | ||
public class FlyWeightFactory { | ||
private HashMap<String, FlyWeight> flyWeights = new HashMap<String, FlyWeight>(); | ||
|
||
public FlyWeight getFlyWeight(String key) { | ||
if (!flyWeights.containsKey(key)) { | ||
flyWeights.put(key, new ConcreteFlyWeight()); | ||
} | ||
|
||
return flyWeights.get(key); | ||
} | ||
|
||
} |