简体   繁体   中英

How Open Close Principle decides which instance of interface to use?

I am reading the clean architecture chapter - 8 Page number 72, The Open-Closed Principle. The chapter has a thought experiment of a system that displays the Financial data on a web page and there is a requirement to show the data on black-white printed page with proper page headers, page footers etc.

解决方案

Uncle bob says that the problem should be modeled as shown in the diagram. In the diagram controller has no dependency on the Screen Presenter or Web Presenter and it's easy to add another Presenter as well.

Does this architecture means that for formatting the data in the pdf format. I will have to initialize a new instance of the Financial Report Controller with Print Presenter as one of the instance variable?

I will have to initialize a new instance of the Financial Report Controller with Print Presenter as one of the instance variable?

No. But you will have to pass the appropriate Print Presenter to the Financial Report Controller somehow.

When you decide which one is appropriate doesn't have to be on initialization. You could pass it later with a setter. Or you could pass a collection of them to choose from.

Or, like you said, create a new instance of the controller. They all work. Use what makes sense for your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM