Benefits
It removes the need to bind application-specific classes into the code. The code interacts solely with the resultant interface, so it will work with any classes that implement that interface.
Because creating objects inside a class is more flexible than creating an object directly, it enables the subclass to provide an extended version of an object.
Applicable Scenarios
A class is not able to anticipate the class of objects it needs to create.
A class wants its subclasses to specify the objects it instantiates.
Classes assign responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
博客介绍了代码仅与接口交互,无需绑定特定类,能与实现该接口的任何类协作,且类内创建对象更灵活,子类可提供扩展版本。还说明了适用场景,如类无法预知需创建的对象类、希望子类指定实例化对象等。
2156

被折叠的 条评论
为什么被折叠?



