Skip to content

Instantly share code, notes, and snippets.

@seagalputra
Created January 14, 2022 11:08
Show Gist options
  • Save seagalputra/3eda4b9eebfbbb400e2a4503122f048d to your computer and use it in GitHub Desktop.
Save seagalputra/3eda4b9eebfbbb400e2a4503122f048d to your computer and use it in GitHub Desktop.
Publisher abstraction for sending message using several message broker
class Publisher
attr_accessor :strategy
def initialize(strategy)
@strategy = strategy
end
def publish(data)
@strategy.call(data)
end
class Strategy
def call(data)
raise NotImplementedError, "#{self.class} has no implementation method '#{__method__}'"
end
end
# in case you want to add more strategy, just inherit call method from Strategy class and write your own logic
# to call the producer from message broker
class KafkaStrategy < Strategy
def call(data)
# implement some logic for sending message using Apache Kafka here...
end
end
class GooglePubSubStrategy < Strategy
def call(data)
# implement some logic for sending message using Google Pub Sub here...
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment