|
Rarestyleへようこそ
|
|
|
|
|
|
デザインパターンでキャリアアップ
|
Prototypeパターン (デザインパターン)
Prototypeパターン 導入
ProtoTypeパターンは、すでに存在するオブジェクトを複製することにより、新しいオブジェクトを生成します。
newによる初期化は属性値までも、初期値に戻ります。オブジェクトの複製を作るということは、今現在のインスタンスをまるごとコピーして返すということです。
ProtoTypeパターン ポイント
インスタンスの複製を作る場合は、インスタンス自身に分身能力(自分の複製を返すインターフェース)を実装します。
浅いコピーと深いコピーについての補足説明
サンプルのC#の例で.NetFrameworkではICloneableインタフェースを通じてMemberwiseClone()メソッドを呼び出しています。これはobject(System.Object全てオブジェクトの基底クラス)クラスのメンバーでオブジェクト全体の浅いコピーを作成するものです。(JavaではCloneableインターフェースとclone()メソッドがこの役目)浅いコピーとはそのオブジェクトだけの複製を作ることを意味します。逆に深いコピーとは、オブジェクトから参照されるオブジェクトも含めたコピーを作成することを意味します。深いコピーを行うならば、これらのメソッドをオーバーライドする必要があります。
UML ProtoTypeパターン
C#ソースコード Prototypeパターン
|
Sample.cs
|
|
using System;
namespace Prototype2
{
public abstract
class Prototype_MobilSuite :ICloneable
{
public
abstract string MobilSuiteName
{
set ;
get ;
}
public
abstract string Weapon();
public
object Clone()
{
return
MemberwiseClone();
}
}
public class
ConcretePrototype_FirstGundam :Prototype_MobilSuite
{
private
string strName;
private
string strWeapon;
public ConcretePrototype_FirstGundam(string strName,string
strWeapon)
{
this.strName=strName;
this.strWeapon=strWeapon;
}
public
override string MobilSuiteName
{
set{this.strName=value;}
get{return this.strName;}
}
public
override string Weapon()
{
string
weapon =this.strWeapon
+"で撃破!!";
return
weapon;
}
}
class Class1
{
[STAThread]
static
void Main(string[] args)
{
ConcretePrototype_FirstGundam RX01 =
new ConcretePrototype_FirstGundam("RX-01","ビームライフル");
ConcretePrototype_FirstGundam
RX02 =
new ConcretePrototype_FirstGundam("RX-02","ビームサーベル");
ConcretePrototype_FirstGundam[]
jim =new ConcretePrototype_FirstGundam[6];
for(int Count=0;Count
<jim.Length
;Count++)
{
if(Count %2 ==0)
{
jim[Count]=(ConcretePrototype_FirstGundam)RX01.Clone();
jim[Count].MobilSuiteName="Jim-"+Count.ToString();
}
else
{
jim[Count]=(ConcretePrototype_FirstGundam)RX02.Clone();
jim[Count].MobilSuiteName="Jim-"+Count.ToString();
}
}
foreach(ConcretePrototype_FirstGundam jimClones
in jim)
{
Console.WriteLine( jimClones.MobilSuiteName
+"武器は"
+ jimClones.Weapon());
}
Console.WriteLine(RX01.MobilSuiteName
+"登場 !! 武器は" +
RX01.Weapon());
Console.WriteLine(RX02.MobilSuiteName
+"同じく推参!!武器は" +
RX02.Weapon());
}
}
}
|
Prototype 実行結果
UMLプラグインによる描画 Prototypeパターン
Javaソースコード ProtoTypeパターン
| IMobilSuiteName.java |
1: package prototype;
2:
3: public interface IMobilSuiteName {
4:
5: String getMobilSuteName();
6:
7: void setMobilSuteName(String MobilSuteName);
8:
9: }
|
| Prototype_MobilSuite.java |
1: package prototype;
2:
3: public abstract class Prototype_MobilSuite implements IMobilSuiteName, Cloneable{
4:
5: public abstract String Weapon();
6:
7: public Object Clone() throws CloneNotSupportedException{
8: return super.clone();
9: }
10:
11: }
|
| ConcretePrototype_FirstGundam.java |
1: package prototype;
2:
3: public class ConcretePrototype_FirstGundam extends Prototype_MobilSuite {
4: private String strMobilSuiteName;
5: private String strWeapon;
6:
7:
8: public String getMobilSuteName() {
9:
10: return this.strMobilSuiteName;
11: }
12:
13: public void setMobilSuteName(String MobilSuteName) {
14:
15: this.strMobilSuiteName=MobilSuteName;
16:
17: }
18:
19: public String Weapon() {
20:
21: String strWeapon =this.strWeapon + "で撃破!";
22:
23: return strWeapon;
24: }
25:
26: public ConcretePrototype_FirstGundam(String strName, String strWeapon) {
27: this.strMobilSuiteName=strName;
28: this.strWeapon=strWeapon;
29: }
30:
31: }
|
| StartClass.java |
1: package prototype;
2:
3: public class StartClass {
4:
5: public static void main(String[] args)
6: throws CloneNotSupportedException {
7:
8: //ガンダムは2機生成します。
9: ConcretePrototype_FirstGundam rx01
10: =new ConcretePrototype_FirstGundam("RX-01","ビームライフル");
11:
12: ConcretePrototype_FirstGundam rx02
13: =new ConcretePrototype_FirstGundam("RX-02","ビームサーベル");
14:
15:
16: //ジム(量産型)を6台作ります。
17:
18: //変数の宣言
19: ConcretePrototype_FirstGundam[] jim =new ConcretePrototype_FirstGundam[6];
20:
21: //インスタンスを生成します。この時すでに作成している
22: //RX-01、RX-02の内容をコピーして作成します。
23: for(int Count= 0; Count < jim.length; Count++){
24:
25: if(Count %2==0){
26: //RX-01の内容でコピーします。
27: jim[Count]=(ConcretePrototype_FirstGundam)rx01.Clone();
28: //このままではRX-01とまったく同じなので名前を設定します。
29: jim[Count].setMobilSuteName("Jim-"+Count);
30: }else{
31: //RX-02の内容でコピーします。
32: jim[Count]=(ConcretePrototype_FirstGundam)rx02.Clone();
33: //このままではRX-02とまったく同じなので名前を設定します。
34: jim[Count].setMobilSuteName("Jim-"+Count);
35: }
36: }
37:
38: //ジムを列挙
39: for(int Count= 0; Count < jim.length; Count++){
40: System.out.println(jim[Count].getMobilSuteName()+"登場 !!"
41: + " 武器は"+jim[Count].Weapon());
42: }
43:
44: //RX-01,RX-02に影響がないことを確認します。
45: System.out.println(rx01.getMobilSuteName()+"登場 !!"
46: + " 武器は"+rx01.Weapon());
47: System.out.println(rx02.getMobilSuteName()+"登場 !!"
48: + " 武器は"+rx02.Weapon());
49:
50: }
51: }
|
デザインパターン「ひとくちメモ」へ
Homeへもどる
|
|
| お天気情報 |
|
東京都 東京 - 今日の天気
|
曇り
-
|
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
東京都 東京 - 明日の天気
|
晴のち雨
-
|
|
|
最高
|
最低
|
| 気温
|
9 |
3 |
| 湿度
|
48.2 |
37.4 |
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
東京都 東京 - あさっての天気
|
晴時々曇
-
|
|
|
最高
|
最低
|
| 気温
|
11 |
4 |
| 湿度
|
51.8 |
39.2 |
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
大阪府 大阪 - 今日の天気
|
晴のち曇
-
|
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
大阪府 大阪 - 明日の天気
|
雨
-
|
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
大阪府 大阪 - あさっての天気
|
曇時々晴
-
|
|
|
最高
|
最低
|
| 気温
|
7 |
3 |
| 湿度
|
44.6 |
37.4 |
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
| livedoorのWebサービスより提供 |
|
【工事中】お天気配信 ユーザー登録 |
|
|
|
|
|
Internet Explorer5.0以上のご利用を推奨いたします。
|
 |
Copyright 2006 Rarestyle
このページへのリンクは確認不要です。
Programming by Xenon Project Team
postmaster@rarestyle.net
免責事項について
|