Skip to content

nalama1/design-patterns-python

Factory Method Pattern in Python

This project implements the Factory Method design pattern in Python, demonstrating how to create objects of various game types (Chess, Solitaire) without tightly coupling the client code to concrete classes.

image

⬆️ Back to top

Factory Method Pattern

To clearly represent the Factory Method pattern with the following classes:
🧍‍♂️ Client (FactoryMethodClient)
🧱 Abstract factory (Board)
🧩 Concrete factories (ChessBoard, SolitaireBoard)
🎮 Abstract product (Game)
🕹 Concrete products (ChessGame, SolitaireGame)
💡 How to interpret it:
🧱 Board is abstract → it defines the create_game() method but does not implement it.
🧩 ChessBoard and SolitaireBoard are concrete factories, which return instances of ChessGame or SolitaireGame.
🎮 Game is the abstract base class for all games.
🧍‍♂️ FactoryMethodClient represents the client that initiates the entire process.
ILogger is an interface for logging (implemented by Logger).

⬆️ Back to top

🚀 Objective

To apply the Factory Method pattern to:

  • Create games in a flexible and decoupled manner.
  • Incorporate new functionalities (e.g., new game types) without modifying existing client code.
  • Practice good Git workflow (branching, Pull Requests, rebase, merges, logging).

⬆️ Back to top

🏗️ Project Structure

  • factory_method/
    • audit/ (Module for logging functionality)
      • ILogger.py
      • Logger.py
    • client/ (Main client / entry point)
      • FactoryMethodClient.py
    • factory/ (Base and concrete Factory classes)
      • Board.py (Abstract Factory)
      • ChessBoard.py (Concrete Factory)
      • SolitaireBoard.py (Concrete Factory)
    • products/ (Product classes / specific game types)
      • ChessGame.py (Concrete Product)
      • Game.py (Abstract Product)
      • SolitaireGame.py (Concrete Product)
    • README.md

⬆️ Back to top

🧠 Core Features

Feature Description
Dynamic Game Creation Instances are created without the client knowing the concrete class (Factory Method implementation).
Game Types Supported Supports ChessGame, SolitaireGame
Configurable Difficulty Allows selection between easy, normal, and hard difficulties.
Timer/Duration Tracking Records game duration and move counts.
Logger Integration Logs activity and results to audit/factory_logs.txt.

⬆️ Back to top

🧰 Technologies and Standards

  • Python 3.10+
  • Factory Method Design Pattern
  • PEP8 (Code conventions)
  • File handling (os, datetime modules)
  • Git and GitHub (Professional branching and PR workflow)

⬆️ Back to top

🔀 Git Development Workflow

No. Command/Action Action/Description
1 Git checkout main Switch to the main branch (the stable branch of the project).
2 Git fetch --prune Download changes from the remote repository and remove local references to branches that no longer exist on the remote.
3 Git pull --rebase origin main Synchronize the local main branch with the remote, downloading the latest changes and rebase local commits, if any, to maintain a clean, linear history.
4 Git status Show the status of the working tree and the staging area (modified, added files, etc.).
5 Git checkout -b nueva-rama Create a new branch called nueva-rama (new-branch) and switch to it immediately.
6 Code Perform changes, implement new features, or fix bugs in the files.
7 Git add . Add all modified and new files in the current working directory to the staging area for the next commit.
8 Git commit -m Message Save the changes from the staging area to the history of the current branch with a descriptive message.
9 Git pull --rebase origin main Update the working branch (nueva-rama) with the latest changes from main before pushing, using rebase so that your own commits come after main's commits.
10 Git push origin nueva-rama Upload the local nueva-rama branch and all its commits to the remote repository.
11 Create Pull Request Formally request the merge of nueva-rama into main through the repository interface (GitHub, GitLab, etc.).
12 Git branch Verify that you are on the newly created branch.
13 Git status Check the status of the branch.
14 Git pull --rebase origin main Update the working branch with the latest changes from main (in case main was updated while waiting for approval).
15 Pull Request approved The PR has been approved and the merge of nueva-rama into main has been performed on the remote repository. The new branch is also deleted from the remote repository.
16 Git checkout main Return to the main branch to synchronize.
17 Git branch Verify the branches.
18 Git status Check the status of the branch.
19 Git fetch --prune Update the list of remote branches and remove the local reference to nueva-rama if it was deleted on the remote after the merge.
20 Git pull --rebase origin main Download the merge commit (or the rebased commits) that was performed on the remote.
21 Git status Verify that the working tree is clean.
22 Git branch -d nueva-rama Delete the nueva-rama branch locally (this only works if the branch has already been merged).

⬆️ Back to top

🧱 Applied Design Principles

  • Factory Method: Creates objects without exposing the instantiation logic to the client.
  • Single Responsability Principle (SRP): Each module and class has a clear, defined responsibility.
  • Open/Closed Principle (OCP): The system is open for extension (new game types) but closed for modification (client code remains unchanged).

⬆️ Back to top


🚀 Getting Started

These instructions will help you set up and run the project on your local machine.

🧩 Prerequisites

Make sure you have the following installed on your system:

  • Python 3.10+ (or your exact project version)
  • Git (for cloning the repository)
  • Poetry (recommended Python dependency manager). If you don’t have Poetry, install it globally:
pip install poetry

⬆️ Back to top


⚙️ Installation & Setup

Follow these steps to set up the environment and install dependencies:

1️⃣ Clone the Repository

Replace the URL with your GitHub project link:

git clone [YOUR_GITHUB_REPOSITORY_URL]

2️⃣ Change Directory

cd [your-project-folder-name]

3️⃣ Install Dependencies

This command creates a virtual environment and installs all required libraries:

poetry install

4️⃣ Activate the Virtual Environment

poetry shell

⬆️ Back to top


▶️ Usage

Once the environment is active, run the main client file to see the Factory Method Pattern in action:

python -m client.FactoryMethodClient

⬆️ Back to top


🛠️ Built With

Python Poetry Git GitHub PyCharm VS Code PlantUML Markdown MIT License

⬆️ Back to top


📷 Capture images of program from console (Terminal PyCharm)

image

Selecting the option one:

image

Selecting the option two:

image

Selecting the option three:

image

Selecting the option zero:

image

⬆️ Back to top


⚙️ Board and Game Creation Process

Game Initialization and Creation Flow

image

⬆️ Back to top


Educational and practical project demonstrating clean architecture and design patterns in Python.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages