Skip to main content
deleted 13 characters in body
Source Link
Songo
  • 6.7k
  • 4
  • 52
  • 90

I'm working on refactoring a legacy application where I implemented the State pattern successfully as shown in the following diagram:

enter image description hereState Pattern UML

As you see there is a common behavior between the 3 states, so I decided to extract the common method Refund() to a parent abstract class RefundableState where I implemented it:

enter image description hereRefactoring for common operations

Refactoring further I notice that the Ship() method is common between the 2 states Cancelled and NewOrder, so I go for another round of extracting the common behavior to another parent class ShippableState:

enter image description hereRefactoring problem!!

Now I need my Cancelled state to extend 2 classes and in the same time I can't group the Refund() and Ship() function together as not all state permit the 2 actions.

How can I fix this?

Notes:

  • This question is mainly about applying DRY and refactoring not about how to implement the state pattern.
  • I'm using PHP 5.3, but I'm open to answers in any other languages.

I'm working on refactoring a legacy application where I implemented the State pattern successfully as shown in the following diagram:

enter image description here

As you see there is a common behavior between the 3 states, so I decided to extract the common method Refund() to a parent abstract class RefundableState where I implemented it:

enter image description here

Refactoring further I notice that the Ship() method is common between the 2 states Cancelled and NewOrder, so I go for another round of extracting the common behavior to another parent class ShippableState:

enter image description here

Now I need my Cancelled state to extend 2 classes and in the same time I can't group the Refund() and Ship() function together as not all state permit the 2 actions.

How can I fix this?

Notes:

  • This question is mainly about applying DRY and refactoring not about how to implement the state pattern.
  • I'm using PHP 5.3, but I'm open to answers in any other languages.

I'm working on refactoring a legacy application where I implemented the State pattern successfully as shown in the following diagram:

State Pattern UML

As you see there is a common behavior between the 3 states, so I decided to extract the common method Refund() to a parent abstract class RefundableState where I implemented it:

Refactoring for common operations

Refactoring further I notice that the Ship() method is common between the 2 states Cancelled and NewOrder, so I go for another round of extracting the common behavior to another parent class ShippableState:

Refactoring problem!!

Now I need my Cancelled state to extend 2 classes and in the same time I can't group the Refund() and Ship() function together as not all state permit the 2 actions.

How can I fix this?

Notes:

  • This question is mainly about applying DRY and refactoring not about how to implement the state pattern.
  • I'm using PHP 5.3, but I'm open to answers in any other languages.
Tweeted twitter.com/#!/StackProgrammer/status/301318143685570561
Source Link
Songo
  • 6.7k
  • 4
  • 52
  • 90

Applying DRY to an inheritance hierarchy

I'm working on refactoring a legacy application where I implemented the State pattern successfully as shown in the following diagram:

enter image description here

As you see there is a common behavior between the 3 states, so I decided to extract the common method Refund() to a parent abstract class RefundableState where I implemented it:

enter image description here

Refactoring further I notice that the Ship() method is common between the 2 states Cancelled and NewOrder, so I go for another round of extracting the common behavior to another parent class ShippableState:

enter image description here

Now I need my Cancelled state to extend 2 classes and in the same time I can't group the Refund() and Ship() function together as not all state permit the 2 actions.

How can I fix this?

Notes:

  • This question is mainly about applying DRY and refactoring not about how to implement the state pattern.
  • I'm using PHP 5.3, but I'm open to answers in any other languages.