Skip to main content
added 103 characters in body
Source Link
Greg Burghardt
  • 46.6k
  • 8
  • 87
  • 151

It's a difference between checking the correctness of something at run time (example #1) or compile time (example #2).

The difference is that the secondfirst example does not requirerequires you to pass around a string to determine the type of pizza, which can introduce the possibility of runtime errors:

PizzaStore store = new PizzaStore();
Pizza pizza1 = store.orderPizza("chicago style"); // Seems right
Pizza pizza2 = store.orderPizza("blue jeans");    // Blatantly wrong, but compiles

The second pizza type will probably not be understood by the pizza factory (who ever heard of a "blue jeans" pizza?) and at that point the pizza factory will throw an exception.

The second example verifies the correctness of your pizza types at compile time because you are using classes and inheritance for both the pizza stores and pizza styles.


Clarifications from comments

Goyo commented:

The second example avoid the problem you described by ignoring the pizza type in the order. Sure, this allows clients to order any kind of pizza without risking a crash, but looks dumb to me. And this could have been done with composition too, the change to inheritance is unrelated.

What Goyo says is technically true, but I think it misses the main point of the code examples. The "type" of pizza really should be an attribute of the pizza itself, but a "pizza" is a concept that is familiar to many people and was used so people would focus on the code, rather than the idea.

The difference is that the second example does not require you to pass around a string to determine the type of pizza, which can introduce the possibility of runtime errors:

PizzaStore store = new PizzaStore();
Pizza pizza1 = store.orderPizza("chicago style"); // Seems right
Pizza pizza2 = store.orderPizza("blue jeans");    // Blatantly wrong, but compiles

The second pizza type will probably not be understood by the pizza factory (who ever heard of a "blue jeans" pizza?) and at that point the pizza factory will throw an exception.

The second example verifies the correctness of your pizza types at compile time because you are using classes and inheritance for both the pizza stores and pizza styles.


Clarifications from comments

Goyo commented:

The second example avoid the problem you described by ignoring the pizza type in the order. Sure, this allows clients to order any kind of pizza without risking a crash, but looks dumb to me. And this could have been done with composition too, the change to inheritance is unrelated.

What Goyo says is technically true, but I think it misses the main point of the code examples. The "type" of pizza really should be an attribute of the pizza itself, but a "pizza" is a concept that is familiar to many people and was used so people would focus on the code, rather than the idea.

It's a difference between checking the correctness of something at run time (example #1) or compile time (example #2).

The first example requires you to pass around a string to determine the type of pizza, which can introduce the possibility of runtime errors:

PizzaStore store = new PizzaStore();
Pizza pizza1 = store.orderPizza("chicago style"); // Seems right
Pizza pizza2 = store.orderPizza("blue jeans");    // Blatantly wrong, but compiles

The second pizza type will probably not be understood by the pizza factory (who ever heard of a "blue jeans" pizza?) and at that point the pizza factory will throw an exception.

The second example verifies the correctness of your pizza types at compile time because you are using classes and inheritance for both the pizza stores and pizza styles.

Clarified answer
Source Link
Greg Burghardt
  • 46.6k
  • 8
  • 87
  • 151

The difference is that the second example does not require you to pass around a string to determine the type of pizza, which can introduce the possibility of runtime errors:

PizzaStore store = new PizzaStore();
Pizza pizza1 = store.orderPizza("chicago style"); // Seems right
Pizza pizza2 = store.orderPizza("blue jeans");    // Blatantly wrong, but compiles

The second pizza type will probably not be understood by the pizza factory (who ever heard of a "blue jeans" pizza?) and at that point the pizza factory will throw an exception.

The second example verifies the correctness of your pizza types at compile time because you are using classes and inheritance for both the pizza stores and pizza styles.


Clarifications from comments

Goyo commented:

The second example avoid the problem you described by ignoring the pizza type in the order. Sure, this allows clients to order any kind of pizza without risking a crash, but looks dumb to me. And this could have been done with composition too, the change to inheritance is unrelated.

What Goyo says is technically true, but I think it misses the main point of the code examples. The "type" of pizza really should be an attribute of the pizza itself, but a "pizza" is a concept that is familiar to many people and was used so people would focus on the code, rather than the idea.

The difference is that the second example does not require you to pass around a string to determine the type of pizza, which can introduce the possibility of runtime errors:

PizzaStore store = new PizzaStore();
Pizza pizza1 = store.orderPizza("chicago style"); // Seems right
Pizza pizza2 = store.orderPizza("blue jeans");    // Blatantly wrong, but compiles

The second pizza type will probably not be understood by the pizza factory (who ever heard of a "blue jeans" pizza?) and at that point the pizza factory will throw an exception.

The second example verifies the correctness of your pizza types at compile time because you are using classes and inheritance for both the pizza stores and pizza styles.

The difference is that the second example does not require you to pass around a string to determine the type of pizza, which can introduce the possibility of runtime errors:

PizzaStore store = new PizzaStore();
Pizza pizza1 = store.orderPizza("chicago style"); // Seems right
Pizza pizza2 = store.orderPizza("blue jeans");    // Blatantly wrong, but compiles

The second pizza type will probably not be understood by the pizza factory (who ever heard of a "blue jeans" pizza?) and at that point the pizza factory will throw an exception.

The second example verifies the correctness of your pizza types at compile time because you are using classes and inheritance for both the pizza stores and pizza styles.


Clarifications from comments

Goyo commented:

The second example avoid the problem you described by ignoring the pizza type in the order. Sure, this allows clients to order any kind of pizza without risking a crash, but looks dumb to me. And this could have been done with composition too, the change to inheritance is unrelated.

What Goyo says is technically true, but I think it misses the main point of the code examples. The "type" of pizza really should be an attribute of the pizza itself, but a "pizza" is a concept that is familiar to many people and was used so people would focus on the code, rather than the idea.

Source Link
Greg Burghardt
  • 46.6k
  • 8
  • 87
  • 151

The difference is that the second example does not require you to pass around a string to determine the type of pizza, which can introduce the possibility of runtime errors:

PizzaStore store = new PizzaStore();
Pizza pizza1 = store.orderPizza("chicago style"); // Seems right
Pizza pizza2 = store.orderPizza("blue jeans");    // Blatantly wrong, but compiles

The second pizza type will probably not be understood by the pizza factory (who ever heard of a "blue jeans" pizza?) and at that point the pizza factory will throw an exception.

The second example verifies the correctness of your pizza types at compile time because you are using classes and inheritance for both the pizza stores and pizza styles.