Skip to main content

6 posts tagged with "structural-patterns"

View All Tags

Flyweight pattern

· 4 min read
Esteban Gonzalez
Collaborator

Example

This image is part of the game この素晴らしい世界に祝福を! Attack of the Destroyer developed by Entergram

The objective of this post is to explain and show how to implement the Flyweight Pattern in a basic way and see how this pattern can help you to reduce the memory usage of your applications.

Adapter pattern

· 3 min read
Johnatan Garzon
Collaborator

banner

The objective of this post is to explain and show how to implement the Adapter Pattern in a basic way.

Pre-requisites

Check all the description and information related to the Adapter Pattern and return here to see a practical example.

Description

You're an avid gamer with a collection of controllers—Xbox, PlayStation, Nintendo, you name it. Each controller speaks a different "language" with unique button mappings and input methods. Your game, however, expects a single, unified interface to process these inputs. How can you make your Xbox controller work effortlessly with a game that was originally designed for a different controller?

The solution

The Xbox Controller Adapter bridges the gap between the diverse Xbox controller interface and the standardized game controller interface expected by your game. By implementing the Adapter Pattern, we create a seamless translation layer that allows the Xbox controller to communicate perfectly with your game.

How It Works:

1. Define the Game Controller Interface:

We start by defining a universal interface, IGameController, that includes the basic functionalities any game controller should have—button presses and joystick movements.


public interface IGameController
{
void SendKey(char key);
}

2. Adapting the Xbox Controller:

The Xbox controller has its unique methods for handling input. Our XboxControllerAdapter steps in to translate these unique methods into the standard IGameController interface.


public class XboxAdapter : IGameController
{
private readonly XboxController _adaptee;
public XboxAdapter(XboxController adaptee)
{
this._adaptee = adaptee;
}
public void SendKey(char key)
{
string button = "";
switch (key)
{
case 'w':
button = "UP";
break;
case 's':
button = "DOWN";
break;
case 'a':
button = "LEFT";
break;
case 'd':
button = "RIGHT";
break;

}
_adaptee.PressButton(button);
}
}


public class XboxController
{
public void PressButton(string button)
{
Console.WriteLine(button);
}
}

3. Seamless Integration:

With the adapter in place, you can now use your Xbox controller as if it was designed for the game’s original controller interface. The adapter ensures all inputs are correctly translated and processed.


public class Game
{
private readonly IGameController _gameController;

public Game(IGameController gameController)
{
_gameController = gameController;
}
public void Play()
{
_gameController.SendKey('a');
_gameController.SendKey('w');
}
}

class Program
{
public static void Main()
{
XboxController xbox = new XboxController();
IGameController gameController = new XboxAdapter(xbox);

Game game = new Game(gameController);
game.Play();
}
}


In order to see the result of the code above, you can run the following code snippet:

Class Diagram

Proxy pattern

· 5 min read

The objective of this post is to explain and show how to implement the Proxy Pattern in a basic way.

Pre-requisites

Check all the description and information related to the Proxy Pattern and return here to see a practical example.

Description: The Magical World

Welcome to a magical world where wizards wield powerful spells from enchanted spellbooks. Each spellbook is a treasure trove of mystical knowledge, containing spells capable of performing wonders like summoning fireballs, controlling the weather, or even teleporting across vast distances. However, these spells come with certain conditions. A wizard needs sufficient mana (magical energy) to cast a spell, and some spells require the wizard to solve ancient riddles before they can be used.

Composite pattern

· 4 min read

mario

The objective of this post is to explain and show how to implement the Composite Pattern in a basic way.

Pre-requisites

Check all the description and information related to the Composite Pattern and return here to see a practical example.

Description

Imagine you're developing a game where players can build and customize their own game levels. Each level can contain various elements like buildings, trees, and enemies. Some elements are simple (like a single tree), while others are complex (like a fortress composed of walls, towers, and gates). Players should be able to interact with these elements uniformly, whether they are single items or complex structures.

Façade pattern

· 5 min read
Esteban Gonzalez
Collaborator

The objective of this post is to explain and show how to implement the Façade Pattern in a basic way.

Pre-requisites

Check all the description and information related to the Façade Pattern and return here to see a practical example.

Description

Example

Image taken from GeekForGeeks

Imagine that you want to process multimedia like videos and images using FFmpeg.

During your development you found that your library just executes FFmpeg and run a command, but your clients don't like this idea because they manually need to create the command and make all the validations to the file, for example you want you join two videos intro.mp4 and ending.mp4 but you need to validate that they have the same codecs, the same resolution, the output file result.mp4 cannot exists, and the destination path where the program is going to save the file has the right permissions (read and write).

Bridge pattern

· 4 min read
Esteban Gonzalez
Collaborator

GraphicsApi

Image taken from ReplayBird

The objective of this post is to explain and show how to implement the Bridge pattern

Pre-requisites

Check all the description and information related to the Bridge Pattern and return here to see a practical example.

Description

Imagine that you are creating a new graphics engine and this engine needs to be able to run on the different operating systems Like Windows, Linux and OSX.

As you delve deeper into the development process, you encounter a significant hurdle: the diverse Graphics APIs supported by each operating system. Your initial approach involves creating separate classes to implement the functionality for each Graphics API, resulting in DirectxApi, OpenGlApi, and VulkanApi. However, as your codebase grows, maintaining these distinct classes becomes increasingly unwieldy.

With the Bridge Pattern, you're presented with an elegant solution to this conundrum. By adopting the Bridge Pattern, you can effectively separate the abstraction of the graphics functionality from the platform-specific implementations. Let's explore how the Bridge Pattern transforms your approach and mitigates the challenges of managing a growing codebase.