Q. Why would you prefer code reuse via composition over inheritance? Both the approaches make use of olymorphism and gives code reuse (in different ways) to achieve the same results but:
1,The advantage of class inheritance is that it is done statically at compile-time and is easy to use. The disadvantage of class inheritance is that because it is static, implementation inherited from a parent class cannot be changed at run-time. In object composition, functionality is acquired dynamically at run-time by objects collecting references to other objects. The advantage of this approach is that implementations can be replaced at run-time. This is possible because objects are accessed only through their interfaces, so one object can be replaced with another just as long as they have the same type. For example: the composed class AccountHelperImpl can be replaced by another more efficient implementation as shown below if required:
public class EfficientAccountHelperImpl implements AccountHelper {
public void deposit(double amount) {
System.out.println("efficient depositing " + amount);
}
public void withdraw(double amount) {
System.out.println("efficient withdrawing " + amount);
}
}
2,Another problem with class inheritance is that the subclass becomes dependent on the parent class
implementation. This makes it harder to reuse the subclass, especially if part of the inherited implementation is no longer desirable and hence can break encapsulation. Also a change to a superclass can not only ripple down the inheritance hierarchy to subclasses, but can also ripple out to code that uses just the subclasses making the design fragile by tightly coupling the subclasses with the super class. But it is easier to change the interface/implementation of the composed class.
Due to the flexibility and power of object composition, most design patterns emphasize object composition over
inheritance whenever it is possible. Many times, a design pattern shows a clever way of solving a common problem through the use of object composition rather then a standard, less flexible, inheritance based solution.
- composiion_over_inheritance.zip (1.2 KB)
- 描述: a clever way of solving a common problem through the use of object composition rather then a standard, less flexible, inheritance based solution.
- 下载次数: 8
分享到:
相关推荐
<filter-name>CAS Filter</filter-name> <filter-class> edu.yale.its.tp.cas.client.filter.CASFilter </filter-class> <!-- server login url --> <init-param> <param-name> edu.yale.its.tp...
-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> --> <param-value>classpath:beans.xml</param-value> </context-param> <filter> <filter-name>...
<column name="ID" type="java.lang.Integer"/> </table> -<table schema="SCOTT" name="BIZ_CLAIM_VOUCHER"> <column name="ID" type="java.lang.Integer"/> </table> -<table schema="SCOTT" name="BIZ_...
<display-name>RegisterSy</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>...
javaee-api-8.0-javadoc.jar,这是javaee1.8api的jar包,解压后可用javadoc2chm制作成chm帮助文档。
<servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>velocity</servlet-name> ...
- 配置 `<servlet>` 和 `<servlet-mapping>` 标签来定义 `DispatcherServlet` 并指定其拦截的 URL 模式。 ```xml <servlet> <servlet-name>controller</servlet-name> <servlet-class>org.springframework.web....
JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. ...
├<阶段1 java语言基础> │ ├<1-1-Java基础语法> │ ├<1-2 -面向对象和封装> │ └<1-3-Java语言高级> ├<阶段2 JavaWeb·> │ ├<01 HTML和CSS> │ ├<02 JavaScript> │ ├<03 BootStrap> │ ├<04 XML> │ ├...
Java EE (Java Platform, Enterprise Edition) 是一个用于开发和部署企业级应用程序的框架,它包含了多个组件和服务,如Servlet、JSP(JavaServer Pages)、EJB(Enterprise JavaBeans)等。在给定的文件列表中,...
在开发Java Web应用程序时,Spring MVC是一个非常流行的框架,它提供了强大的模型-视图-控制器架构,便于构建可维护和可扩展的Web应用。...随着你对Spring MVC框架的深入理解和实践,你的项目会变得更加健壮和高效。
### IntelliJ IDEA 12 创建 Maven 管理的 JavaEE Web 项目 #### 一、简介 本篇文章将详细介绍如何使用 IntelliJ IDEA 12 来创建一个由 Maven 管理的 JavaEE Web 项目。这不仅适用于初学者了解整个项目创建流程,...
<display-name>s3h3</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> ...
### Web.xml 详解 在Java Web开发中,`web.xml` 文件扮演着极其重要的角色,它是Web应用程序的核心配置...掌握`web.xml` 文件的编写对于深入理解Java Web开发至关重要。希望本文能帮助大家更好地理解和使用`web.xml`。
<!!-- web.xml文件 --> <?xml version="1.0" encoding="UTF-8"?...<param-value>/WEB-INF/acegi-config.xml</param-value> </context-param> <!-- Acegi 的 Filter Chain 代理 --> <filter> <filter-name>
下面将深入解析`web.xml`中的主要配置元素及其功能,帮助开发者更好地理解和运用这个强大的配置文件。 ### `<web-app>`标签 `<web-app>`是`web.xml`的根元素,它包含了整个Web应用程序的所有配置信息。根据Servlet...
这份压缩包包含的24个文件很可能是各种面试题目的集合,旨在帮助求职者准备Java和JavaEE相关的技术面试。以下是根据这些文件可能涵盖的知识点进行的详细解析: 1. **Java基础知识**:这是所有Java面试的基础,包括...
<servlet-name>JSON</servlet-name> <servlet-class>struts2.json.demo.JSON</servlet-class> </servlet> <servlet-mapping> <servlet-name>JSON</servlet-name> <url-pattern>/</url-pattern> </servlet-...
本实验旨在通过实际操作,帮助学生深入理解并掌握Spring MVC框架的使用方法,同时结合Hibernate技术进行数据库操作,以实现一个完整的Web应用开发过程。具体来说,本实验的目标包括: 1. **理解Spring MVC模型**:...
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> ...