Design Patterns

Design Patterns and Unity3D – Part One (Events Aggregator)

After working in different teams, and on different projects with Unity, I noticed couple of bad practices (in my opinion of course) – it’s the lack of code design and unified code structuring. I’ve been there with PHP couple of years ago, and if we’ll consider the amount of programmers in the world, there is a lot of “if it works – don’t touch it” guys, or “we’ll fix it later”, but “later” never comes. So I’m not surprised.

There can be a lot of reasons for this phenomenon, and I have couple of theories (not Illuminati – I promise!), but this post is about something else. Instead of ranting and bragging about it, I decided to write a series of posts about how, in my opinion, the code should and should not look. And I guess I’ll start with Design Patterns.

I’m not awarded with “Best coder of the year” award, hell I’m not even nominated and I don’t say my methods are perfect. All of the posts in these series are based on my personal opinion and my personal experience. I would love to receive criticism, advices or suggestions.

So, without further adieu, let’s start with the Events Aggregator. I suggest you this article, to understand it better. But if you don’t want to, here’s a quote:

A system with lots of objects can lead to complexities when a client wants to subscribe to events. The client has to find and register for each object individually, if each object has multiple events then each event requires a separate subscription.

An Event Aggregator acts as a single source of events for many objects. It registers for all the events of the many objects allowing clients to register with just the aggregator.

Before we’ll get into details, I would like to tell you about practices I’ve seen, and why you should not use them. In this post, I’ll present a task (and it’s solution), with minimum classes, but you should know – the more components will be in your code, the more messy it’s gonna get. How? – Let me explain.

Continue reading →

Posted by migs in Blog Post, Design Patterns, Unity3D, 0 comments