π§ͺπ§ 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 ☕:
- Boil water
- Add base ingredient
- 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
Post a Comment