存档

文章标签 ‘Design Pattern; Programming’

Favov object composition over inheritance

2010年3月24日 Jarod Lee 没有评论

The other major concept you should recognize is that of object composition. This is simply the construction of objects that contain others:
encapsulation of several objects inside another one. While many beginning
OO programmers use inheritance to solve every problem, as you begin to
write more elaborate programs, you will begin to appreciate the merits of
object composition.Your new object can have the interface that is best for
what you want to accomplish without having all the methods of the parent
classes. Thus, the second major precept suggested by Design Patterns is
Favor object composition over inheritance.

Program to an interface and not to an implementation

2010年3月24日 Jarod Lee 没有评论

Putting this more succinctly, you should define the top of any class
hierarchy with an abstract class or an interface, which implements no
methods but simply defines the methods that class will support. Then in all
of your derived classes you have more freedom to implement these
methods as most suits your purposes.