Does this make sense to be a subclass?
I'm trying to create a subclass to implement some custom functionality to a parent class. I'm making a discord bot that has a class
Discord
. This Discord
class has a constructor that takes a token.
I've created a subclass Bot
that has a constructor that calls the parent function. I've done this so I can implement environment variables.
Everything runs fine but for some reason, this just looks really off to me. I'm not sure if I'm overcomplicating it or creating an antipattern.26 Replies
honestly, this looks a bit overcomplicated
you're still going to have discord-specific code on the
bot
class
the bot
makes sense if it is an abstract class that has something generic that can be used for bots that are for other services, like teams or slack or twitch or something else
for example, if you need to send a message from the bot, you can have something like this:
so, this forces the bot to have specific methods implemented, which can be used on any other bot
now, imagine your bot does some processing, like checking the user
you can do something like this:
now, if you decide to implement a bot for something else, you can just do the same and extend from Bot
, keep the same contract, guarantee that the same functions exist everywhere and have all the generic code in a generic place that's not repeated
for example, doesn't make sense to have discord-specific code in Bot
, and doesn't make sense to have database access in DiscordBot
That’s pretty great answer but guessing from his previous question - “Discord” class is external/dependency project class, and you are meant to use it by calling its constructor with those 2 params.
Thus my guess is his goal is to take out the part of the code where he fetches the needed param(s) from the env file to a “function”/constructor to avoid repetition in case he needs the discord class instance in more than one place in the code🙂
makes sense, but for different bots you will need different configurations
which can't be made in a generic sense
what you can do is create an abstract function in
Bot
that demands you to implement the fetching of the settings yourself
and, obviously, you can share the same instance of DotEnv
between both classes
in this case, this:
Sorry guys I haven't gotten a full chance to read everything but I wanted to say I originally wanted to make this a static class. And instead of
Bot
I should probably rename this to Umpire
(my discord bot name). I don't really plan this to be reusable but instead an instance of my custom class that I can call static methods on and initialize with the env varsthis is how i would implement the basic steps
if you're not going to make it generic, doesn't make sense to separate both
My thinking is I want to abstract functions from the main file
Instead of having a messy main file I have 1 class that handles most of the methods that I can call statically in the main file
yeah, and the best way to implement it is how i explained it
this is because each class does it's own thing
what you had before, both classes do parts of the same thing
both have discord specific code and handling
if you decide to make it a slack bot later on, then you will have to do what i did anyways
unless you're okay with repeating code
Got it, reading everything now
From what I understand what you want I would call it “DiscordBotBuilder”/ “DiscordUmpireBuilder” static class with static functions to tidy your main function that will use them
With no goal of decoupling and enabling further extension
Yes, exactly that! I just wanted a nice way to call an object that contains methods I need (that would be extended from
Discord
) and a tidy way to initialize the Discord
class (or some instance of it) with my env vars 🙂
There is no way I will ever use this for anything else in the future and if there comes a time then, I'll be better equipped to rewrite it anyway since I'll know more about oop / phpwell, you can do that with my method
Ye not saying I can't, I'm reading abstract classes now
That's how little I know about oop lol
but i would make the generic
Bot
a singleton
or create a bot factoryI was thinking that too but I felt that might be a bit overkill but idk anything
a singleton is what you described
a class you initialize just once, and use it multiple times and you share the same instance everywhere, with static method or not
but the idea is that there's ever just 1 instance, at most
The problem with that is he has to write his abstract run() and abstract on() and decoupling those will produce some lines of code.
See I was thinking that I don't even need it to be initialized in an object -- I'd be happy if I was just calling it from the class whch is why I wanted it to be static but I was getting errors trying to do that
Cuz he probably needs the on() and run() from Discord class
oh okay I understand this now but yea not sure if this is what im looking for could be wrong tho
I was writing this whole thing out but realized that yea, definitely can't make this static
Or can I 🤔
I think I definitely need a singleton now that I'm writing it out and thinking about it more
Because the
Discord
class uses a constructor, so I can't get the instance of the Discord
class on the static Umpire
classIf you want umpire to be singleton you can’t make it static, but usually you hide the constructor by making it private if the language allows and you define static function like “getInstance()” that makes sure it creates it’s instance only once and then saves it in its fields and every following call to it just returns that same instance
Okay, I think I got it.
Here's what I have:
And then:
Looks / functions better?
you don't have to call it a
singleton
that's not exactly descriptive
that's an implementation detail
i would call it UmpireBot
it's php - more lines of code have little to no impact
if it was javascript, i would be a lot more worriedI ended up rewriting it to just be a regular class like Epic showed
I still called it something specific
Umpire
rather than Bot
Basically just a wrapper class that instantiates the Discord
class and has some utility functions to it
It's not a subclass but it's own thing -- I didn't want to override the parent props / methodssounds like a sensible intermediary solution
great i was hoping so 😂 i kept thinking about how to set it up and finally landed on this. i was thinking about it in bed last night too lol
i dont think it has to be a perfect solution im gonna make a lot of mistakes at this stage anyway i just want something thats not total shite and does what i need it to do
for a prototype, that's all you need