0% found this document useful (0 votes)
3 views8 pages

Effective Design Patterns for Code Optimization

Uploaded by

kapil.agarwal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Effective Design Patterns for Code Optimization

Uploaded by

kapil.agarwal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Design Patterns in Action

Our List Management & Beyond


Knowledge Sharing Session by Kapil
Builder Pattern 🧱
• Problem: Messy conditional SQL joins
• Before:
– $query = DB::table('contacts');
if ($hasProfile) {
$query->join('profiles', 'profiles.contact_id', '=', '[Link]');
}
if ($hasInvestments) {
$query->join('investments', 'investments.user_id', '=', '[Link]');
}
• After:
– $query = (new ContactListBuilder())
->joinProfile()
->joinInvestments()
->applyFilters($filters)
->get();
• ✨ Benefit: Cleaner, reusable, and extendable query building
Strategy Pattern 🎯

• Problem: Different list modes required if/else jungle


• Before:
– if ($mode === 'manual') {
$this->buildManualList($contacts);
} elseif ($mode === 'static') {
$this->buildStaticList($contacts);
} elseif ($mode === 'dynamic') {
$this->buildDynamicList($contacts);
}
• After:
– $context = new ListContext(new DynamicListStrategy());
$contacts = $context->build($filters);
• ✨ Benefit: Removes if/else jungle and makes it easy to add new strategies
Rule Object Pattern ⚖️

• Problem: Complex inclusion/exclusion criteria


• Before:
– if ($contact->investment > 100000 && $contact->country == 'IN') {
$eligible[] = $contact;
}
• After:
– $rules = [
new InvestmentRule(100000),
new CountryRule('IN')
];

$engine = new RuleEngine($rules);


$eligible = $engine->apply($contacts);
• ✨ Benefit: Rules are independent, testable, and easy to extend
DTO Pattern 📦

• Problem: Passing raw arrays/models


everywhere
• Before:
– return [
'id' => $contact->id,
'name' => $contact->full_name,
'email' => $contact->email,
];
• After:
– class ContactDTO {
Abstract Factory Pattern 🏭

• Problem: Different service providers


(AWS/Netcore) for SMS/Email
• Before:
– if ($provider === 'AWS') {
$service = new AwsSmsService();
} else {
$service = new NetcoreSmsService();
}
• After:
– $factory = ProviderFactory::create('AWS');
Key Takeaways 🚀

• Patterns helped reduce complexity & improve


testability
• Codebase became modular & maintainable
• Easier for new team members to onboard
• Patterns solved our real project problems
Q&A 💬
• 👉 Open floor for doubts & discussion

You might also like