很久没写设计模式学习笔记了,今天无聊,随便把以前看的模式的都写下.

先把代码帖上来吧.

这个模式,是模拟 西山居的一款游戏,剑侠情缘 来写的,呵呵,当然,该游戏到底是怎么设计的,我是不知道,只是作为一个例子放到这个地方.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace 建造者
{
    public interface 地图
    {
        路 路
        {
            get;
            set;
        }
        树 树
        {
            get;
            set;
        }

        Color MainColor
        {
            get;
            set;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace 建造者
{
    public class 长白山南麓 : 地图
    {

        private 路 tempRoad;
        public 路 路
        {
            get
            {
                return tempRoad;
            }
            set
            {
                tempRoad = value;
            }
        }

        private 树 tempTree;
        public 树 树
        {
            get
            {
                return tempTree;
            }
            set
            {
                tempTree = value;
            }
        }

        private System.Drawing.Color tempMainColor;
        public System.Drawing.Color MainColor
        {
            get
            {
                return tempMainColor;
            }
            set
            {
                tempMainColor = value;
            }
        }

       
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace 建造者
{
    public interface 路
    {
        double Width
        {
            get;
            set;
        }

        double Height
        {
            get;
            set;
        }

        System.Drawing.Color Color
        {
            get;
            set;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace 建造者
{
    public class 水泥路 : 路
    {
        #region 路 Members

        public double Width
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public double Height
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public System.Drawing.Color Color
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace 建造者
{
    public interface 树
    {
        System.Drawing.Color Color
        {
            get;
            set;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace 建造者
{
    public class 梧桐树 : 树
    {
        #region 树 Members

        public System.Drawing.Color Color
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace 建造者
{
    public interface 地图创建
    {
        void 创建路();

        void 创建树();

        void 设置颜色();

        地图 地图对象();   
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace 建造者
{
    public class 长白山南麓地图创建 : 地图创建
    {
        private 地图 长白山南麓地图;
        public 长白山南麓地图创建()
        {
            长白山南麓地图 = new 长白山南麓();
        }

        public void 创建路()
        {
            路 长白山南麓的路 = new 水泥路();
            长白山南麓地图.路 = 长白山南麓的路;
        }

        public void 创建树()
        {
            树 长白山南麓的树 = new 梧桐树();
            长白山南麓地图.树 = 长白山南麓的树;
        }

        public void 设置颜色()
        {
            长白山南麓地图.MainColor = System.Drawing.Color.White;
        }

        public 地图 地图对象()
        {
            return 长白山南麓地图;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace 建造者
{
 

 public class 系统
    {
        地图创建 地图创建;
        public 系统(地图创建 一个地图创建实例)
        {
            地图创建 = 一个地图创建实例;
        }

        public 地图 创建地图()
        {

            地图创建.创建路();

            地图创建.创建树();

            地图创建.设置颜色();

            return 地图创建.地图对象();
        }
    }


}


客户端调用:

 

            建造者.系统 建造者系统 = new 建造者.系统(new 建造者.长白山南麓地图创建());

          
            建造者.地图 长白山南麓地图 = 建造者系统.创建地图();



我很苦恼,按照这样的做法,怎么越来越象 工厂方法了.
呵呵,也不知道这个模式有没有写对.
先把代码放到这个地方,有时间在研究 创建者  和工厂方法的区别.

posted on 2007-12-18 11:23  颜昌钢  阅读(305)  评论(9编辑  收藏  举报