You are currently viewing Fun with Interfaces: Patterns

Fun with Interfaces: Patterns

  • Post category:Live Blog
  • Post comments:0 Comments
  • Reading time:5 mins read

Today I had my fifth live stream. The topic was interfaces, and this time I took a different angle at it: patterns. When I polled about live stream topics you’d like to hear about, most of you said “patterns”. So today’s stream covers several patterns and puts them in perspective of interfaces, or the opposite way around, however you like it.

The stream is available on my new http://vjeko.live/ page, and here it is if you hate clicking.

Inversion of Control

The first pattern I talked about was inversion of control. It’s not really a pattern, it’s a design principle. If anything, you could say it’s a group of patterns.

In tightly-coupled world, when an object A directly accesses object B, you could say that object A directly depends on object B. You can imagine, having objects directly depend on other objects is a bad idea. The problem in AL is that it’s all over the place. Nice thing is that interfaces and enums in AL make inversion of control seem almost natural. In fact, you don’t even have to think about it most of the times you use enums and interfaces.

I tried to explain this concept in simple terms in my video, so if you want to see how it maps to AL, check it out.

Dependency Injection

If you ever heard me speak about interfaces, you have also heard me mention dependency injection. Dependency injection is one of possible implementations of inversion of control principle. It’s a technique (or a pattern if you want) where the caller injects dependency into an object that it calls. My session today covers this in depth. I absolutely love this approach in C#, and with interfaces it’s now also easy to do it in AL.

Factory

Strictly speaking, there is no such thing as a factory pattern. There are two patterns that use the word “factory” in their name (they are factory method pattern, and abstract factory pattern). But strictly speaking, AL can’t achieve neither of them exactly as their UML description demands.

Still the principles of both can be summed under “factory” pattern, and in reality, there are varying implementations of general “factory” pattern principles that you can easily achieve in AL if you want. In my stream, I present a couple of those, and I give you templates that you can use as snippets or boilerplate for creating your own factories.

Discovery Factory Pattern

Now we are talking real AL. Officially, there is no such thing as “discovery factory pattern”, but it would be so cool if there actually was one. So I came up with it.

This pattern that I propose is extension of a generic “factory” pattern that applies the handled pattern (I called that one “gentlemen’s agreement pattern” once). It extends the factory pattern through a discovery-like event. If it discovers an implementation, it uses that one. If not, does the factory stuff in the usual way.

Repository with examples

Like (nearly) always with my demos, I publish code examples on my GitHub page. Check the “Fun with Interfaces – Episode 2” examples here:

https://github.com/vjekob/fwi2

Thanks for bearing with me through my technical difficulties, I’ll post another blog where I summarize my experience with live streaming, software, hardware, and configuration.

Have a great weekend, see you next week when I’ll talk about using interfaces for testability.

Vjeko

Vjeko has been writing code for living since 1995, and he has shared his knowledge and experience in presentations, articles, blogs, and elsewhere since 2002. Hopelessly curious, passionate about technology, avid language learner no matter human or computer.

Leave a Reply