Table of contents
No headings in the article.
Imagine you are enjoying your summer vacation, and you went into the kitchen to make a glass of "Lemonade". While preparing your drink you mistakenly added an extra quantity of lemon than required. Now you try many things to make the drink taste like a perfect glass of lemonade. At last, when you get tired of it, you reach out to your one-stop solution, MOM!! (or DAD!! cuz why not?😆). She fixes the drink in an instant because she had encountered such a problem and now she knows how to fix it. Yes, we are talking about Design Patterns.
Now, What exactly is a Design Pattern?
A Design Pattern is a description or template for how to solve a problem that can be used in many different situations.
It is a general repeatable solution to a commonly occurring problem in software design.
These Design Patterns can be improved over time, according to the need of a general problem.
There are many types of Design Patterns namely:
Creational Design Pattern
Structural Design Pattern
Behavioral Design Pattern
Let's discuss some of the Design Patterns that developers face during Android Development (Kotlin).
Singleton Pattern :
• Most of the time while fetching data from the backend we call an endpoint more than once, as we all are aware that fetching something online is a costly task, so we need to have only one instance of our class for ex. a single Database connection shared by multiple objects as creating a separate database connection for every object may be costly.
• Singleton Design Pattern restricts the user to instantiate only a single object of such a Class.
• For example: If I'm making an API request for fetching data using Retrofit, I'll make a Singleton Object like :
Adapter Pattern:
• Most Mac users might have come across problems where they have to connect a USB but they don't have a port for it, so they will buy an Adapter that will have a Type C cable to connect with Mac laptops. Here the Adapter acts as an agent.
• So, The adapter pattern converts the interface of a class into another interface clients expect. The adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
There are many other Design Patterns such as:
Factory
Builder
Facade
Dependency Injection
These Design Patterns have been developed for software developers to ease out the developing process. Design patterns can speed up the development process by providing tested, proven development paradigms. Learning design patterns helps to prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar with the patterns.