#Why is interface needed in this example?

1 messages · Page 1 of 1 (latest)

latent nacelle
#

Hello everyone. I was watching a youtube video about Open-Closed principle and it showed this piece of code. Can somebody explain why the interface is needed?

brittle portalBOT
#

<@&987246841693360200> please have a look, thanks.

latent nacelle
#

Any help?

drifting quartz
#

Why do you think it's not needed?

indigo ember
#

Maybe its good to go back to the roots and understand the concept of interfaces ^^ Understanding interfaces as a method contract, and a way to allow different implementations of a method. Which allows a class to define that they promise to provide the specific behavior promised by the interface. In this way all implementing classes can be treated the same.

You could try to implement the same code as you've shown in the picture without interaces and with interfaces, if you'd like to feel the difference.

You can read these if you want to see some more explanations and examples
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
https://java-programming.mooc.fi/part-9/2-interface
https://en.wikipedia.org/wiki/Interface_(Java)

An interface in the Java programming language is an abstract type that is used to declare a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations (variable declarations that are declared to be both static and final)...

sonic sandal
# latent nacelle Any help?

In the above screenshot, the interface allows any of those 3 implementations to be used interchangeably.

If they don't need to be used interchangeably, then you might not need the interface.

#

Side note, I don't think the open-closed principle very helpful in most situations.

It's quite an abstract concept, and on its own lacks context of when it's useful and when it's harmful.

If you find it helpful, that's great, don't let me minimize that for you. But most of the time I see it causing confusion, and so if you're confused about it, you're not alone!

fading bloom
#

Open and closed principle tells Open for extension and closed for modification

FOR EXAMPLE -

think of it like u have a laptop it has a track pad for mouse what if u wanted a mouse instead of trackpad

will u disassemble the whole laptop just to connect it to mouse

Nope, Instead you have USB port where u plug things in so that ur new mouse works

YOUR CODE

- FIRST LET US SEE WHAT IF WE DIDNT HAVE ANY INTERFACE
Imagine you have a class which is named as YoutubeIncomeEarnings
You will have to do something like this

public class YoutubeIncomeEarnings {

public double calculateEarnings(String category, Video video) {
    double earnings = 0.0;
    
    if (category.equals("Educational")) {
        earnings = video.getLikes() * 0.013 + video.getViews() * 0.0013;
    } else if (category.equals("Gaming")) {
        earnings = video.getLikes() * 0.012 + video.getViews() * 0.0012;
    }
    
    return earnings;
}

}

now what if u wanted to add education and show how much income they should get or some more channel
you will have to add another elseif inside that class

you will have to manually change the code base 
just disassembling laptop and adding part and then again assembling it just to add mouse instead of just of how we just used USB

+BEST WAY BY USING OPEN CLOSE PRINCIPLE

make an interface called as IEarningsCalculator

and implement this interface to whatever new class you want and pass the object in YoutubeEarnings just like this ur image 

and in main class you will do something like this

EarningsCalculator educationalCalculator = new EducationalEarningsCalculator()

double educationalEarnings = earningsService.calculateEarnings(educationalCalculator, video);

HOPE THIS HELPED PLEASE DO LET ME KNOW

brittle portalBOT
fading bloom
#

@latent nacelle

#

man that took soo long to type
am trying to improve my explanation skills please do rate me at end whoever reads

fading bloom
#

@vivid robin how is my explanation uwu

white hamlet
# fading bloom # Open and closed principle tells Open for extension and closed for modification...

    public double calculateEarnings(String category, Video video) {
        double earnings = 0.0;

        if (category.equals("Educational")) {
            earnings = video.getLikes() * 0.013 + video.getViews() * 0.0013;
        } else if (category.equals("Gaming")) {
            earnings = video.getLikes() * 0.012 + video.getViews() * 0.0012;
        }

        return earnings;
    }
}

now what if u wanted to add education and show how much income they should get or some more channel
you will have to add another elseif inside that class

you will have to manually change the code base 
just disassembling laptop and adding part and then again assembling it just to add mouse instead of just of how we just used USB```
Can't u just extend YoutubeIncomeEarnings
fading bloom
#

i had to go from startso that he knows why

white hamlet
#

No just the way u explained it sums to interface = open closed princp

#

Just thought I should give some constructive feedback

#

xd sorry to say but the example u choose is terrible

#

I am ngl this earnings example is more about inheritance, defining contract between classes and re-useability

sonic sandal
white hamlet
#

good effort tho

sonic sandal
#

yeah effort 10/10, I appreciate the intention put into trying to format things well, it just wasn't accessible on mobile.
so I can't easily rate the content itself yet.

fading bloom
#

i will improve

#

i tried to tell why are we using OCP

#

by telling the issue first then solving

sonic sandal
fading bloom
#

yea i didnt specify interface i was trying to tell how can we code to do extension and not modification

#

i was not emphasizing on interface his code had so was trying to relate

white hamlet
sonic sandal
#

As someone who has studied software design enough, I find open-closed to be true, but not necessarily helpful. This above confusion is a prime example of it not being helpful.
It's application often includes debates about exactly what it means, and offers little guidance about when it applies and when it doesn't apply, leading to frequent misapplication.

Also, just because good software often exhibits open-closed, does not mean that making your software open-closed will make it good. This also makes it unhelpful in my experience, especially unhelpful as a recommendation for someone beginning to improve their design skills. Pursuing open-closed for the sake of open-closed itself usually leads to lower quality results.

fading bloom
#

cos even i am learning and trying to have all SOLID makes code more confusing and large

#

but slowly ig i will learn