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

πŸ” Is final Really Final in Java? The Truth May Surprise You 😲

πŸ’¬ “When I was exploring what to do and what not to do in Java, one small keyword caught my eye — final . I thought it meant: locked, sealed, frozen — like my fridge when I forget to defrost it.”   But guess what? Java has its own meaning of final… and it’s not always what you expect! πŸ˜… Let’s break it down together — with code, questions, confusion, jokes, and everything in between. 🎯 The Confusing Case: You Said It's Final... Then It Changed?! 🫠 final List<String> names = new ArrayList <>(); names.add( "Anand" ); names.add( "Rahul" ); System.out.println(names); // [Anand, Rahul] 🀯 Hold on... that’s final , right?! So how on earth is it still changing ? Time to dive deeper... 🧠 Why Is It Designed Like This? Here’s the key secret: In Java, final applies to the reference , not the object it points to . Let’s decode this like a spy mission πŸ•΅️‍♂️: Imagine This: final List<String> names = new ArrayList <>(); Be...

🌟 My Journey – From Zero to Senior Java Tech Lead 🌟

 There’s one thing I truly believe… If I can become a Java developer, then anyone in the world can. πŸ’― Sounds crazy? Let me take you back. πŸ•“ Back in 2015… I had zero coding knowledge . Not just that — I had no interest in coding either. But life has its own plans. In 2016, I got a chance to move to Bangalore and joined a Java course at a training center. That’s where it all started — Every day, every session made me feel like: "Ohhh! Even I can be a developer!" That course didn’t just teach Java — it gave me confidence . πŸ§ͺ Two Life-Changing Incidents 1️⃣ The Interview That Wasn't Planned Halfway through my course, I had to urgently travel to Chennai to donate blood to a family member. After that emotional rollercoaster, I found myself reflecting on my skills and the future. The next day, as I was preparing for my move to Bangalore to complete the remaining four months of my course, I randomly thought — "Let me test my skills... let me just see...

🎒 Java Loops: Fun, Fear, and ForEach() Fails

πŸŒ€ Oops, I Looped It Again! — The Ultimate Java Loop Guide You Won't Forget “I remember this question from one of my early interviews — I was just 2 years into Java and the interviewer asked, ‘Which loop do you prefer and why?’” At first, I thought, “Duh! for-each is cleaner.” But then he grilled me with cases where it fails. 😡 That led me to explore all loop types, their powers, and their pitfalls. Let’s deep-dive into every major Java loop with examples &  real-world guidance so you'll never forget again. πŸ” Loop Type #1: Classic For Loop — “The Old Reliable” ✅ When to Use: You need an index You want to iterate in reverse You want full control over loop mechanics ✅ Good Example: List<String> names = List.of("A", "B", "C"); for (int i = 0; i < names.size(); i++) { System.out.println(i + ": " + names.get(i)); } πŸ”₯ Reverse + Removal Example: List<String> item...