I have two ts pages (let’s say page A and page B) . In these pages I have functions and I want only one of the other pages in my project to use them (we’ll call it page C). The rest of the functions in pages A and B will be public for the rest of the pages in my project.
I figured I could use classes, turn pages A,B and C into classes and get C to inherit A and B. Then I will turn the functions limited to C only to protected functions and the rest will be public functions. The problems is, I cannot find a way to inherit two classes and actually use their functions in my new class (I can’t figure out how can I use functions from class A and B in C when using mixins to inherit from both). Is there any way to use mixins that way? Or any other idea to do what I want? (make some of the functions limited to C only).
#make class inherit multiple classes
30 messages · Page 1 of 1 (latest)
in traditional OOP, it's not possible to inherit from multiple classes
sounds like composition could be a better model
TS has a concept of "mixins" tho, which uses composition to "fake" multiple inheritence
tho you could very well use different techniques to achieve the same result
What technique?
not use classes
and use objects that you compose yourself
either fulyl manually, or using some kind of builder/factory pattern
or don't use inheritence to model it, you could use composition with classes instead
Can I use protected functions in composition?
not sure why you'd want to
Because I want only class C to be able to use these functions
which functions, exactly?
can't you use mixing to add the public methods from A and B, then add that protected method on the C class directly?
Do you mean to put these functions directly in C? I don’t do it for readability and logic separation mostly
it's not very clear what class should own what method
maybe you could give us some example of how you want your code to be divided and what you want the final result to look like
It’s basically classes that holds a data structure each and has some basic logic like add to the structure of delete from the structure, the thing is in my project itself when adding to the structure I need to add to the other structure as well and do some additional things like saving them both in local storage. So I figured I’d do a third class (this one was a utility before and I changed it to a class only for this inheritance idea thingy) that has the actual methods use in the ui (like add(){ addToFirstStructure(); addToSecond(); saveInLocalStorage();}
sounds more like a design issue
think there could be design patterns that would make your life easier
like visitor pattern for the classes needing to make changes to the data structure
and strategy pattern for the class handling the storage part
but yeah, think the storage needs to be decoupled from the data structure itself
not too sure if you have access to services/dependency injection, or some kind of store holding the general state
so yeah
just a couple of ideas
Its a web app and the method that combines all of these not related structures and savings and stuff is used as an event (like the user clicks on something and these structures + local storage) updates
maybe you could have a certal store holding the data structure, and that store would also have methods to perform the different actions and modify the structures accordingly
you could very well create the store using slices (depending on what kind of store you use)
allowing for smaller units of logic
Interesting idea, I will check it out. thank you 🙂
you might be interested in https://zustand.docs.pmnd.rs/guides/slices-pattern
each slice would have "private" methods
and the store has "public" ones that call the "private" ones