当前位置:网站首页>How to handle different types of data
How to handle different types of data
2022-06-13 00:50:00 【Small fish game development】
demand : Different props need different treatment , Such as props 1 The hook can be released , The props 2 Summon the dog
For these different effects , If you inherit directly, override the function , For heat change . Post maintenance is very unfriendly
In fact, different props are actually the processing of different data
We just need to write the data class first , Then deal with the data class
demo:
Write the data class first
public struct Move
{
public float from;
public float to;
}
Then process the data
public interface IEvent
{
void Handle();
void Handle(object a);
}
public abstract class AEvent : IEvent
{
void IEvent.Handle()
{
Run();
}
protected abstract void Run();
void IEvent.Handle(object a)
{
throw new NotImplementedException();
}
}
public abstract class AEvent<T> : IEvent where T : struct
{
void IEvent.Handle()
{
throw new NotImplementedException();
}
void IEvent.Handle(object a)
{
Run((T)a);
}
protected abstract void Run(T a);
}
public class Log2Event : AEvent<Move>
{
protected override void Run(Move a)
{
System.Console.WriteLine($"from:{
a.from} to:{
a.to}");
}
}
Just test it
class Program
{
static void Main(string[] args)
{
(new Log2Event() as IEvent).Handle(new Move {
from=0,to=1 });
}
}

without doubt , The rewrite handler has nothing to do with the original data class .
It is obvious from the above that , The life cycle of the handler should be from the beginning of the application to the end of the application
So we just need to create a handler when initializing the game , Suppose you want to update the handler , Just replace it directly
class Program
{
static void Main(string[] args)
{
Dictionary<Type, IEvent> dict = new Dictionary<Type, IEvent>();
dict.Add(typeof(Move), new Log2Event());
var move = new Move {
from = 0,to=1 };
dict[move.GetType()].Handle(move);
}
}
At first glance, there seems to be no problem . In case we have 100 Data classes , Do you want to write 100 individual Add?
At this time, it should be reflected , Reflect the entire assembly , Add the traversal handler to the dictionary .
Definition dictionary Key, Any type , As long as it is a unique value, there is no problem
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class EventAttribute : Attribute
{
public string eventId;
public EventAttribute(string eventId)
{
this.eventId = eventId;
}
}
Traverse the assembly all added key Processing function of , Add to dictionary
public class EventSystem
{
readonly Dictionary<string, List<IEvent>> dict = new Dictionary<string, List<IEvent>>();
public EventSystem(System.Reflection.Assembly assembly)
{
var eventAttribute = typeof(EventAttribute);
foreach (var type in assembly.GetTypes())
{
if (type.IsAbstract||type.IsInterface)
continue;
var attr = type.GetCustomAttributes(eventAttribute, false);
if (attr.Length == 0)
continue;
var obj = Activator.CreateInstance(type) as IEvent;
if (obj == null)
continue;
var e = attr[0] as EventAttribute;
if (!dict.ContainsKey(e.eventId))
dict.Add(e.eventId, new List<IEvent>());
dict[e.eventId].Add(obj);
}
}
public void Run(string eventId)
{
List<IEvent> list;
if (!dict.TryGetValue(eventId, out list))
return;
foreach (var e in list)
{
try
{
e.Handle();
}
catch (Exception ex)
{
throw ex;
}
}
}
public void Run<T>(string eventId,T a)where T : struct
{
List<IEvent> list;
if(!dict.TryGetValue(eventId, out list))
return;
foreach (var e in list)
{
try
{
e.Handle(a);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Examples of use :
[Event("test")]
public class LogEvent : AEvent
{
protected override void Run()
{
System.Console.WriteLine("test");
}
}
class Program
{
static void Main(string[] args)
{
var eventSystem = new EventSystem(typeof(Program).Assembly);
eventSystem.Run("test");
eventSystem.Run("test2", new Move {
from = 0, to = 1 });
}
}
边栏推荐
- [virtual machine] notes on virtual machine environment problems
- The seventh finals of the Blue Bridge Cup
- Android Weather
- Set sail
- MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
- 蓝桥杯单片机第七届决赛
- 单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
- 生物解锁--指纹录入流程
- Biological unlocking - Fingerprint entry process
- Build your own PE manually from winpe of ADK
猜你喜欢
![[JS component] floating text](/img/e5/7faad5422bba919bed34e3dbcf7ba0.jpg)
[JS component] floating text
![[JS component library] drag sorting component](/img/f9/4090b52da1a5784b834cb7dbbb948c.jpg)
[JS component library] drag sorting component

Kotlin coroutine suspend function suspend keyword

高阶极点对于波形的影响

Expression tree - medium order printout

Summary of openstack installation problems

Antdpro - protable realizes the linkage effect of two selection boxes
![[MRCTF2020]Ez_ bypass --BUUCTF](/img/73/85262c048e177968be67456fa4fe02.png)
[MRCTF2020]Ez_ bypass --BUUCTF

Influence of higher order poles on waveform

Arduino control tm1637 common positive four digit nixie tube
随机推荐
[virtual machine] notes on virtual machine environment problems
Composite key relationships using Sqlalchemy - relationships on composite keys using Sqlalchemy
Canvas game lower level 100
Cve-2021-24078 vulnerability analysis
Andersen global expands its business in northern Europe through cooperation agreements in Finland and Denmark
硬(磁)盘(一)
Mysql批量插入数据时如何解决重复问题?
(01). Net Maui actual construction project
Buuctf's babysql[geek challenge 2019]
为什么磁盘分区的时候,第一个分区前面总有一段空间(63或者2048个扇区)
Arduino control soil moisture sensor
Undirected graph -- computing the degree of a node in compressed storage
[server data recovery] successful cases of data loss recovery during data migration between storage servers
STM32 USB Basics
Tree - delete all leaf nodes
Map from getting started to performance optimization
[virtual machine] notes on virtual machine environment problems
ROS2之OpenCV人脸识别foxy~galactic~humble
Basic operations of FreeMarker
Arduino interrupt