Hello
It is very reasonable and simple code structure: create different objects in run-time
But, it violate Open-Closed Principle. Why? If we add new constructed type we need change enum and change SimpleFactory.
But, do not worry, maybe it will feet Your needs. Maybe Types will never be added. For example, there 12 types of months and in future no month will be added, becouse there is no 13 months in year :-)
Class Diagram:
Code:
Using:
That's all
It is very reasonable and simple code structure: create different objects in run-time
But, it violate Open-Closed Principle. Why? If we add new constructed type we need change enum and change SimpleFactory.
But, do not worry, maybe it will feet Your needs. Maybe Types will never be added. For example, there 12 types of months and in future no month will be added, becouse there is no 13 months in year :-)
Class Diagram:
Client should know:
1. Which object to create. It can be enum (like in example, "ObjectType") or string or something else.
2. SimpleFactory and its static method Create
3. Created object interface (like in example, "IFactoryInterface")
My example - is factory of processors: Intel, AMD and VIA
Code example:
That's all
No comments:
Post a Comment