#PARAN SILVERLIGHT#
  • Tistory
    • 관리자
    • 글쓰기
Carousel 01
Carousel 02
Previous Next

Event Practice

PROGRAMING/C# 2012. 11. 13. 14:13

시나리오 : Ball이 이벤트를 일으켰을 때 이벤트를 등록한 Pitcher object에 Invoke를 한다.

 

EventPractice.zip

 

 

 

 

    class Ball
    {
        //Ball Calss는 Event를 멤버 변수 가진다.
        public event EventHandler BallInPlay;

        public void OnBallInPlay(BallEventArgs e)
        {
            EventHandler ballInPlay = BallInPlay;

            //OnBallPlay 실행 시 등록된 Event Handle가 있다면 Invoke 한다.
            if (ballInPlay != null)
                ballInPlay(this, e);
        }
    }

 

 

    class BallEventArgs : EventArgs // EventArgs class를 상속
    {
        public int Trajectory { get; private set; }
        public int Distance { get; private set; }
        public BallEventArgs(int trajectory, int distance)
        {
            this.Trajectory = trajectory;
            this.Distance = distance;
        }
    }

 

 

    class Pitcher
    {
        public Pitcher(Ball ball)
        {
            //생성자에서  event handler에 ball_BallInPlay함수포인터를 넘긴다(등록한다)
            ball.BallInPlay += new EventHandler(ball_BallInPlay);
            //ball.BallInPlay += ball_BallInPlay;
        }

        //Ball Class에서 ballInPlay를 invoke하면 등록된 아래 함수가 실행된다.
        void ball_BallInPlay(object sender, EventArgs e)
        {
            if (e is BallEventArgs)
            {
                BallEventArgs ballEventArgs = e as BallEventArgs;
                if ((ballEventArgs.Distance < 95) && (ballEventArgs.Trajectory < 60))
                    CatchBall();
                else
                    CoverFirstBase();
            }
        }


        private void CatchBall()
        {
            Console.WriteLine("Pitcher: I caught the ball");
        }

        private void CoverFirstBase()
        {
            Console.WriteLine("Pitcher: I covered first base");
        }
    }

 

    public partial class Form1 : Form
    {
        Ball ball = new Ball();
        Pitcher pitcher;
        Fan fan;


        public Form1()
        {
            InitializeComponent();
            pitcher = new Pitcher(ball);
            fan = new Fan(ball);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BallEventArgs ballEventArgs = new BallEventArgs((int)listBox1.Value, (int)listBox2.Value);

      //Ball 에서 Event를 발생한다. => eventhandler에 의해 등록된 모든 함수가 실행
      ball.OnBallInPlay(ballEventArgs);

        }
    }

 

 

 

cf ) Main Form에서 button1_Click event 생성 시 자동 생성된 코드를 살펴보면 Button의 Event handler에 등록을 한 것을 볼수 있다.  System.EventHandler는  Button의 Click Event가 발생 시에 대리자로써 Form에 button1_Click()함수를 실행시킨다 .

 

    public partial class Form1 : Form
    {
        Ball ball = new Ball();
        Pitcher pitcher;
        Fan fan;


        public Form1()
        {
            InitializeComponent();
            pitcher = new Pitcher(ball);
            fan = new Fan(ball);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BallEventArgs ballEventArgs = new BallEventArgs((int)listBox1.Value, (int)listBox2.Value);
            ball.OnBallInPlay(ballEventArgs);

        }
    }

 

Form1.Designer에서 자동으로 생성된 코드(button1_Click가 등록)

            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(26, 127);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(186, 96);
            this.button1.TabIndex = 4;
            this.button1.Text = "PlayBall";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);

 

저작자표시 비영리 (새창열림)
블로그 이미지

파란실버라이트

To remember the time when I started learning Silver Light!

,

카테고리

  • Inforamtion Technology (281)
    • DESIGN PATTERN (33)
      • 실용주의 디자인패턴 (29)
    • SOFTWARE ENGINEERING (21)
      • Art Of Readable Code (12)
      • Object Oriented Programming (6)
      • TDD (2)
    • FRAMEWORK (22)
      • Spring.net (2)
      • LightSwitch (20)
    • PROGRAMING (58)
      • C# (20)
      • .NET (6)
      • HTML5 (7)
      • ASP.NET (9)
      • SILVERLIGHT (7)
      • Ruby On Rails (6)
    • PROJECT MANAGEMENT (10)
      • SW Version Management (7)
      • Schedulring Management (1)
    • BOOKS (18)
    • MOBILE APP (1)
      • SENCHA TOUCH (1)
    • SECURITY (5)
    • MES (1)
    • B2B (14)
      • WEBMETHODS (4)
    • ERP (53)
      • SAP/R/3 (51)
    • ABOUT TOOLS (2)
    • FUNDAMENT CONCEPT (21)
    • SOA BPM (22)
    • PORTFOLIO (0)

태그목록

  • 동시성
  • 프로그래밍
  • 병렬

최근에 받은 트랙백

글 보관함

링크

파란실버라이트

블로그 이미지

To remember the time when I started learning Silver Light!

LATEST FROM OUR BLOG

RSS 구독하기

LATEST COMMENTS

BLOG VISITORS

  • Total :
  • Today :
  • Yesterday :

Copyright © 2015 Socialdev. All Rights Reserved.

티스토리툴바