Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Strategy Pattern — T-Shirt Demo

An interactive command-line application demonstrating the Strategy design pattern through simulated payment methods.

Features

  • Select multiple colors, sizes, and fabrics.
  • Validate that each selection contains at least one option.
  • Choose credit/debit card, bank transfer, or cash.
  • Execute only the selected payment strategy.
  • Handle cancellation gracefully.
  • Run locally or with Docker.
  • Colored terminal banner and step-by-step menus.
  • Formatted selection summary using Rich.
  • Confirmation before running the payment demo.
  • Start another selection without restarting the application.

This is an educational demo. No real payments are processed.

Selections are lists of preferences. The application does not calculate prices, quantities, or individual product combinations.

Strategy Pattern

PaymentStrategy defines the common pay() interface.

CardPayment, BankTransferPayment, and CashPayment implement that interface.

PaymentContext delegates execution to its selected strategy. The strategy can be replaced through set_strategy().

The CLI selects the strategy after the user chooses a payment method.

Files

  • app.py: interactive menus and application entry point.
  • payments.py: payment strategies and context.
  • requirements.txt: Python dependencies.
  • Dockerfile: container image definition.
  • docker-compose.yml: interactive container configuration.
  • init.sh: optional Linux/macOS launcher.

Local setup — Windows PowerShell

Use Python 3.11 for consistency with the Docker image.

From the project root:

py -3.11 -m venv venv
.\venv\Scripts\Activate.ps1

python -m pip install -r requirements.txt
python app.py

Python 3.11 must be installed for the py -3.11 command.

For Windows Command Prompt, activate with:

venv\Scripts\activate.bat

Local setup — Linux / macOS

With Python 3.11 installed:

python3.11 -m venv venv
source venv/bin/activate

python -m pip install -r requirements.txt
python app.py

Docker

Install Docker with Docker Compose support, then run:

docker compose run --build --rm python

To run the existing image again:

docker compose run --rm python

Rebuild after changing application code or dependencies.

Controls

  • Arrow keys: navigate.
  • Space: toggle checkbox selections.
  • Enter: continue.
  • Ctrl+C: cancel.

Run the application in a terminal, such as the VS Code integrated terminal, PowerShell, or a Linux/macOS terminal.

About

Interactive Python CLI demonstrating the Strategy design pattern through a T-shirt selection demo. Features a Rich terminal UI, three simulated payment methods, JSON result storage, and Docker support.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages