brown bag

fear is a feature, not a bug. ( it is fear which keep us alive for most times, not a problem that stops ppl from doing something.)

  • replace condtions with polymophism: switch operation depends on the type

make subclass, in superclass the method is abstract, and different type subclass overidden it

prefer polyphosim over switch conditions

example

instead of

class Opnode {double evaluate(){

switch op
case +
case *}}

make evaluate() in Opnode abstractclass

AdditionNode extends Opnode{double evaluate() { return left.evaluate() + right.evaluate()}}

class MultipleNOde extend Opnode{double evaluate() { return left.evaluate() * right.evaluate()}}

  • seperate if conditions to different subclass (piles of objects), one state of if conditions corresponds to one subclass
  • the if logic does not go away, it sits in the factories/builders/provider ( piles of construction)
  • Java, Guice for provider
  • benefit: condition is localized in only one place; no duplication; separate of responsibility and global state

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s