2012年6月24日 星期日

[UML] Class Diagram for C#

[UML] Class Diagram for C#

This article is designing a Class Diagram for the ROLES Classes in a Star Wars game.
In this game, every person have his/her own role type which can be "Civilian", "Warrior", "Politician" ... etc.

The following claass diagram only shows how a "Warrior" use their weapons.


Picture 01 - The whole diagram



1. Realization

So I create a class named CWarrior, which implements the interface IWarrior.
The relation between them is Realization.
That means CWarrior implements IWarrior.
  (Picture 02 - Realization)
public class CWarrior : IWarrior
    {
        private CCamp myCamp; //陣營
        protected String myName; //名稱

        /* cCamp */
        public CCamp Camp
        {
            get
            {
                return myCamp;
            }
            set
            {
                myCamp = value;
            }
        }
        /* sName */
        public String Name
        {
            get
            {
                return myName;
            }
            set
            {
                myName = value;
            }
        }

        /* 使用武器 */
        public virtual void Use_Weapon()
        {
            Console.WriteLine("{0} 使用了武器。", myName);
        }
    }


2. Aggregation

We use a enum class named CCamp in CWarrior.

A warrior belongs to no or only one camp.
A camp can keep existing even when a warrior disappear.
So the relation between them is Aggregation.
 (Picture 03 - Aggregation)

public enum CCamp
    {
        Empire,
        Rebel_Alliance
    }




3. Generalization

Next step, we create two class CJedi and CSith , to generalize CWarrior.
We add an additional property : CWeapon Weapon (see 4.),
and a override method : Use_Weapon() to use Jedi's and Sith's special weapons.
The Generalization between a child class and a father class is showed in picture 04.
 (Picture 04 - Generaliztion )

public class CJedi : CWarrior
    {
        public CWeapon Weapon; //武器

        /* 使用武器 (改寫) */
        public override void Use_Weapon()
        {
            Console.WriteLine("絕地武士 {0} 使用了武器 - {1}。", this.myName, this.Weapon);
        }
    }

public class CSith : CWarrior
    {
        public CWeapon Weapon; //武器

        /* 使用武器 (改寫) */
        public override void Use_Weapon()
        {
            Console.WriteLine("西斯武士 {0} 使用了武器 - {1}。", this.myName, this.Weapon);
        }
    }



4. Composition

We use CWeapon in both CJedi and CSith.
In my opnion, the weapons cannot be used if the Jedi and Sith warriors all disappear.
So I use the Composition relation here.
That means CWeapon only exist or use with CJedi and CSith, other class can't use CWeapon.
Furthermore, one jedi/sith can only have one weapon, so the multiplicity is set to be 1 to 1.
 (Picture 05 - Composition)
 public enum CWeapon
    {
        Laser_Gun,
        Light_Saber
    }



Test result ...

A warrior named "白兵" used his weapon.
output  :  白兵 使用了武器。

A Jedi named "路克天行者" used his weapon.
output : 絕地武士 路克天行者 使用了武器 - Light_Saber。

A Sith named "達斯維達" used his weapon.
output : 西斯武士 達斯維達 使用了武器 - Light_Saber。



Reference :
http://www.webdsai.idv.tw/?p=2329

2012年6月8日 星期五

[C#] 無法建置專案輸出群組'內容檔'...

今天在把舊有的專案的組件名稱和預設命名空間修改之後,
發現原來的佈署專案,建置的時後出現了

無法建置專案輸出群組'內容檔'...

這個錯誤訊息,後來發現是因為專案屬性更改前,我就手動把原本的專案編譯出來的dll砍掉了,
但是Visual Studio還是留著這個dll的紀錄,所以在建置內容檔時會出現這個錯誤。

所以下次如果有這個問題,不妨先看一下方案總管裡面是否有出現驚嘆號的檔案或其它文件。