Skip to content

Latest commit

Β 

History

History
106 lines (81 loc) Β· 2.5 KB

File metadata and controls

106 lines (81 loc) Β· 2.5 KB

πŸ’³ Credit Card Factory (Factory Method Pattern)

A practical demonstration of the Factory Method design pattern in C#. This project showcases how to decouple object creation from business logic by leveraging the Factory Method, making your code more maintainable, scalable, and aligned with SOLID principles.


πŸš€ Technologies & Tools

  • C# C#
  • DotNet .NET Console Application

πŸ“ Design Principles & Patterns

  • Factory Method Pattern
    Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

  • SOLID Principles

    • Single Responsibility Principle
    • Open/Closed Principle

πŸ—οΈ Project Structure

🧩 Product (Abstract Class)

Defines the contract for all credit card types.

public abstract class CreditCard
{
    public int Limit { get; set; }
}

πŸͺͺ Concrete Products

  • CreditCardGold (Limit: 5000)
  • CreditCardBlack (Limit: 10000)
  • CreditCardPlatinum (Limit: 15000)
public class CreditCardGold : CreditCard
{
    public CreditCardGold() { Limit = 5000; }
}
// ... other products follow same pattern

🏭 Creator (Factory)

Abstracts the creation process.

public abstract class CreditCardFactory
{
    public abstract CreditCard RequestCreditCard();
}

🏒 Concrete Factories

Each factory produces a specific credit card.

public class GoldFactory : CreditCardFactory
{
    public override CreditCard RequestCreditCard() => new CreditCardGold();
}
// ... other factories follow same pattern

πŸ–₯️ How It Works

The user selects a credit card type, and the appropriate factory creates the corresponding product:

Console.WriteLine("Choose your Credit Card.");
Console.WriteLine("1: Gold\n2: Black\n3: Platinum");
var selected = Console.ReadLine();

CreditCardFactory factory = selected switch
{
    "1" => new GoldFactory(),
    "2" => new BlackFactory(),
    "3" => new PlatinumFactory(),
    _ => null
};

if (factory != null)
{
    var card = factory.RequestCreditCard();
    Console.WriteLine($"Credit Card Type: {card.GetType().Name} | Limit: {card.Limit}");
}

πŸ“¦ Output Examples

Choose your Credit Card.
1: Gold
2: Black
3: Platinum
1

Credit Card Type: CreditCardGold | Limit: 5000