🎓AI Code vs Design Patterns

🧠 Big Idea (Start Here)

AI writes code that looks like design patterns.
But you decide if it is a good design pattern.

👉 Think of it like this:

  • AI = a fast code generator
  • You = the software architect

🏗️ Analogy Students Understand

Imagine building a house:

  • Design Patterns → blueprints created by expert architects
  • AI Code → a builder who has seen thousands of houses and builds something similar

💡 The builder (AI) can build fast…
⚠️ But may not always choose the right design


🔁 Example 1: Factory Pattern (C++)

🎯 Problem:

We want to create objects without hardcoding new everywhere.


🧱 What a Student Learns (Intentional Design)

class Shape {
public:
    virtual void draw() = 0;
};

class Circle : public Shape {
public:
    void draw() override { std::cout << "Circle\n"; }
};

class Square : public Shape {
public:
    void draw() override { std::cout << "Square\n"; }
};

class ShapeFactory {
public:
    static Shape* createShape(std::string type) {
        if (type == "circle") return new Circle();
        if (type == "square") return new Square();
        return nullptr;
    }
};

👉 Student understands:

  • Encapsulation of creation
  • Flexibility for future shapes

🤖 What AI Might Generate

Shape* getShape(std::string type) {
    if (type == "circle") return new Circle();
    else if (type == "square") return new Square();
    return nullptr;
}

👉 Looks similar, right?

But ask students:

  • Where is the pattern name?
  • Where is the intent?
  • Is this scalable?
  • Where would we extend it?

💥 This is the teaching moment.


🔄 Example 2: Strategy Pattern (JavaScript)

🎯 Problem:

We want to change behavior at runtime.


🧱 Intentional Design

class PaymentStrategy {
    pay(amount) {}
}

class CreditCard extends PaymentStrategy {
    pay(amount) {
        console.log("Paid with credit card:", amount);
    }
}

class PayPal extends PaymentStrategy {
    pay(amount) {
        console.log("Paid with PayPal:", amount);
    }
}

class ShoppingCart {
    constructor(strategy) {
        this.strategy = strategy;
    }

    checkout(amount) {
        this.strategy.pay(amount);
    }
}

🤖 AI Version (Typical)

function pay(type, amount) {
    if (type === "card") console.log("Paid with card");
    else if (type === "paypal") console.log("Paid with PayPal");
}

💥 Ask Students:

  • Which one is easier to extend?
  • Which one avoids big if statements?
  • Which one follows Open/Closed Principle?

🔍 What AI Is Actually Doing

AI is basically saying:

“I’ve seen this pattern before… here’s something similar.”

But it may:

  • ❌ Skip abstraction
  • ❌ Mix responsibilities
  • ❌ Forget scalability
  • ❌ Create “spaghetti if-else logic”

⚖️ Side-by-Side Comparison

FeatureDesign PatternsAI-Generated Code
Intent✅ Clear❓ Often unclear
Naming✅ Standard (Factory, Observer…)❌ Usually missing
Structure✅ Clean & scalable⚠️ Sometimes messy
Learning Value✅ Teaches architecture⚠️ Teaches shortcuts
Reliability✅ Proven❓ Needs review

🎯 The Golden Rule (Teach This Line)

👉 “AI gives you code. Design patterns give you control.”


🧪 Classroom Exercise (Very Effective)

Give students AI-generated code and ask:

  1. What pattern is this closest to?
  2. Is it implemented correctly?
  3. How would you improve it?
  4. Can you refactor it into a real pattern?

💡 This turns AI into a learning tool, not a crutch.


🎤 How You Might Say It in Class

“AI is like a student who memorized answers but doesn’t always understand the question.
Design patterns are how we understand the question.


🧠 Final Takeaway

  • AI can imitate patterns
  • Only you can apply them correctly

👉 That’s why learning design patterns still matters—even more in the age of AI.


Leave a Reply

Your email address will not be published. Required fields are marked *