Need help in OOP
is there a better way to do this using OOP? this feels a bit inconsistent to me but im inexperienced in OOP and don't know if there's a better way to do this. would love some help!
42 Replies
I think a more OOP style approach would be for Shape to have
abstract function getArea(): number;
and the extending classes would define the properties themselves - it doesn’t really make sense for a rectangle to have a radius
Imo my library Sceneify is a decent demonstration of OOP with abstract classes and inheritanceinteresting
how would that look like in code? i've never seen an abstract function
It’s like putting a function on a trait in rust
You do everything except add the curly braces and body of the function
Funnily enough, this example uses the abstract class like a trait
hmm, it says abstract methods can only appear in abstract classes
need to learn this lol
Ah yeah you’ll need to make the class abstract too
An abstract class can’t be instantiated, but classes that inherit it can
like this?
Ye
The reason you’d do it this way is that now you can make a function accept
Shape
and know that the getArea
function will existohh that makes sense now
If you move all the properties from
Shape
to the extending classes, you could turn it into an interfaceyeah im trying to code up both approaches to this
one using adts and one using oop
huh
Would never have thought to do that example in that way but yeah it works
what were you thinking
Problem is that it couples
Shape
to the three specific shapesyeah you're gonna have to add other shapes manually
And if this was a library there’d be no way to add more shapes
You also lose being able to rely on object prototypes and
instanceof
what do you think is a better approach then?
I think having shape as an abstract class is better for something like this
I’m generally against OOP but in some cases it just makes sense
For TS that is, Rust is a different story just bc of how the lang works
here's ocaml
so satisfying lol
Is very nice haha
But again you can’t extend shape once you’ve defined it
yeah well thats just the fp approach i guess
tradeoffs
Hmm I don’t think that’s an inherently fp thing
Surely in fp u can define types that stuff adheres to like an interface
sure, i meant ADTs
i think this example isn't great lol
prolly should think of something else
Ah ok for ADTs you’re right
Take a look at this abstract class: it’s useless on its own, but Scene and Input both add functionality on top https://github.com/Brendonovich/sceneify/blob/main/packages/core/src/Source.ts
GitHub
sceneify/Source.ts at main · Brendonovich/sceneify
The simplest way to control OBS from JavaScript. Contribute to Brendonovich/sceneify development by creating an account on GitHub.
thats really helpful thanks
In addition to that, some functions accept either Scenes or Inputs since they’re just Sources, but I’m still able to check the specific type using
instanceof
(line 207): https://github.com/Brendonovich/sceneify/blob/main/packages/core/src/Scene.tsGitHub
sceneify/Scene.ts at main · Brendonovich/sceneify
The simplest way to control OBS from JavaScript. Contribute to Brendonovich/sceneify development by creating an account on GitHub.
i get why you'd use abstract now
It just so happens that modelling stuff from OBS in an OOP fashion works really well
yeah thats cool
oop is a good abstraction for most ui related things imo
guis for example
Do u prefer retained or immediate mode guis?
idk what that is 😄
Immediate: react, imgui. Each render discards the previous ui node tree and produces a new one
Retained: Qt, wxwidgets. UI elements are distinct object in memory that hold state and are traversed each render
ohhh
idk what i prefer cause i've never built guis
U write react 💀
but i'd probably use immediate?
i mean
lets say you're building a game
is the immediate mode really suited for that
It can be
Some games use stuff like imgui for their ui
Though if u do ui through unity or unreal it’s probs retained
that makes sense
everyday you learn something new haha
Have u had your game developer phase yet
not yet
Damn
i've only finished 1 year of coding this month lol
very beginner
Oh bruh u killing it