01 logo

Abstraction in Software Development

Abstraction in action. Power of Interfaces

By ConficlePublished 3 years ago 4 min read
Like

Hello all, we are back with our next article after a long break. In case you would like to refer to our previous articles, you can find them here.

In this article we will try to understand one of the very common concepts of object oriented programming - Abstraction.

What is Abstraction?

So if you will try to search on the internet or you might have already heard many times abstraction is a concept or mechanism to hide specific details and show only essential details.

The very common examples of abstraction are:

  1. ATM machine. As a user we know what steps to perform for cash withdrawal but we are not aware or concerned of what happens behind each step.
  2. We send SMS from the mobile. As a user we know how to open a messaging app and type the message. However we are not aware or concerned of how that message reaches the other user.
  3. A car. As a driver we drive the car using accelerators, break, clutch & steering. However we are not aware or concerned of how pressing the break stops the car.

So these are very common examples that show abstraction in action.

Now let us try to understand it in the software development world and let us see what problem it addresses.

So let us assume we have this startup product company named Wayne Enterprises, who has developed a payment SDK. This payment SDK can be integrated by any E-Commerce client that accepts online payments. Wayne enterprises have developed their SDK in such a way that any client who integrates this SDK will just have to create an instance of CardPayment class and call pay() method with amount, card number, expiry and cvv on it.

So let us assume Hammer industries has an E-Commerce platform that sells almost everything online and they are using this payment SDK of Wayne Enterprises. So let’s try to look into the software code of how Hammer industries has integrated this SDK.

class HammerIndustries {

void startPayment() {

CardPayment cardPayment = new CardPayment()

cardPayment.pay(amount, cardNumber, expiry, cvv)

}

}

So that’s a very easy integration.

Now as the some time passes Wayne enterprises has got around 10 more clients. And all the 10 clients have above piece of code in their E-Commerce software.

Wayne enterprises is doing really great in their business and suddenly pandemic has hit the world and people are avoiding card payments and using a new technology which scans QR codes instead of cards to make the payment.

So Wayne enterprises quickly updated their payment SDK and created a new class called QRCodePayment, which processes payments through QR code. As soon as Wayne enterprises releases their new SDK, all clients start complaining as they also need to change their software code to accept the payments by QR code. Every client software now needs to create instance of QRCodePayment class instead of CardPayment class and call pay(amount, qrCode) method instead of pay(cardNumber, expiry, cvv).

Considering the client complaints. Wayne enterprises hires the software architect which makes the following changes in their SDK.

So instead of exposing CardPayment and QRCodePayment classes Wayne enterprises’s SDK exposes a new interface called PaymentInterface which has only pay(amount) method and PaymentProcessor class. This interface will be implemented by both CardPayment and QRCodePayment class.

interface PaymentInterface {

void pay(amount)

}

class PaymentProcessor {

static void createPaymentProcessor(): PaymentInterface {

QRCodePayment qrCodePayment = new QRCodePayment()

return qrCodePayment

}

}

class CardPayment: PaymentInterface {

void pay(amount) {

}

}

class QRCodePayment: PaymentInterface {

void pay(amount) {

}

}

So after these changes client could should look as follows:

class Client {

void startPayment() {

PaymentInterface paymentInterface = PaymentProcessor.createPaymentProcessor()

paymentInterface.pay()

}

}

After exposing PaymentInterface and PaymentProcess Wayne enterprises did hide their implementation details behind these 2 classes. So now in future they create as many as different payment classes and keep releasing new versions of their SDK. That will not impact the client code ever.

So we saw how we can achieve abstraction using interfaces. Interfaces are one of the most powerful concepts of software development and can be used to solve many other problems. They can also be used to write unit test cases which we will discuss in future articles.

That’s it for this article. We hope we are able to explain how can we achieve abstraction using interfaces. Thanks for reading this article. If you have any queries related to this topic or iOS, Objective C and Swift. Please write us at [email protected] or direct message us on instagram at conficle(instagram username).

Also keep watching this space for upcoming articles on iOS development, software development and technology concepts.

how to
Like

About the Creator

Conficle

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.