Object-oriented design principle (2): OCP

zhaozj2021-02-12  161

Too many famous, no longer talk more. Main two points

(1) Open for Extension

(2) Closed for modification

Robert.c.martin gifted an example of a Shape to explain the problem. The key idea is that if we predict changes, you can design an abstraction to isolate it. But regardless of how the module is "closed", there will be some unable to seal. This requires designers to design the module to choose the change in the module, and he must guess the most likely variation. The abstraction is then constructed. This predictive ability requires experienced accumulation !! And usually guess mistakes, what to do, Martin pointed out a road:

(1) only being fooled once

We are willing to be hit by the first bullets, then we will ensure that they are no longer hit by any bullets launched by the same gun.

(2) Stimulating changes:

The sooner the first bullet comes, the sooner of us, the more you write test, use a short iterative cycle, as early as possible, often show the software to customers

OCP is the core of object-oriented design. Follow this principle to bring huge benefits, but the shackle abstraction is not a good attention, refuses to be immature abstraction and abstraction itself !!!

转载请注明原文地址:https://www.9cbs.com/read-6550.html

New Post(0)