|
Rarestyleへようこそ
|
|
|
|
|
|
デザインパターンでキャリアアップ
|
Flyweightパターン (デザインパターン)
Flyweightパターン 導入
Flyweightパターンは、インスタンスを使いまわすことで、新たなインスタンスに必要なメモリ量を抑えるパターンです。
Clientでインスタンス生成するよりも、FlyweightFactoryに使用するインスタンスを蓄積しておき、必要に応じて蓄積したインスタンスを渡すようにします。FlyweightFactoryにはインスタンス生成機能など管理機能なども加えるなどして、必要なインスタンスを生成できるようにします。
Flyweightパターン ポイント
システム全体で、使いまわすインスタンスの把握が必要です。
UML Flyweightパターン
C#ソースコード Flyweightパターン
|
Sample.cs
|
|
using System;
using System.Collections;
namespace Flyweight
{
public abstract
class Flyweight
{
private
string strNeta;
public
string Neta
{
set{strNeta=value;}
get{return strNeta;}
}
public
abstract void Operation();
}
public class
ConcreteFlyweight_Susi :Flyweight
{
private
string strSyari;
public ConcreteFlyweight_Susi(string strNeta)
{
this.strSyari="酢飯";
this.Neta=strNeta;
}
public
string Syari
{
get{return this.strSyari;}
}
public
override void Operation()
{
Console.WriteLine("このお寿司のねたは{0}です",this.Neta);
}
}
public class
FlyweightFactory
{
Hashtable hash =new Hashtable();
public FlyweightFactory()
{
hash.Add("うに",new ConcreteFlyweight_Susi("北海道産生うに"));
hash.Add("たい",new
ConcreteFlyweight_Susi("天然鯛"));
hash.Add("あわび",new ConcreteFlyweight_Susi("あわび"));
hash.Add("イカ",new
ConcreteFlyweight_Susi("細工切りイカ"));
hash.Add("ばってら",new ConcreteFlyweight_Susi("きゅうり"));
}
public Flyweight
getFlyweight(string key)
{
if
(hash.Contains(key))
{
return
(Flyweight)hash[key];
}
else
{
Console.WriteLine("注文ありがとうございます。新しく握ります。");
return
new ConcreteFlyweight_Susi(key);
}
}
}
public class
UnsharedConcreteFlyweight_Onigiri : Flyweight
{
private
string strGohan;
public UnsharedConcreteFlyweight_Onigiri(string strNeta)
{
this.strGohan="白飯";
this.Neta=strNeta;
}
public
string Gohan
{
get{return this.strGohan;}
}
public
override void Operation()
{
Console.WriteLine("このおにぎりのねたは{0}です",this.Neta);
}
}
class Class1
{
[STAThread]
static
void Main(string[] args)
{
FlyweightFactory Kappasusi
=new FlyweightFactory();
Flyweight susi1 =
Kappasusi.getFlyweight("たい");
susi1.Operation();
Flyweight susi2 =
Kappasusi.getFlyweight("とろ");
susi2.Operation();
}
}
}
|
flyweight 実行結果
UMLプラグインによる描画 Flyweightパターン
Javaソースコード Flyweightパターン
| Flyweight.java |
1: package flyweight;
2:
3: import java.lang.String;
4:
5: public abstract class Flyweight {
6:
7: private String Neta;
8:
9: public abstract void operation();
10:
11: public String getNeta() {
12: return Neta;
13: }
14:
15: public void setNeta(String Neta) {
16: this.Neta = Neta;
17: }
18:
19: }
|
| ConcreteFlyweight_Susi.java |
1: package flyweight;
2:
3: public class ConcreteFlyweight_Susi extends Flyweight {
4:
5: private String Syari;
6:
7: public void operation() {
8: System.out.println("このお寿司のねたは" +this.getNeta()+"です");
9: }
10:
11: public String getSyari() {
12: return Syari;
13: }
14:
15: public void setSyari(String Syari) {
16: this.Syari = Syari;
17: }
18:
19: public ConcreteFlyweight_Susi(String strNeta) {
20: //コンストラクタでねたとシャリをセット
21: this.setNeta(strNeta);
22: this.setSyari("酢飯");
23: }
24:
25: }
|
| FlyweightFactory.java |
1: package flyweight;
2: import java.util.HashMap;
3:
4: public class FlyweightFactory {
5:
6: //コレクションインスタンスの宣言
7: private HashMap hashFlyweight =new HashMap();
8:
9: //インスタンスを作り置きしています。
10: public FlyweightFactory() {
11: hashFlyweight.put("うに",new ConcreteFlyweight_Susi("北海道産うに"));
12: hashFlyweight.put("たい",new ConcreteFlyweight_Susi("天然鯛"));
13: hashFlyweight.put("あわび",new ConcreteFlyweight_Susi("あわび"));
14: hashFlyweight.put("イカ",new ConcreteFlyweight_Susi("細工切りイカ"));
15: hashFlyweight.put("ばってら",new ConcreteFlyweight_Susi("しめさば"));
16: }
17:
18: public Flyweight getFlyweight(String key) {
19: if (hashFlyweight.containsKey(key))
20: {
21: return (Flyweight)hashFlyweight.get(key);
22:
23: }else{
24: System.out.println("注文ありがとうございます。新しく握ります。");
25: return (Flyweight)new ConcreteFlyweight_Susi(key);
26: }
27:
28: }
29:
30: }
|
| UnsharedConcreteFlyweight_Onigiri.java |
1: package flyweight;
2:
3: import java.lang.String;
4:
5: public class UnsharedConcreteFlyweight_Onigiri extends Flyweight {
6:
7: private String Gohan;
8:
9: public void operation() {
10: System.out.println("このおにぎりのねたは" +this.getNeta()+"です");
11: }
12:
13: public String getGohan() {
14: return Gohan;
15: }
16:
17: public void setGohan(String Gohan) {
18: this.Gohan = Gohan;
19: }
20:
21: UnsharedConcreteFlyweight_Onigiri() {
22: }
23:
24: public UnsharedConcreteFlyweight_Onigiri(String strNeta) {
25: //コンストラクタねたとご飯をセット
26: this.setNeta(strNeta);
27: this.setGohan("白飯");
28: }
29:
30: }
|
| StartClass.java |
1: package flyweight;
2:
3: public class StartClass {
4:
5: public static void main(String[] args) {
6:
7: //FlyweightFactory(回転寿司)インスタンス作成
8: FlyweightFactory flyweightfactory =new FlyweightFactory();
9:
10: //利用者はFlyweightFactoryに問い合わせます
11:
12: Flyweight susi1 =flyweightfactory.getFlyweight("たい"); //鯛を注文
13: susi1.operation();
14:
15: Flyweight susi2 =flyweightfactory.getFlyweight("とろ"); //とろを注文
16: susi2.operation();
17: }
18: }
|
デザインパターン「ひとくちメモ」へ
Homeへもどる
|
|
| お天気情報 |
|
東京都 東京 - 今日の天気
|
晴れ
-
|
|
|
最高
|
最低
|
| 気温
|
14 |
4 |
| 湿度
|
57.2 |
39.2 |
|
|
|
|
取得日:2010/03/11 17:00:00
ST:
True |
|
|
東京都 東京 - 明日の天気
|
曇時々晴
-
|
|
|
|
|
取得日:2010/03/11 17:00:00
ST:
True |
|
|
東京都 東京 - あさっての天気
|
曇時々晴
-
|
|
|
最高
|
最低
|
| 気温
|
18 |
8 |
| 湿度
|
64.4 |
46.4 |
|
|
|
|
取得日:2010/03/11 17:00:00
ST:
True |
|
|
大阪府 大阪 - 今日の天気
|
晴れ
-
|
|
|
|
|
取得日:2010/03/11 17:00:00
ST:
True |
|
|
大阪府 大阪 - 明日の天気
|
曇り
-
|
|
|
|
|
取得日:2010/03/11 17:00:00
ST:
True |
|
|
大阪府 大阪 - あさっての天気
|
晴時々曇
-
|
|
|
最高
|
最低
|
| 気温
|
17 |
6 |
| 湿度
|
62.6 |
42.8 |
|
|
|
|
取得日:2010/03/11 17:00:00
ST:
True |
|
| livedoorのWebサービスより提供 |
|
【工事中】お天気配信 ユーザー登録 |
|
|
|
|
|
Internet Explorer5.0以上のご利用を推奨いたします。
|
 |
Copyright 2006 Rarestyle
このページへのリンクは確認不要です。
Programming by Xenon Project Team
postmaster@rarestyle.net
免責事項について
|