SRP(Single Responsibility Principle) make class interfaceare complete and minimal. That is difficult in practical
programming. such as "The zen of Design patterns", A phone class
should be derived from IConnetionManager and IDataTransfer.
That is an extremely example, but explain some idea of how to
keep interface minimal. (In my view, functions should be less than
20. )
Interface Segregation Principle.
Difference between Single Responsibility Principle. SRP
consider from inside of a class. Interface Segregation Principle
consider from the user of a class.
A simple example is "Interface Segregation Principle" which I have
add to my evernote. If there is robot class, You can’t inherit it
from IWorker, because robot don’t need eat, Maybe you
can put an empty function here. but It’s not good design.
IWorker{ work(); eat() }
LSP( Liskov Substitution Principle) Functions That use pointersor references to base classes must be able to use objects ofderived classes without knowing it.
Subclass must implement all baseclass method. 1) You have
to implement pure virtual function. 2) you may or maynot
override virtual function. 3) you can’t override any non-virtual
function.
Law of Demeter
Basically, class interface should be only include functions. and
avoid data members in the public interface.
Only talk with friends Only member. Only input orreturn parameter in class method are friend.
Program to an interface, not an implementation. There areanother same two principles. They share the same ideabehind.
DIP (Dependence Inversion Principle) High level modules
should not depend upon low level modules. Both should
depend upon abstraction.
Open close principle (open for extension but closed for
modifications.) Allow the adding of new functionality as new
classes or inherate current classes, keeping as much as possible
existing code unchanged.
Subclass extend it’s functionality by reusing functionality
in parent classes is just inheritance half story, and frankly
speaking, not prefer one. Favor object composition overobject inheritance
Inheritance can make clients remain unaware of specific type
of class or obj behind the abstract interface. Then put all
change behind the abstract interface.
In C++, you need to declare base class pointer( or reference)
and specify a particular implementation obj when you need
to change.
How
to understand interface? Client<->Interface<->Implement.
through Interface, you keep all the change happen implement
side, not affect Client at all.
When you want to reuse some codes. Favor object compositionover class inheritance.