Skip to main content
added 80 characters in body
Source Link

Your concern of static factory method violating open close principle is really not a concern when the pattern is employed in the main side of the code. If it is used in application side then it could be an issue.

When code follows Open-Close principle the code becomes scalable. However the complexity is also increased. So the decision of follow open-close principle or not has to be exercised based on the tradeoffs. If we make our code 100% open-close, then the complexity will be very very high.

It would be a good design for an application (be it web, desktop, embedded, and so on) to segregate the code in to application (business) code and main (bootstrapping) code.

Application side code

  • Would contain business logic and high level policies.
  • Most of the code in the project would come in this side.
  • Here most of the principles are followed (or at least hoped to be followed).
  • This side ideally could have 100% unit test coverage.
  • This side should not depend on main side.

Main side code

  • Contains implementation of dependencies and the factory logic for choosing the specific implementation for a dependency.
  • This is also called the bootstrapping code.
  • The code at the main side has to be generally small and very simple and doesn't need to scale as it is written for a specific deployment or a specific client.
  • The main side of the code is outer most layer of code. Nothing else in the project depends on the main side code. The main side code depends on almost every other part of the project.
  • Due to the nature of main side code, the cost of modification is quite low.

Summary

The static factory patterns are generally used in the main side code where the complexity is low and the cost of modification is also low.

The below class diagram could help in visualization. The interactions between the classes in the main side is NOT shown. In the diagram below bootstrappingMain and dependencyProject is considered as main side code Class Diagram of a generic application

Your concern of static factory method violating open close principle is really not a concern when the pattern is employed in the main side of the code. If it is used in application side then it could be an issue.

When code follows Open-Close principle the code becomes scalable. However the complexity is also increased. So the decision of follow open-close principle or not has to be exercised based on the tradeoffs. If we make our code 100% open-close, then the complexity will be very very high.

It would be a good design for an application (be it web, desktop, embedded, and so on) to segregate the code in to application (business) code and main (bootstrapping) code.

Application side code

  • Would contain business logic and high level policies.
  • Most of the code in the project would come in this side.
  • Here most of the principles are followed (or at least hoped to be followed).
  • This side ideally could have 100% unit test coverage.
  • This side should not depend on main side.

Main side code

  • Contains implementation of dependencies and the factory logic for choosing the specific implementation for a dependency.
  • This is also called the bootstrapping code.
  • The code at the main side has to be generally small and very simple and doesn't need to scale as it is written for a specific deployment or a specific client.
  • The main side of the code is outer most layer of code. Nothing else in the project depends on the main side code. The main side code depends on almost every other part of the project.
  • Due to the nature of main side code, the cost of modification is quite low.

The static factory patterns are generally used in the main side code where the complexity is low and the cost of modification is also low.

The below class diagram could help in visualization. Class Diagram of a generic application

Your concern of static factory method violating open close principle is really not a concern when the pattern is employed in the main side of the code. If it is used in application side then it could be an issue.

When code follows Open-Close principle the code becomes scalable. However the complexity is also increased. So the decision of follow open-close principle or not has to be exercised based on the tradeoffs. If we make our code 100% open-close, then the complexity will be very very high.

It would be a good design for an application (be it web, desktop, embedded, and so on) to segregate the code in to application (business) code and main (bootstrapping) code.

Application side code

  • Would contain business logic and high level policies.
  • Most of the code in the project would come in this side.
  • Here most of the principles are followed (or at least hoped to be followed).
  • This side ideally could have 100% unit test coverage.
  • This side should not depend on main side.

Main side code

  • Contains implementation of dependencies and the factory logic for choosing the specific implementation for a dependency.
  • This is also called the bootstrapping code.
  • The code at the main side has to be generally small and very simple and doesn't need to scale as it is written for a specific deployment or a specific client.
  • The main side of the code is outer most layer of code. Nothing else in the project depends on the main side code. The main side code depends on almost every other part of the project.
  • Due to the nature of main side code, the cost of modification is quite low.

Summary

The static factory patterns are generally used in the main side code where the complexity is low and the cost of modification is also low.

The below class diagram could help in visualization. The interactions between the classes in the main side is NOT shown. In the diagram below bootstrappingMain and dependencyProject is considered as main side code Class Diagram of a generic application

Source Link

Your concern of static factory method violating open close principle is really not a concern when the pattern is employed in the main side of the code. If it is used in application side then it could be an issue.

When code follows Open-Close principle the code becomes scalable. However the complexity is also increased. So the decision of follow open-close principle or not has to be exercised based on the tradeoffs. If we make our code 100% open-close, then the complexity will be very very high.

It would be a good design for an application (be it web, desktop, embedded, and so on) to segregate the code in to application (business) code and main (bootstrapping) code.

Application side code

  • Would contain business logic and high level policies.
  • Most of the code in the project would come in this side.
  • Here most of the principles are followed (or at least hoped to be followed).
  • This side ideally could have 100% unit test coverage.
  • This side should not depend on main side.

Main side code

  • Contains implementation of dependencies and the factory logic for choosing the specific implementation for a dependency.
  • This is also called the bootstrapping code.
  • The code at the main side has to be generally small and very simple and doesn't need to scale as it is written for a specific deployment or a specific client.
  • The main side of the code is outer most layer of code. Nothing else in the project depends on the main side code. The main side code depends on almost every other part of the project.
  • Due to the nature of main side code, the cost of modification is quite low.

The static factory patterns are generally used in the main side code where the complexity is low and the cost of modification is also low.

The below class diagram could help in visualization. Class Diagram of a generic application