It seems like there’s a RESTful API for everything these days. From payments to booking tables, from notifications to spinning up virtual machines, you can do almost anything with a simple HTTP interaction.

If you’re building a service of your own, you’ll often want to be able to use it on multiple platforms at once. Following time-tested OOD (object oriented design) principles makes your code more resilient and more easily extended.

In this post, we examine one particular design approach called SOLID (it’s an acronym). We put it to practical use in writing a service that’s a Slack integration and then extending it for use with Twilio.

The service sends you a random Magic the Gathering card. If you’d like to see it in action right away, you can text the word magic to: 1-929-236-9306 (U.S. and Canada only – you’ll get an image as an MMS, so messaging rates for your carrier may apply). You can also join my Slack organization by clicking here. Once you’re in, you can type: /magic.

SOLID Meets Magic the Gathering

If you are not yet familiar with SOLID, it is a set of principles for Object Oriented Design (OOD), popularized by Uncle Bob Martin. SOLID is an acronym for:

S – SRP – Single Responsibility Principle

O – OCP – Open-Closed Principle

L – LSP – Liskov Substitution Principle

I – ISP – Interface Segregation Principle

D – DIP – Dependency Inversion Principle

By following this set of principles, your code is more maintainable and more easily extended. We’ll talk about each of these principles in more detail throughout this post.

There are a lot of good examples of SOLID in a variety of languages out there. Rather than repeat the typical Shape , Circle , Rectangle , Area example, I wanted to show off the benefit of SOLID in a real-world, fully functional application.

Recently, I’ve been playing around with the Slack API. It’s really easy to create custom slash commands. I’m also a huge fan of Magic the Gathering, so I thought I’d make a Slack slash command that returns an image of a random Magic the Gathering card.

I did this very quickly using Spring Boot. As you’ll see below, Spring Boot gives you a couple SOLID principles right out of the box.

Twilio has an excellent API for voice and text messaging services. I thought it would be interesting to see just how easy it was to take my Slack example and make it work with Twilio. The idea is that you text a command to a known phone number and you get back a random Magic the Gathering image.

What follows is a breakdown of the SOLID principles (not in order) in action along the way of this software development exercise.

All the code can be found here. We’ll later also look at how we can deploy the code and get it running on your Slack and/or Twilio account, if you want to do so.

A First Pass: Magic with Slack

Just by virtue of using Spring Boot to create the Magic App, you get 2 out of the 5 SOLID principles for free. However, you’re still responsible for architecting your app properly.

Since we’ll be looking at the different principles along the development of the code, you can look at the code example at any point by checking out the respective tags in the GitHub project (you’ll find them under “Releases”). For the full code of this section check out the slack-first-pass tag.