Skip to main content

πŸ§ͺ🧠 TDD vs BDD & πŸ€” Why Interviewers Ask Which Design Pattern Are You Using?

πŸ§ͺ🧠 TDD vs BDD & πŸ€” Why Interviewers Ask “Which Design Pattern Are You Using?”

Two interview questions that look simple… but silently decide your fate. πŸ˜„☕

If you’re a Java / Spring Boot developer and you’ve attended even 2–3 interviews, you’ve definitely heard these questions:

  • “Are you using TDD or BDD?”
  • “What design pattern are you using in your project?”

And suddenly your brain goes like…

🧠 “Wait… we are just writing code da… what pattern??”

Don’t worry. This blog will spoon-feed you the answer πŸ‘ΆπŸ₯„ — slowly, clearly, and in an interview-safe way.


πŸ§ͺ Question 1: TDD vs BDD – What are you really doing?

🀯 Dumb Question:

“Both are testing… then why two names?”

πŸ’‘ Brilliant Answer (Baby Explanation):

Think like this πŸ‘‡

TDD BDD
Developer talking to code πŸ§‘‍πŸ’»➡️πŸ’» Business talking to system πŸ§‘‍πŸ’Ό➡️πŸ–₯️

πŸ”΅ TDD – Test Driven Development πŸ§ͺ

πŸ‘Ά One-line concept:
Write test first ❌, then write code ✔, then clean it πŸ”„



@Test

void shouldCalculateTotalAmount() {

    OrderService service = new OrderService();

    assertEquals(100, service.calculateTotal());

}



public int calculateTotal() {

    return 100;

}

✔ Developer focused
✔ Unit testing
✔ Clean design


🟒 BDD – Behavior Driven Development 🎭

πŸ‘Ά One-line concept:
Describe behavior in human language, then automate it



Scenario: Successful order placement

Given user is logged in

When user places an order

Then order should be created



@When("user places an order")

public void placeOrder() {

    orderService.placeOrder();

}

✔ Business friendly
✔ Acceptance testing
✔ Common in Cucumber


πŸ€” Question 2: Which Design Pattern are you using?

😨 Dumb Question:

“We didn’t sit and choose pattern… then what to answer?”

πŸ’‘ One-line truth:
You are already using design patterns — Spring forces you to πŸ˜„

πŸ‘‘ Singleton Pattern



@Service

public class PaymentService { }

✔ One instance per container

πŸ”Œ Dependency Injection



@Service

public class OrderService {

  private final PaymentService paymentService;

  public OrderService(PaymentService paymentService) {

    this.paymentService = paymentService;

  }

}

🏭 Factory Pattern

🧠 One-line concept:
Factory means – you ask for an object, factory decides which object to give.

😡 Dumb Question:

“Why can’t I just use new keyword?”

πŸ‘Ά Baby Explanation:

Think like this πŸΌπŸ‘‡

πŸ‘Ά Baby: “I want milk”
🏭 Factory (Mom): decides cow milk or formula

Baby doesn’t care HOW milk is made. Baby just drinks πŸ˜„

☕ Normal Java (Without Factory) ❌



PaymentService service = new CreditCardPaymentService();

❌ Tight coupling
❌ Code change needed everywhere

☕ Factory Pattern (Manual Way) ✅



public class PaymentFactory {

    public static PaymentService getPayment(String type) {

        if ("CARD".equals(type)) {

            return new CreditCardPaymentService();

        } else if ("UPI".equals(type)) {

            return new UpiPaymentService();

        }

        throw new IllegalArgumentException("Invalid type");

    }

}



PaymentService service = PaymentFactory.getPayment("CARD");

✔ Loose coupling
✔ Centralized object creation

🀯 How Spring does Factory internally

πŸ‘‰ ApplicationContext is a BIG factory



PaymentService service = context.getBean(PaymentService.class);

Spring decides:

  • Which implementation
  • When to create
  • How many instances


🧩 Strategy Pattern

🧠 One-line concept:
Same job, different strategies — choose at runtime.

πŸ‘Ά Baby Explanation:

🍼 Baby wants to go to school:

  • Bus 🚌
  • Auto πŸ›Ί
  • Cycle 🚲

Destination same, travel strategy different.

☕ Java Example



interface PaymentStrategy {

    void pay(int amount);

}



class CardPayment implements PaymentStrategy {

    public void pay(int amount) { }

}



class UpiPayment implements PaymentStrategy {

    public void pay(int amount) { }

}

Spring usage: choose implementation using @Qualifier


🧱 Template Method Pattern

🧠 One-line concept:
Fix the steps, allow subclasses to change details.

πŸ‘Ά Baby Explanation:

🍼 Making tea ☕:

  1. Boil water
  2. Add base ingredient
  3. Add extras

