A design pattern is a general reusable solution to a
commonly occurring problem within a given context. With the help of design, we
can avoid much error well in advance. Design Patter ensures the best practices
of coding.
The following are a list of design patterns in Apex:
- Singleton
- Strategy
- Decorator
- Facade
- Composite
- Bulk State Transition
Singleton
- Repeated execution of a class within a single transaction may lead to exceed the governor limit. This pattern is to reduce repeated instantiation of a class.
- Problem: Developers often write inefficient code that can cause repeated instantiate of objects. This can result in inefficient, poorly performing code, and potentially the breaching of governor limits. This most commonly occurs in triggers, as they can operate against a set of records.
- Implementation: In order to implement a Singleton pattern in apex, the class must instantiate only once. It is implemented by:
- Creating a class with a method that creates a new instance of the class if it doesn't already exist.
- If it already exists, then simply return a reference to the object.
- For example, use a global static variable, before assigning it to value check if it has already a value.
Strategy:
- The Strategy pattern (aka the policy pattern) attempts to solve the issue where you need to provide multiple solutions for the same problem so that one can be selected at run-time.
- Use Case: You need to provide a geographical-based search engine solution where the implementing code can choose the search provider at run-time.
- Implementation: In Order to implement a Strategy pattern in apex, you need to define a family algorithms. It is implemented by:
- Creating the interface class with methods that will be implemented by other classes
- Covers use of interfaces, Aggregation (Has A) relationship, Method Overloading.
Decorator:
- In some scenarios, we need to show the value only in UI level, but not to store that value in Database. Here we have a Decorator Design pattern to solve this kind of problem. This pattern provides the decorator class which wraps the apex class to provide the extended functionality for the sObject
- Use Case: Whenever the user needs to select the records and process the selected records however select checkbox is not saved.
- Implementation: In order to implement a Decorator pattern in apex, we need to aware that this is not a true implementation the intention is to add the behavior at the run time. It is implemented by:
- Use of wrapper classes inside wrapper class and perform required operation when it is get;set; in vf page instead when the page is loading. We can say it lazy loading. Used in VF page mostly.
Bulk State Transition:
- The bulk state transition requires a trigger that allows only the eligible record that has changed its state and a utility class to implement the logic. The trigger needs to be framed by its events and the DML type.
- Use Case: Whenever a developer needs to write the trigger we always consider as bulk and we use the Bulk State Transition Pattern.
- Implementation: In order to implement a Bulk State Transition in apex, use proper filter to check the eligible records using the Trigger Context variable and call the utility class which contacts the method that handles the operation on the bulk operation.
Facade –
- The intention of the facade design pattern is to convert complex Apex classes into simplified classes or interfaces where groups of classes are either wrapped into one class or only one class is used to delegate responsibilities to the other classes.
- Use Case: Simplifying the execution of classes with complex interfaces (e.g. web service callouts)
- Implementation: Maintainability of Apex code by simplifying the execution of one or complex classes with a facade class. For example, just create a simple class where place code to execute methods/functions of a complex class.
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteDigital Marketing online training
full stack developer training in pune
full stack developer training in annanagar
full stack developer training in tambaram
I think you have a long story to share and i am glad after long time finally you cam and shared your experience.
ReplyDeletepython training institute in chennai
python training in Bangalore
python training in pune
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteData Science training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
ReplyDeletejava training in jayanagar | java training in electronic city
java training in chennai | java training in USA
I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs online Training
angularjs Training in marathahalli
Greetings from Florida! I’m bored at work, so I decided to browse your site on my iPhone during lunch break.
ReplyDeletenebosh igc courses in chennai
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Java Script online training
Share Point online training
Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
ReplyDeleteData science Course Training in Chennai | Data Science Training in Chennai
RPA Course Training in Chennai | RPA Training in Chennai
AWS Course Training in Chennai | AWS Training in Chennai
Devops Course Training in Chennai | Best Devops Training in Chennai
Selenium Course Training in Chennai | Best Selenium Training in Chennai
Java Course Training in Chennai | Best Java Training in Chennai
Web Designing Training in Chennai | Best Web Designing Training in Chennai
Thanks for sharing this great article. It made me understand few things about this concept which I never knew before. Keep posting such great articles so that I gain from it. Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai
ReplyDeleteGreat post very useful info thanks for this post ....
ReplyDeletedata science training chennai | data science class in chennai
Blueprism training in chennai | blueprism training class chennai