Factory Method: Implementation and Example

Implementing the Factory Method pattern

In our example, we’ll build an application capable of playing audio on either Windows or Linux platforms. Because these two operating systems have completely different APIs, they require different codes and our application needs to be able to work on both of them.

To enable this, we need two distinct implementations of the audio player functionality. We need a separate implementation for Windows and a separate one for Linux. The Factory Method pattern is perfect for this scenario.

Creating a console application

For demonstration purposes, we’ll create a console application project. Inside this project, we’ll add the Player.cs file with the following code:

Get hands-on with 1400+ tech skills courses.