GAM456-SpaceInvaders / SpaceInvaders / 0_Input / InputSubject.cs
InputSubject.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class InputSubject
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        private SLinkMan poSubscriberList;


        //-------------------------------------------------------------------
        //  CONSTRUCTION
        //-------------------------------------------------------------------

        public InputSubject() 
        {
            this.poSubscriberList = new SLinkMan();
            Debug.Assert(this.poSubscriberList != null);
        }


        //-------------------------------------------------------------------
        //  PUBLIC METHODS
        //-------------------------------------------------------------------

        public void Attach(InputObserver pObserver)
        {
            Debug.Assert(pObserver != null);

            pObserver.pSubject = this;
            this.poSubscriberList.AddToFront(pObserver);
        }

        public void Notify()
        {
            Iterator pIterator = this.poSubscriberList.GetIterator();

            InputObserver pObserver = (InputObserver)pIterator.Peek();
            while(!pIterator.IsDone())
            {
                //call listener
                pObserver.Notify();

                //get next
                pObserver = (InputObserver)pIterator.Next();
            }
        }

        public void DetachInputs()
        {
            SLink pNode = this.poSubscriberList.pHead;
            SLink ptemp;

            while(pNode != null)
            {
                ptemp = pNode;
                pNode = pNode.pNext;

                Debug.Assert(ptemp != null);
                this.poSubscriberList.Remove(ptemp);

                ptemp = null;
            }
        }


    } // end class

} // end namespace