Original article: Why you shoshould think about toop-testable Object Oriented Programming Author: Roy osherove, chief architect of typemock --- The art of unit testing: author with examples in. net
I think Object Oriented Design \ Programming) it's time to make some changes.
the pursuit of quality and testability and continuous integration has begun in the industry.
we need to consider a simple fact: in many cases, there are some conceptual conflicts between a pure OOD and a testable design. I once wrote an article that fxcop removed some testable features in pursuit of pure object-oriented features (of course, the author despised the fxcop team ).
the following are some testable designs that require" hiding "for Ood/oop) example of the design proposal "public.
OOD: do not disclose private members or methods that do not need to be used by other developers.
testable: dependency injection through interfaces, open the setters method to make public or replace private members of those objects.
OOD: classes are closed unless you need to extend them to others.
testable: sealing is not allowed by default (unless security considerations are taken into consideration) so that users can reuse your methods and override according to their testing needs.
OOD: Your method is virtual only when you need to override your method. -- Because Java is virtual by default, the author prefers Java at this point.
testable: by default, your method is virtual, in this way, those who need to write tests can inherit and override your methods to remove some dependencies.
OOD: Use the singleton method to ensure that only one instance is used and no one is allowed to touch private objects.
testable: allow replacing a singleton object to remove dependencies in the test.
OOD: Unless necessary, the default type is private or interna.
testable: Open the types of APIs that you think are useful for testing projects.
OOD: do not add unnecessary APIs.
testable: add a specific API (method, interface, especially type) for testing, these are required for testing.
What if we add "testability" to the Ood vocabulary?
"Testable Object Oriented Design:Tood "(Testable object-oriented design)
"Testable object oriented programming:Toop "(Testable Object-Oriented Programming)
tood and toop make your design more solid
(Refer to Bob Martin's solid design principles. This series of principles persists, including the single responsibility principle, open closed principle, and liskov substitution principle ), interface segregation principle and dependency inversion principle ).)