Mafia — An Object Oriented Design
By: Nitish Sharma
Tags: Chain Of Responsibility, Godfather, Object Oriented, Mafia, Design Patterns

The Godfather is one of the very first novels I properly made the effort to read. It was also my introduction to American Mafia and it’s prevalence during the early 20th century.

The American Mafia, commonly referred to in North America as “the Mafia” or sometimes “the Mob”, or the Italian-American Mafia, is a highly organised Italian-American criminal society and criminal organisation.

Ever since, I have pretty much watched all the mafia movies ever made. So hypothetically, if I were to create a crime family, I’d go about it the object oriented way.

Let’s get crackin’

The American Mafia operates on a strict hierarchical structure.

Hierarchy in a Mafia organisation

When a boss makes a decision, he never issues orders directly to workers who would carry it out, but instead passes instructions down through the chain of command. This way, the higher levels of the organisation stay protected from law enforcement if the lower level members who actually commit the crime

are captured or investigated, providing plausible deniability.

Hierarchical Structure

Godfather (Boss) — He is the head of the family, receives a cut of every operation.

— He is the head of the family, receives a cut of every operation. Underboss — Second in command of the family, first in line to become acting boss if the boss is imprisoned/dead, ensures collection of profits from criminal enterprises, gets a percentage of the family’s income from the boss’s cut.

— Second in command of the family, first in line to become acting boss if the boss is imprisoned/dead, ensures collection of profits from criminal enterprises, gets a percentage of the family’s income from the boss’s cut. Consigliere —Advisor to the family, serves as a representative for the family in meetings with other families, rival criminal organisations, and important business associates.

—Advisor to the family, serves as a representative for the family in meetings with other families, rival criminal organisations, and important business associates. Caporegime — In charge of a crew, a group of soldiers who report directly to him, is responsible for any tasks assigned, including murder.

— In charge of a crew, a group of soldiers who report directly to him, is responsible for any tasks assigned, including murder. Soldier — Main workers of the family, usually committing crimes like assault, murder, extortion, intimidation, etc.

Based on this structure of the system, we can incorporate the Chain of Responsibility design pattern

Chain of Responsibility Design Pattern

Chain of Respon­si­bil­i­ty is a behav­ioural design pat­tern that lets you pass requests along a chain of han­dlers. Upon receiv­ing a request, each han­dler decides either to process the request or to pass it to the next han­dler in the chain.

Chain of Responsibility pattern UML diagram

Han­dler declares the inter­face, com­mon for all con­crete han­dlers. It contains two methods, one for han­dling requests and anoth­er for set­ting the next han­dler on the chain

declares the inter­face, com­mon for all con­crete han­dlers. It contains two methods, one for han­dling requests and anoth­er for set­ting the next han­dler on the chain Base Han­dler has code that’s com­mon to all han­dler classes

has code that’s com­mon to all han­dler classes Con­crete Han­dlers con­tain the actu­al code for pro­cess­ing requests. Upon receiv­ing a request, each han­dler must decide whether to process it and, addi­tion­al­ly, whether to pass it along the chain

Classes and Objects

The classes for our design will be:

Handler — The interface

The interface MafiaHandler — Base handler

Base handler Classes for concrete handlers — Godfather, Underboss, Consigliere, Caporegime, Soldier

Class diagram for the system

The Code

Classes to represent the handler interface and base MafiaHandler

Concrete handler classes for various handlers in the hierarchy to serve requests

This is how this system will function

# Create a handler instances

godfather = Godfather.new

underboss = Underboss.new

consigliere = Consigliere.new

caporegime = Caporegime.new

soldier = Soldier.new # Create the chain of responsibility

godfather.

next_handler(underboss).

next_handler(consigliere).

next_handler(caporegime).

next_handler(soldier) underboss.handle(:recruit_soldier)

# => Caporegime recruits a soldier godfather.handle(:attend_meeting)

# => "Consigliere represents the Godfather at meeting with another Mafia family"

References