《软件设计精要与模式》勘误

感谢读者Calix.Chen对本书的仔细阅读,给我指出了书中的几个错误。

1、在本书第20章278页的20.2节第5行:“Observer抽象为包含ChangeState方法的接口”,改为“将Subject抽象为包含ChangeState方法的接口”。

2、在本书第21章所给出的示例中,犯了两个严重的错误:
(1)不应该在代理对象ProxyControlsCreator的构造函数中多线程运行真实对象RealControlsCreator的CreateEQPControls方法,而应该在代理对象ProxyControlsCreator的CreateEQPControls方法中多线程运行;

(2)按照原书给出的代码,由于真实对象的CreateEQPControls方法是在代理对象的构造函数中执行,则可能导致在执行代理对象的CreateEQPControls方法执行之前,真实对象的CreateEQPControls方法已经执行完毕,从而可能导致该方法会被执行两次(客户端显式调用一次,AddControlsEvent事件触发时调用一次)

鉴于以上两个错误,需要修改代理对象ProxyControlsCreator的实现,代码如下:
namespace DonOfDesign.PracticeProxy.ControlsShow
{
    public class ProxyControlsCreator : ControlsCreator
    {
        private RealControlsCreator m_realCreator = new RealControlsCreator();
        private Thread m_thread;

        //定义事件;
        public event AddControlsHandler AddControlsEvent;
       
        public ProxyControlsCreator()
        {
            //将CreateEQPControls方法与真实对象的事件建立委托链
            m_realCreator.AddControlsEvent += new AddControlsHandler(CreateEQPControls);             
        }
       
        public override void CreateEQPControls()
        {
            if (m_realCreator.BeCompleted)
            {               
                m_controls = new Control[m_realCreator.Controls.Length];
                m_realCreator.Controls.CopyTo(m_controls, 0);

                //当代理对象的控件集合被更新后,需要触发事件通知OperatorForm
                if (AddControlsEvent != null)
                {
                    AddControlsEvent();
                }

                if (m_thread != null)
                {
                    m_thread.Abort();
                }
            }
            else
            {
                //在后台运行真实对象的业务逻辑;
                m_thread = new Thread(new ThreadStart(m_realCreator.CreateEQPControls));
                m_thread.Start();

                //在前台运行代理对象的业务逻辑;
                base.CreateEQPControls();
            }
        }
    }
}

posted @ 2007-11-29 14:23  张逸  阅读(2655)  评论(13编辑  收藏  举报