I was studying design patterns (and specifically Adapter pattern) and I found this example from https://refactoring.guru/design-patterns/adapter
As the diagram in the solution part describes, XML to JSON class is being called from the Core class and it calls the approached class that needs the conversion.
and Also in the examples I found it implemented that way.
# The Target defines the domain-specific interface used by the client code.
class Target
# @return [String]
def request
'Target: The default target\'s behavior.'
end
end
# The Adaptee contains some useful behavior, but its interface is incompatible
# with the existing client code. The Adaptee needs some adaptation before the
# client code can use it.
class Adaptee
# @return [String]
def specific_request
'.eetpadA eht fo roivaheb laicepS'
end
end
# The Adapter makes the Adaptee's interface compatible with the Target's
# interface.
class Adapter < Target
# @param [Adaptee] adaptee
def initialize(adaptee)
@adaptee = adaptee
end
def request
"Adapter: (TRANSLATED) #{@adaptee.specific_request.reverse!}"
end
end
# The client code supports all classes that follow the Target interface.
def client_code(target)
print target.request
end
puts 'Client: I can work just fine with the Target objects:'
target = Target.new
client_code(target)
puts "\n\n"
adaptee = Adaptee.new
puts 'Client: The Adaptee class has a weird interface. See, I don\'t understand it:'
puts "Adaptee: #{adaptee.specific_request}"
puts "\n"
puts 'Client: But I can work with it via the Adapter:'
adapter = Adapter.new(adaptee)
client_code(adapter)
now my questions are:
1- Isn't that creates a coupling issue ? specially if I want to use the XML to JSON class in anything else other than converting xml to json for the data analytics tool ?
2- Is that violates the single responsibility concept as it converts XML to JSON AND calls a function in the data analytics class ?
3- Is it fine to call the XML to JSON passing the XML data and on receiving the conversion call the data analytics class with the JSON data ? I'm always doing this to avoid any coupling issues is that wrong ?!
Thank you!
Aucun commentaire:
Enregistrer un commentaire