Welcome to part two of Object Oriented Principles in Typescript!Encapsulation- Object Oriented Principles in TypescriptWriting Better Applications in Typescriptmedium.
comWhat is Abstraction?Abstraction is another one of the simpler concepts when it comes to the four principles of OOP.
It is the process of hiding the internal complexity of a class while only requiring the absolute necessary data to function correctly.
The goal behind abstractionThrough abstraction, our goal is to make our source code as simple and easy to use as possible for our clients so that way they can focus on handling their complexity only and not to juggle back and forth with ours also.
Sounds like its time to hop into an example.
So the Stake Holders Wants UpdatesIf you remember from last time, our banking application was very simple and we even got our first bank member, Chuck.
But we just got a word in that very soon our banks stake holders want to be more competitive in the banking scene and wants to offer more than just a savings account by the end of the first quarter.
But this implementation will be quite difficult given our current circumstances.
Taking a close look over our code existing code base, we’ve too tightly coupled our members savings account directly to the BankMember class.
Take a quick look.
The BankMembers savings balance is tied directly into the class itself where there’s no way to properly add any further functionality.
I’m starting to smell multiple responsibilities here, and it smells fishy if you ask me…Now, though we aren’t here to directly talk about decoupling your code, decoupling is a great way to understand the basis of abstraction so we’ll continue down this road for now.
Creating the Savings Accounts ClassNow understanding the flaws of our existing code base, let’s fix it up with a brand new SavingsAccount class that our BankMember can use.
Very simple, now we can put this class in charge of handling the responsibilities of any savings account actions while the BankMember can focus on, well, the BankMember.
Now lets go ahead and set up our BankMember class to utilize our new SavingsAccount.
Since the last article on encapsulation, we’ve enhanced the BankMember class to take in the members name and randomly generate an id for that member just to seem more authentic lol.
Take a close look at the subtle moves we’ve made here.
We’ve composed our BankMember class with a SavingsAccount instance variable that will handle that members savings account actions.
And most importantly, we exposed a public method that gives our members the ability to create their savings accounts.
We also threw in a little helper function getSavingsBalance() just in case they were curious :)“…our goal is to make our source code as simple and easy to use as possible for our clients so that way they can focus on handling their complexity only and not to juggle back and forth with ours also.
”Running the New CodeGreat!.Let’s hook this up in our index.
ts file and give it a run.
Awesome!.We’ve successfully decoupled our savings account from our bank member, introduced a bit of composition and even a pinch of delegation!.All of which are seemingly more advanced subjects.
And most importantly, it works!But not so fast, lets stop all the celebration for a second and take a look at our index file… It’s seems to me that it’s doing something complex that it shouldn’t have to worry about… Remember what abstraction is?…the process of hiding the internal complexity of a class while only requiring the absolute necessary data to function correctly.
But yet we put what should have been the complexity of our BankMember class into the hands of ALL of our clients!.(in this case our index file) And this will become a huge hiccup in our code as we begin to expand even larger.
We did a horrible job at hiding the internals of our class, therefore, lets go ahead and fix that issue before we get more details about our coming updates.
Go ahead and compare this new version of our createSavingsAccount() method with the old.
We no longer require an argument of type SavingsAccount, the only thing we require from our clients is the absolute necessary data for the method to function properly, and in this case it’s the starting balance that our members would like to open a savings account with!.Now Look what that did to our index file.
Our client is no longer in charge of creating the savings account instance, and can now worry about all the other problems that the client already has to worry about.
Clients shouldn’t have to worry about our problems, so we made sure that they don’t through the process of abstraction!A Further RevelationNow that we’ve hidden the internals of our BankMember class, no longer putting our clients in charge of doing what we should have from the start, lets look at how this scales.
Imagine we opened up our code for third parties to leverage within their applications.
Now imagine that we required all of those hundreds of outside applications to new up their own version of the SavingsAccount class to provide it as a parameter to our createSavingsAccount()method.
Nooow Imagine that with this huge update that we’re expecting by the end of the quarter, we are required to change the SavingsAccount specification.
This would require ALL of our clients to go in and make a hard change to their owns source code because of our neglect to abstract that simple piece of information away from them!If you can truly understand this, it means you now have a deep understanding of the importance of abstraction and why we use it!Final Test RunLet’s run our full application now to see that everything is hooked up correctly.
Added a check to make sure that users can not overwrite their existing savings account.
Perfect! Our code base not only accurately encapsulates it’s data, but now it abstracts unnecessary complexity away from it’s clients for ease of use.
I think we’re well on our way to tackling this coming update! :DSigning OffAbstraction is the process of hiding away what the clients shouldn’t have to worry about.
Clients have their own struggles that they are trying to take care of as do our own source code.
And this ‘complexity hiding’ process is only made possible through our compliance with the principle of abstraction.
I really hope you guys enjoyed the this article on abstraction and can begin to implement this idea into your currently active code bases! Remember to stay tuned to part three of this series on Object Orient Principles in Typescript where we’ll be talking about a big one… inheritance!P.
s at the time of this writing, I have not released my video on the topic of abstraction, but it will be live for you guys this week to help solidify your understaning :).. More details