mercredi 20 octobre 2021

Is this problem match with bridge design pattern

So I want to design a data class for storing network information. Network information should store something like this.

data class PatrickNetwork(
  @Id
  val networkId: String,
  val name: String,
  val description: String,
  val vlan: Int,
  val networkType: NetworkType,
  val networkInfo: NetworkInfo
)

enum class NetworkType {
  BRIDGE,
  PEERING
}

the other fields are fine, but the problem is on the network info field which depends on the network type. This is how it should store

// for bridge type
data class NetworkInfo(
  val ipv4Cidr: String,
  val bridgeIP: List<String>
)
// for peering type
data class NetworkInfo(
  val ipv4Cidr: String,
  val customerIP: String,
  val organizeIP: String
)

I have tried applying the bridge design pattern to my code and I got this

data class PatrickNetwork(
  @Id
  val networkId: String,
  val name: String,
  val description: String,
  val vlan: Int,
  val networkType: NetworkType,
  val networkInfo: IPatrickNetworkInfo
)

interface IPatrickNetworkInfo {
   val ipv4Cidr: String
}

data class BridgeNetworkInfo(
  override val ipv4Cidr: String,
  val bridgeIP: List<String>
): IPatrickNetworkInfo

data class PeeringNetworkInfo(
  override val ipv4Cidr: String,
  val customerIP: String,
  val organizeIP: String
): IPatrickNetworkInfo

So I'm very new to design pattern am I doing it wrong or miss understanding the concept or are there any design pattern that fit better for this problem

Aucun commentaire:

Enregistrer un commentaire