//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Configuration;
//using System.Threading;
//namespace ConsoleApp1
//{
// public class EvenNumberEventArgs : EventArgs
// {
// public EvenNumberEventArgs(int i)
// {
// this.I = i;
// }
// public int I { get; set; }
// }
// class Program
// {
// static void Main(string[] args)
// {
// new Program().Run();
// }
// void Run()
// {
// Config config = Config.Instance;
// EventProcess ep = new EventProcess();
// //Console.WriteLine(config.SampleApplication);
// for (int i = 0; i < 10; i++)
// {
// config.NumberCheck(i);
// }
// }
// }
// public sealed class Config
// {
// private static readonly Lazy<Config> lazy =
// new Lazy<Config>(() => new Config(), LazyThreadSafetyMode.ExecutionAndPublication);
// public static Config Instance { get { return lazy.Value; } }
// private Config()
// {
// //this.SampleApplication = ConfigurationManager.AppSettings["sampleApplication"];
// }
// //public string SampleApplication { get; } = string.Empty;
// public event EventHandler<EvenNumberEventArgs> MyEvent;
// public void NumberCheck(int i)
// {
// if (i % 2 == 0)
// MyEvent?.Invoke(this, new EvenNumberEventArgs(i));
// }
// }
// class EventProcess
// {
// public EventProcess()
// {
// Config.Instance.MyEvent += EvenNumber;
// }
// void EvenNumber(object o, EvenNumberEventArgs args)
// {
// Console.WriteLine(o.GetType());
// Console.WriteLine(string.Format("this is even Number : {0}", args.I));
// }
// }
//}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
new Program().Run();
}
void Run()
{
Config config = Config.Instance;
new EventProcess();
//Console.WriteLine(config.SampleApplication);
for (int i = 0; i < 10; i++)
{
config.NumberCheck(i);
}
}
}
public sealed class Config
{
private static readonly Lazy<Config> lazy =
new Lazy<Config>(() => new Config(), LazyThreadSafetyMode.ExecutionAndPublication);
public static Config Instance { get { return lazy.Value; } }
private Config()
{
//this.SampleApplication = ConfigurationManager.AppSettings["sampleApplication"];
}
public Action<object, EvenNumberEventArgs> MyEvent;
public void NumberCheck(int i)
{
if (i % 2 == 0)
MyEvent?.Invoke(this, new EvenNumberEventArgs(i));
}
public class EvenNumberEventArgs : EventArgs
{
public EvenNumberEventArgs(int i)
{
this.I = i;
}
public int I { get; set; }
}
}
class EventProcess
{
public EventProcess()
{
Config.Instance.MyEvent += EvenNumber;
}
void EvenNumber(object o, Config.EvenNumberEventArgs args)
{
Console.WriteLine(o.GetType());
Console.WriteLine(string.Format("this is even Number : {0}", args.I));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObserverGoFCSharp2
{
class Program
{
static void Main(string[] args)
{
var ep = new EventPublisher();
var eo1 = new EventObserver1(ep);
var eo2 = new EventObserver2(ep);
eo1.EventAttach();
eo2.EventAttach();
ep.Run();
}
}
class EventPublisher // Subject // 나는 모르겠고 이런일이 있었어 니가 알아서 해~
{
//public event EventHandler<MyEventArgs> MyEvent;
public Action <object, MyEventArgs> MyEvent;
public EventPublisher()
{
}
public void Run()
{
for (int i = 0; i < 20; i++)
{
if (i % 2 == 0)
{
//Console.WriteLine($"even number : {i}");
MyEvent?.Invoke(this, new MyEventArgs
{
ArgType = "even number",
ArgData1 = i
});
}
else
{
//Console.WriteLine($"odd number : {i}");
MyEvent?.Invoke(this, new MyEventArgs
{
ArgType = "odd number",
ArgData1 = i
});
}
}
}
public class MyEventArgs : EventArgs
{
public string ArgType { get; set; }
public int ArgData1 { get; set; }
}
}
class EventObserver1 // 오 ~ 짝수네~ 하고 실행
{
EventPublisher ep;
public EventObserver1(EventPublisher ep)
{
this.ep = ep;
}
public void EventAttach()
{
this.ep.MyEvent += ObserverCallback;
}
public void EventDetach()
{
this.ep.MyEvent -= ObserverCallback;
}
private void ObserverCallback(object sender, EventPublisher.MyEventArgs args)
{
if (args.ArgType.Equals("even number", StringComparison.OrdinalIgnoreCase))
{
if (args.ArgData1 > 5)
EventDetach();
Console.WriteLine($"even number : {args.ArgData1}");
}
}
}
class EventObserver2 // 오 ~ 홀수네~ 하고 실행, 10 이 넘으면 그만 수신할래....까지 찍힘
{
EventPublisher ep;
public EventObserver2(EventPublisher ep)
{
this.ep = ep;
}
public void EventAttach()
{
this.ep.MyEvent += ObserverCallback;
}
public void EventDetach()
{
this.ep.MyEvent -= ObserverCallback;
}
private void ObserverCallback(object sender, EventPublisher.MyEventArgs args)
{
if (args.ArgType.Equals("odd number", StringComparison.OrdinalIgnoreCase))
{
if (args.ArgData1 > 10)
EventDetach();
Console.WriteLine($"odd number : {args.ArgData1}");
}
}
}
}