The Object Oriented Programming (OOP) concept of Abstraction allows us to only show the required details, such as methods, functions, etc. and hide the unnecessary implementations from the calling method.
Below I utilize a simple example of how we use this concept in our programming to create cleaner, reusable code:
Abstraction
When I’m sending email to a user via the web application, I utilize a SendMailMessage function that actually sends the mail, passing in the toEmail,
the subject, the body, the fromEmail, etc.
The SendMailMessage calls other methods within itself. But the methods are set to private, so the calling method that uses SendMailMessage doesn’t see or have access to those methods directly.
If we change values within those private methods, it doesn’t affect the calling class at all.
This keeps it clean by hiding the unnecessary details of the called class.
Public Shared Function SendMailMessage(ByVal toEmail As String,
ByVal subject As String, ByVal body As String,
Optional ByVal fromEmail As String = "",
Optional ByVal bcc As String = "",
Optional ByVal cc As String = "",
Optional ByVal checkServerMode As Boolean = True,
Optional ByVal attachmentFileName As String = "") As String
...
The SendMailMessage calls a protected method that actually instantiates the credentials and sends the mail.
In my code, the called method has changed 3 times during the course of using the SendMailMessage function, but this doesn’t affect the functions that call it, because it’s all handled internally.
Previously, I used: SendUsingSMTPClient and SendUsingMailGunClient. But now I use SendGrid, so a new function was created, SendUsingSendGrid, to use those required credentials.
So within my SendMailMessage function, I can change called methods, I can change the way validations are handled,
I can add code to alter the way it operates such as if it is LocalHost versus remote, all unbeknownst to the calling methods that use it.
...
msg = SendUsingSendGrid(mMailMessage)
...
The actual method initializes the credentials and sends the mail, returning any exception errors:
Protected Shared Function SendUsingSendGrid(ByVal mMailMessage As MailMessage) As String
...