1    课堂概念

1.0  继承关系的选择

1.1  起名

1.2  定义

1.3  组合聚合优缺点

1.4  继承优缺点

1.5  组合聚合区别

 

2    代码演练

2.1  反例

2.2  正例

 

3    疑问解答
3.1  疑问解答

 

1    课堂概念
1.0  继承关系的选择(自己理解)

搜索,官方解答,说继承关系是is a的关系,在我看来,正方形is a 长方形,详看3.1解答和上节的案例。

所以看是否有继承关系,通常要看子类和父类共用的方法,子类是否能够实现实现父类的方法。(自己理解)

 

1.1  起名

合成复用原则,组合复用原则,聚合复用原则

 

1.2  定义

尽量使用组合,聚合,而不是继承关系达到复用软件的目的。

 

1.3  组合聚合(黑箱复用)

优点:降低耦合,提高系统的灵活性。使一个类的变化对其他类造成的影响较小。

缺点:会生成较多的对象进行管理。

 

1.4  继承(白箱复用)

优点:新的扩展性容易实现,修改和扩展相对容易。

缺点:父类的方法侵入性的带给子类,父类方法的改变,子类也必须改变,相比耦合较高。

 

1.5  组合聚合区别

关系强弱,组合强,聚合弱。

 

 

 

2    代码演练
2.1  反例

 类关系:

 

测试类:

package com.geely.design.principle.compositionaggregation;

public class TestBizDao {
    public static void main(String [] args){
        BizDaoNegative bizDaoNegative = new BizDaoNegative();
        bizDaoNegative.getDBConnection();
    }

//    public static void main(String [] args){
//        BizDao bizDao = new BizDao();
////        DBConnection dbConnection = new MySqlConnection();
////        bizDao.setDbConnection(dbConnection);
//        DBConnection dbConnection = new OracleConnection();
//        bizDao.setDbConnection(dbConnection);
//        bizDao.handleBiz();
//    }
}

 

dao类:

package com.geely.design.principle.compositionaggregation;

public class BizDaoNegative extends DBConnectionNegative{

    @Override
    public void getDBConnection() {
        super.getDBConnection();
    }
}

 

获取连接类:

package com.geely.design.principle.compositionaggregation;

public class DBConnectionNegative {
    public void getDBConnection(){
        System.out.println("打开MySql连接");
    }
}

 

打印结果:

"C:\Program Files\Java\jdk1.6.0_43\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=9805:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.6.0_43\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\out\production\design_pattern" com.geely.design.principle.compositionaggregation.TestBizDao
打开MySql连接

Process finished with exit code 0

 

2.2  正例

类图:

 

测试类:

package com.geely.design.principle.compositionaggregation;

public class TestBizDao {
//    public static void main(String [] args){
//        BizDaoNegative bizDaoNegative = new BizDaoNegative();
//        bizDaoNegative.getDBConnection();
//    }

    public static void main(String [] args){
        BizDao bizDao = new BizDao();
        DBConnection dbConnection = new MySqlConnection();
//        bizDao.setDbConnection(dbConnection);
//        DBConnection dbConnection = new OracleConnection();
        bizDao.setDbConnection(dbConnection);
        bizDao.handleBiz();
    }
}

 

dao类:

package com.geely.design.principle.compositionaggregation;

public class BizDao {
    private DBConnection dbConnection;

    public void setDbConnection(DBConnection dbConnection) {
        this.dbConnection = dbConnection;
    }

    public void handleBiz(){
       dbConnection.getConnection();
    }
}

 

获取连接抽象类:

package com.geely.design.principle.compositionaggregation;

public abstract class DBConnection {
    public abstract void getConnection();
}

 

获取Mysql连接类:

package com.geely.design.principle.compositionaggregation;

public class MySqlConnection extends DBConnection{
    @Override
    public void getConnection() {
        System.out.println("打开MySql连接");
    }
}

 

获取Oracle连接类:

package com.geely.design.principle.compositionaggregation;

public class OracleConnection extends DBConnection{
    @Override
    public void getConnection() {
        System.out.println("打开oracle连接");
    }
}

 

 打印结果

"C:\Program Files\Java\jdk1.6.0_43\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=12553:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.6.0_43\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\out\production\design_pattern" com.geely.design.principle.compositionaggregation.TestBizDao
打开MySql连接

Process finished with exit code 0

 

 

3    疑问解答
3.1  疑问解答

 

posted on 2019-07-22 19:52  菜鸟乙  阅读(377)  评论(0编辑  收藏  举报