`

<Java.JavaEE面试整理>(7) --Polymorphism之深入理解(二)

阅读更多

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
分享到:
评论

相关推荐

    cas 配置client 1.0 &2.0 及proxy DEMO 说明

    &lt;filter-name&gt;CAS Filter&lt;/filter-name&gt; &lt;filter-class&gt; edu.yale.its.tp.cas.client.filter.CASFilter &lt;/filter-class&gt; &lt;!-- server login url --&gt; &lt;init-param&gt; &lt;param-name&gt; edu.yale.its.tp...

    生活轨迹SSH服务端

    -- &lt;param-value&gt;/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml&lt;/param-value&gt; --&gt; &lt;param-value&gt;classpath:beans.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;filter&gt; &lt;filter-name&gt;...

    JAVA web.xml配置详解

    &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns=... &lt;display-name&gt;网站名称&lt;/display-name&gt; &lt;description&gt;网站描述&lt;/description&gt; &lt;!-- icon元素包含small-icon和large-icon两个子元素....

    工资管理系统

    &lt;column name="ID" type="java.lang.Integer"/&gt; &lt;/table&gt; -&lt;table schema="SCOTT" name="BIZ_CLAIM_VOUCHER"&gt; &lt;column name="ID" type="java.lang.Integer"/&gt; &lt;/table&gt; -&lt;table schema="SCOTT" name="BIZ_...

    一个在线报名系统

    &lt;display-name&gt;RegisterSy&lt;/display-name&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;index.html&lt;/welcome-file&gt; &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt; &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt; &lt;welcome-file&gt;...

    javaee-api-8.0-javadoc.jar

    javaee-api-8.0-javadoc.jar,这是javaee1.8api的jar包,解压后可用javadoc2chm制作成chm帮助文档。

    最简化velocity的web工程

    &lt;servlet-name&gt;velocity&lt;/servlet-name&gt; &lt;servlet-class&gt;org.apache.velocity.tools.view.VelocityViewServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;velocity&lt;/servlet-name&gt; ...

    spring mvc 环境搭建

    - 配置 `&lt;servlet&gt;` 和 `&lt;servlet-mapping&gt;` 标签来定义 `DispatcherServlet` 并指定其拦截的 URL 模式。 ```xml &lt;servlet&gt; &lt;servlet-name&gt;controller&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web....

    JavaEE主流开源框架-Struts部分rmvb格式.zip

    JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. ...

    160G!全新升级版 JAVAEE云计算全栈就业班课程 完美试炼JAVAEE企业级云计算应用

    ├&lt;阶段1 java语言基础&gt; │ ├&lt;1-1-Java基础语法&gt; │ ├&lt;1-2 -面向对象和封装&gt; │ └&lt;1-3-Java语言高级&gt; ├&lt;阶段2 JavaWeb·&gt; │ ├&lt;01 HTML和CSS&gt; │ ├&lt;02 JavaScript&gt; │ ├&lt;03 BootStrap&gt; │ ├&lt;04 XML&gt; │ ├...

    javaee.jar,jsf-api.jar,jsf-impl.jar,jstl-1.2.jar

    Java EE (Java Platform, Enterprise Edition) 是一个用于开发和部署企业级应用程序的框架,它包含了多个组件和服务,如Servlet、JSP(JavaServer Pages)、EJB(Enterprise JavaBeans)等。在给定的文件列表中,...

    IntelliJ IDEA maven 构建简单springmvc项目(图文教程)

    在开发Java Web应用程序时,Spring MVC是一个非常流行的框架,它提供了强大的模型-视图-控制器架构,便于构建可维护和可扩展的Web应用。...随着你对Spring MVC框架的深入理解和实践,你的项目会变得更加健壮和高效。

    IntelliJ IDEA 12创建Maven管理的JavaEE Web项目.pdf

    ### IntelliJ IDEA 12 创建 Maven 管理的 JavaEE Web 项目 #### 一、简介 本篇文章将详细介绍如何使用 IntelliJ IDEA 12 来创建一个由 Maven 管理的 JavaEE Web 项目。这不仅适用于初学者了解整个项目创建流程,...

    web xml 详解

    ### Web.xml 详解 在Java Web开发中,`web.xml` 文件扮演着极其重要的角色,它是Web应用程序的核心配置...掌握`web.xml` 文件的编写对于深入理解Java Web开发至关重要。希望本文能帮助大家更好地理解和使用`web.xml`。

    一个简单的Acegi入门实例

    &lt;!!-- web.xml文件 --&gt; &lt;?xml version="1.0" encoding="UTF-8"?...&lt;param-value&gt;/WEB-INF/acegi-config.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- Acegi 的 Filter Chain 代理 --&gt; &lt;filter&gt; &lt;filter-name&gt;

    java和javaEE面试题大全打包-24个文件

    这份压缩包包含的24个文件很可能是各种面试题目的集合,旨在帮助求职者准备Java和JavaEE相关的技术面试。以下是根据这些文件可能涵盖的知识点进行的详细解析: 1. **Java基础知识**:这是所有Java面试的基础,包括...

    serverlet4Json

    &lt;servlet-name&gt;JSON&lt;/servlet-name&gt; &lt;servlet-class&gt;struts2.json.demo.JSON&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;JSON&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-...

    WEB高级编程实验报告之Spring-MVC.doc

    本实验旨在通过实际操作,帮助学生深入理解并掌握Spring MVC框架的使用方法,同时结合Hibernate技术进行数据库操作,以实现一个完整的Web应用开发过程。具体来说,本实验的目标包括: 1. **理解Spring MVC模型**:...

    struts2配置2.5版

    &lt;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"&gt; ...

Global site tag (gtag.js) - Google Analytics