4 replaced http://stackoverflow.com/ with https://stackoverflow.com/
source | link

What I actually ended up doing:

At first I was wondering if I am better off with such complexity.

Simpler way was:

There is another pattern here that I could use that keeps things in one class: http://stackoverflow.com/a/2175213/2883328https://stackoverflow.com/a/2175213/2883328. Breaks SRP.

$product = Product::fromProductId($productId, $wheels);

SRP way (chosen)

  • ProductData class that just has the data (can be swapped by database later)
  • ProductFactory class that uses Provider class to look up the model number
  • ProductNumberProvider class that provides model number when given product id
  • Product class that does loads ProductData and does computations

Code:

$provider = new ProductModelNumberProvider();
$factory = new ProductFactory($provider);
$product = $factory->constructFromProductId($productId, $wheels);

What I actually ended up doing:

At first I was wondering if I am better off with such complexity.

Simpler way was:

There is another pattern here that I could use that keeps things in one class: http://stackoverflow.com/a/2175213/2883328. Breaks SRP.

$product = Product::fromProductId($productId, $wheels);

SRP way (chosen)

  • ProductData class that just has the data (can be swapped by database later)
  • ProductFactory class that uses Provider class to look up the model number
  • ProductNumberProvider class that provides model number when given product id
  • Product class that does loads ProductData and does computations

Code:

$provider = new ProductModelNumberProvider();
$factory = new ProductFactory($provider);
$product = $factory->constructFromProductId($productId, $wheels);

What I actually ended up doing:

At first I was wondering if I am better off with such complexity.

Simpler way was:

There is another pattern here that I could use that keeps things in one class: https://stackoverflow.com/a/2175213/2883328. Breaks SRP.

$product = Product::fromProductId($productId, $wheels);

SRP way (chosen)

  • ProductData class that just has the data (can be swapped by database later)
  • ProductFactory class that uses Provider class to look up the model number
  • ProductNumberProvider class that provides model number when given product id
  • Product class that does loads ProductData and does computations

Code:

$provider = new ProductModelNumberProvider();
$factory = new ProductFactory($provider);
$product = $factory->constructFromProductId($productId, $wheels);
3 cleanup
source | link

What I actually did so farended up doing:

At first I was wondering if I am better off with such complexity.

Simpler way was:

There is another pattern here that I could use that keeps things in one class: http://stackoverflow.com/a/2175213/2883328. Breaks SRP.

$product = Product::fromProductId($productId, $wheels);

SRP way (chosen)

  • ProductData class that just has the data (can be swapped by database later)
  • ProductFactory class that uses Provider class to look up the model number
  • ProductNumberProvider class that provides model number when given product id
  • Product class that does loads ProductData and does computations

I am wondering if I am better off.

There is another pattern here that I can use that keeps things in one classCode: http://stackoverflow.com/a/2175213/2883328. It will most likely break SRP if I use it, but the benefit is that I just keep one class (Product) and the amount of code added is small. As I've mentioend I am not (yet) using the database to look up model number, because I can hardcode it due to the number of products being 5 at this time.

So now I am calling

$product$provider = (new ProductFactory(new ProductModelNumberProvider()))->constructFromProductId($productId,;
$factory $wheels= new ProductFactory($provider);

instead of something like

$product = Product::fromProductId$factory->constructFromProductId($productId, $wheels);

What I actually did so far:

  • ProductData class that just has the data
  • ProductFactory class that uses Provider class to look up the model number
  • ProductNumberProvider class that provides model number when given product id
  • Product class that does loads ProductData and does computations

I am wondering if I am better off.

There is another pattern here that I can use that keeps things in one class: http://stackoverflow.com/a/2175213/2883328. It will most likely break SRP if I use it, but the benefit is that I just keep one class (Product) and the amount of code added is small. As I've mentioend I am not (yet) using the database to look up model number, because I can hardcode it due to the number of products being 5 at this time.

So now I am calling

$product = (new ProductFactory(new ProductModelNumberProvider()))->constructFromProductId($productId, $wheels);

instead of something like

$product = Product::fromProductId($productId, $wheels);

What I actually ended up doing:

At first I was wondering if I am better off with such complexity.

Simpler way was:

There is another pattern here that I could use that keeps things in one class: http://stackoverflow.com/a/2175213/2883328. Breaks SRP.

$product = Product::fromProductId($productId, $wheels);

SRP way (chosen)

  • ProductData class that just has the data (can be swapped by database later)
  • ProductFactory class that uses Provider class to look up the model number
  • ProductNumberProvider class that provides model number when given product id
  • Product class that does loads ProductData and does computations

Code:

$provider = new ProductModelNumberProvider();
$factory = new ProductFactory($provider);
$product = $factory->constructFromProductId($productId, $wheels);
2 added 234 characters in body
source | link

What I actually did so far:

  • ProductData class that just has the data
  • ProductFactory class that uses Provider class to look up the model number
  • ProductNumberProvider class that provides model number when given product id
  • Product class that does loads ProductData and does computations

I am wondering if I am better off.

There is another pattern here that I can use that keeps things in one class: http://stackoverflow.com/a/2175213/2883328. It will most likely break SRP if I use it, but the benefit is that I just keep one class (Product) and the amount of code added is small. As I've mentioend I am not (yet) using the database to look up model number, because I can hardcode it due to the number of products being 5 at this time.

So now I am calling

$product = (new ProductFactory(new ProductModelNumberProvider()))->constructFromProductId($productId, $wheels);

instead of something like

$product = Product::fromProductId($productId, $wheels);

What I actually did so far:

  • ProductData class that just has the data
  • ProductFactory class that uses Provider class to look up the model number
  • ProductNumberProvider class that provides model number when given product id
  • Product class that does loads ProductData and does computations

I am wondering if I am better off.

There is another pattern here that I can use that keeps things in one class: http://stackoverflow.com/a/2175213/2883328. It will most likely break SRP if I use it, but the benefit is that I just keep one class (Product) and the amount of code added is small. As I've mentioend I am not (yet) using the database to look up model number, because I can hardcode it due to the number of products being 5 at this time.

What I actually did so far:

  • ProductData class that just has the data
  • ProductFactory class that uses Provider class to look up the model number
  • ProductNumberProvider class that provides model number when given product id
  • Product class that does loads ProductData and does computations

I am wondering if I am better off.

There is another pattern here that I can use that keeps things in one class: http://stackoverflow.com/a/2175213/2883328. It will most likely break SRP if I use it, but the benefit is that I just keep one class (Product) and the amount of code added is small. As I've mentioend I am not (yet) using the database to look up model number, because I can hardcode it due to the number of products being 5 at this time.

So now I am calling

$product = (new ProductFactory(new ProductModelNumberProvider()))->constructFromProductId($productId, $wheels);

instead of something like

$product = Product::fromProductId($productId, $wheels);
1
source | link