oop - Table-driven factory vs inheritance -


i have bunch of derived classes differ static methods.

(it's ruby, question not ruby, it's design.)

class exporter   def export     # ...   end end  class exportera < exporter   def     'aaa_table'   end    def fields     ['a1', 'a2', 'a3']   end    def     'aaa_file'   end end  class exporterb < exporter   def     'bbb_table'   end    def fields     ['b1', 'b2', 'b3']   end    def     'bbb_file'   end end 

so, looked @ , came idea of placing static data kind of table , use exporter appropriate attributes. in case, need kind of exporterfactory class going know who , how create , b exporters.

class exporterfactory   def _table     return {       :a => {         :from => 'aaa_table',         :fields => ['a1', 'a2', 'a3'],         :to => 'aaa_file',       },       :b => {         :from => 'bbb_table',         :fields => ['b1', 'b2', 'b3'],         :to => 'bbb_file',       },     }   end    def create(type)     return exporter.new(self._table[type])   end end 

again, looked @ , don't approach. reasons:

  • my real data _table bigger table looks heavy , ugly.
  • now can create exporters don't make sense.
  • it looks factory knows much, prefer have data a-export encapsulated in exportera.

i can't decide. second approach seems more logical, still want use first one. main idea “i want use inheritance organize big , ugly table”.

what should choose? kind of problems i'm going have in each of ways?

i agree factory knows much, meaning has change every time exporter changes. also, if ever have exporter needs additional code, there no way create it. first design allows write exporters override superclass methods if ever need them.

you make first design more succinct putting data in initialize , overriding rather 3 methods:

class exporter   attr_accessor :from, :fields, :to    def initialize(from, fields, to)     self.from =     self.fields = fields     self.to =   end    def export     # ...   end  end  class exportera < exporter   def initialize     super 'aaa_table', ['a1', 'a2', 'a3'], 'aaa_file'   end  end  class exporterb < exporter   def initialize     super 'bbb_table', ['b1', 'b2', 'b3'], 'bbb_file'   end  end 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -