This is my first question on this website and I hope it goes well.
I have created a Kotlin factory pattern and I am using it very frequently. the code I use it consists of functions inside a class. This is not a static class however, it does not mutate any field inside this class.
When I look at this code, I can tell there is a pattern, so I thought I could improve it. That's why I am asking this question, for ideas & thoughts on how to improve the structure. The code is the following
class Client() {
fun dispatchFollowEvent(followerID: Int, destination: Server, callback: (success: Any?) -> Unit) {
val packet: FollowUserPacket = this.factory.makePacket(PacketType.FOLLOW_USER) as FollowUserPacket
packet.payload = FollowUserPacket.FollowUserPayload(this, followerID)
sendToServer(packet, this, destination, callback)
}
fun dispatchRegisterEvent(destination: Server) {
val packet: RegistrationPacket = this.factory.makePacket(PacketType.REGISTRATION) as RegistrationPacket
packet.payload = RegistrationPacket.RegistrationPayload(this)
sendToServer(packet, this, destination)
}
fun dispatchUploadEvent(image: String, destination: Server) {
val packet: UploadImagePacket = this.factory.makePacket(PacketType.UPLOAD_IMAGE) as UploadImagePacket
packet.payload = UploadImagePacket.UploadImagePayload(this, image)
sendToServer(packet, this, destination)
}
fun dispatchListUsersEvent(destination: Server, callback: (usersIDs: Any?) -> Unit) {
val packet: ListUsersPacket = this.factory.makePacket(PacketType.LIST_USER_IDS) as ListUsersPacket
packet.payload = ListUsersPacket.ListUsersPayload(this)
sendToServer(packet, this, destination, callback)
}
fun dispatchGetFollowRequestsEvent(destination: Server, callback: (requests: Any?) -> Unit) {
val packet: GetFollowRequestsPacket = this.factory.makePacket(PacketType.GET_FOLLOW_REQUESTS) as GetFollowRequestsPacket
packet.payload = GetFollowRequestsPacket.GetFollowRequestsPayload(this)
sendToServer(packet, this, destination, callback)
}
}
The class is simplified just so that it can be easier to read. I'm sure that all of you can see a pattern in this code, but it's beyond my knowledge to know how to make it more structured.
Do not worry about the functionality, the focus of this question is about improving the structure.
The factory code
class PacketFactory : Serializable {
@Throws(Exception::class)
fun makePacket(type: PacketType): Packet {
return when (type) {
PacketType.REGISTRATION -> RegistrationPacket()
PacketType.UPLOAD_IMAGE -> UploadImagePacket()
PacketType.LIST_USER_IDS -> ListUsersPacket()
PacketType.FOLLOW_USER -> FollowUserPacket()
PacketType.GET_FOLLOW_REQUESTS -> GetFollowRequestsPacket()
else -> throw Exception("Type is not provided for the factory!")
}
}
}
The sendToServer function
fun sendToServer(
payload: Packet,
sender: Client,
receiver: Server,
callback: ((data: Any?) -> Unit)? = null
): Unit {
// Code that sends payload to receiver and then invokes the callback
}
sendToServer()
, the functionfacory.makePacket()
, and probably the definitions of the classpacket
. Currently there is not enough code to review and provide good suggestions. \$\endgroup\$ – pacmaninbw Apr 26 '20 at 21:07