Steps same, ingredients change.

☕ Java Example



abstract class DataProcessor {

    public final void process() {

        read();

        validate();

        save();

    }

    abstract void read();

}

Spring usage: JdbcTemplate, RestTemplate


πŸ‘€ Observer Pattern

🧠 One-line concept:
One change, many listeners notified.

πŸ‘Ά Baby Explanation:

🍼 Baby cries 😭 → Mom πŸ‘©, Dad πŸ‘¨, Grandma πŸ‘΅ all react.

☕ Java Example



@EventListener

public void handleOrderEvent(OrderCreatedEvent event) {

}

Spring usage: ApplicationEventPublisher, Kafka listeners


πŸ”„ Question 3: @Transactional – Class Level or Method Level?

🧠 One-line concept:
Always prefer @Transactional at method level.



@Service

@Transactional

public class OrderService { }

❌ All methods transactional
❌ Performance overhead



@Transactional

public void placeOrder() { }

✔ Fine-grained control
✔ Better performance

⚠️ Interview trap: Self-invocation bypasses Spring proxy



public void methodA() {

  methodB(); // Transaction won't work

}



✨ Wrapping Up

❌ Interviews don’t test memory
✅ They test understanding

If this blog helped you, share it with someone who still fears interviews πŸ˜„πŸ’ͺ

Happy coding & happy interviewing! πŸš€πŸ‘¨‍πŸ’»

Comments

Popular posts from this blog

🐱 Tomcat vs ⚡ Netty – Which One Should You Use?

🐱 Tomcat vs ⚡ Netty – Which One Should You Use? So recently I got curious about this too πŸ€”. Everywhere in Spring Boot tutorials we see Tomcat . Then suddenly while exploring Spring WebFlux , the name Netty pops up. And I was like – “Wait, who’s this Netty guy trying to replace Tomcat?” πŸ˜… Let’s break it down with real-time examples , icons , and fun comparisons . 🐱 Tomcat – The Traditional Web Server Type: Servlet Container (blocking I/O) World: Used with Spring MVC Style: Thread-per-request model πŸ‘©‍πŸ’» Pros: Stable, widely used, battle-tested Cons: Struggles with huge concurrent connections πŸ‘‰ Example in real life: Tomcat is like a restaurant with fixed waiters 🍴. - Each customer = one thread/waiter - If too many customers come in at once → waiters run out → customers wait outside πŸšͺ ⚡ Netty – The Reactive Rockstar Type: Asynchronous Event-Driven Network Framework World: Default for Spring WebFlux Style: Event-lo...

🎭 Spring’s Secret: Why @Transactional & Friends Betray You Silently

πŸ’‘ Lesson Learned — Not a Prod Bug, But a Real Pain No, this wasn’t a production outage. Nobody screamed at me. But I sat for 3 hours wondering: “Why the heck is my @Transactional not rolling back!?” 😡‍πŸ’« “Why is Redis cache not working?” 🀯 Turned out, the issue was one silent villain: 🧱 Self-invocation 🀷 What Is @Transactional ? If you're new: @Transactional = Tells Spring to start a DB transaction when a method is called. It’ll commit if everything’s okay. It’ll rollback if something fails. 🧠 Think of it like wrapping your code in: try { beginTransaction(); // your logic commit(); } catch(Exception e) { rollback(); } πŸ•΅️ Real-Life Analogy — The Gateway Community 🏘️ Let me tell you about my society — it has a strict watchman at the gate. Here’s how it works: πŸ›‚ Watchman = Spring Proxy 🏠 Your apartment = Your service class πŸšͺ Your room = A method inside that class πŸƒ Scenario 1: Outsider Visits Your friend from outside...

🧡 Virtual Threads in Java — The Ultimate Guide with Diagrams, Code & Interview Qs!

πŸš€ “How are Virtual Threads different from Thread Pools?” 😡 “Are they OS threads or JVM threads?” πŸ™ƒ “Should I still use CompletableFuture?” 🀯 “How do I even use them in real-time microservices?” 🧠 What are Virtual Threads? Virtual Threads (introduced in Java 21 as stable πŸŽ‰) are lightweight threads managed by the JVM instead of the OS kernel. πŸ‘‰ They look like normal threads, but don’t hog OS resources like traditional threads. 🧠 What is the OS Kernel? πŸ›️ OS Kernel = The Brain of the Operating System It’s the core part of your OS (Windows, Linux, Mac) that: Manages memory 🧠 Schedules threads πŸ•’ Talks to hardware πŸ’» Handles I/O operations πŸ“¨ When you create a traditional thread in Java, the JVM asks the OS Kernel to create a real OS-level thread. πŸ–Ό️ Imagine This... ┌───────────────────────────┐ │ Your Java Application │ └────────────┬──────────────┘ │ ...