More Untapped Power: Design Pattern
Design patterns are conventional solutions to common challenges in software development. They’re similar to schematic outlines that you can tweak to tackle a reoccurring design issue in your code. For a design pattern to be effective, it needs to solve a problem in theory and proven to work in practice.
Design Patterns are generally grouped into 3:
[1] Creational Design Pattern: promotes the flexibility and reusability of existing code
[2] Structural Design Pattern: illustrates how to combine elements and classes into larger structures while keeping flexibility and efficiency in mind
[3] Behavioral Design Pattern: focuses on the interaction and responsibility of objects
“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice”
— Christopher Alexander
Design Patterns in Badaso LMS
Badaso LMS currently has some design patterns implemented that I have noticed: Singleton, Facade, and Factory. Here’s a little peak on how each of them works and an example of my implementation on the Factory Method.
1| Singleton
Q: What is Singleton Pattern?
A: The Singleton Pattern is a Creational Pattern that lets you create an instance and only that instance is created. That single instance will be accessible globally, but there won’t be another one like it.
Q: When is it useful?
A: When you only need one instance for multiple processes to work and any more creation of such instance will only slow the system.
2| Facade Pattern
Q: What is Facade Pattern?
A: The Facade Pattern is a Structural Pattern that provides a more user-friendly interface to a complex library, framework, or set of classes.
Q: When is it useful?
A: When you require a simple but limited interface to a complicated subsystem or/and when you want to divide a subsystem into layers.
3| Factory Method
Q: What is Factory Method?
A: The Factory Method is a Creational Design Pattern which provides a superclass with an interface for producing things while allowing subclasses to change the type of objects created.
Q: When is it useful?
A: The Factory Method separates the code that builds the product from the code that uses it. As a result, it is easier to extend the product without messing with the rest of the code. All you have to do is build a new creator subclass and override the factory function.
Q: My implementation in Badaso LMS?
A: In Badaso LMS, the Factory method is used for testing. The way we use it is by extending the existing factory class provided by Laravel. This is an example of an implementation I did in the previous sprint.
The MaterialCommentFactory extension of Factory was made to aid unit testing by creating fake MaterialComment objects, here’s a few example of how I used it.
I hope you find this blog helpful, if you find any mistakes or misinformation, I would very much appreciate the feedback. Thank you!
Without Design Patterns?
References
https://sourcemaking.com/design_patterns
https://refactoring.guru/design-patterns
Attached illustrations were made by me :)