文档章节

Big class decomposition in Java

银月光海
 银月光海
发布于 2016/03/18 15:20
字数 682
阅读 5
收藏 0
点赞 1
评论 0

I have just started to learn Java and is curious is it any good practice in Java for good object decomposition? Let me describe a problem. In big software project it's always a big classes like 'core' or 'ui' that tends to have a lot of methods and are intended as a mediators between smaller classes. For example, if user clicks a button on some window, this window's class sends a message to 'ui' class. This 'ui' class catches this message and acts accordingly by doing something with application user interface ( via calling method of one of it's member objects ) or by posting message to application 'core' if it's something like 'exit application' or 'start network connection'.

Such objects is very hard to break apart since they are a mere mediators between a lots of small application objects. But having a classes in application with hundreds and thousands of methods is not very handy, event if such methods are trivial task delegation from one object to another. C# solves such problem by allowing to break class implementation into multiple source files: you can divide god object any way you choose, and it will work.

Any practices by dividing such objects in Java?


====================================

One way to begin breaking such a large object apart is to first find a good subset of fields or properties managed by the large object that are related to each other and that don't interact with other fields or properties of the object. Then, create a new, smaller object using only those fields. That is, move all logic from the large class to the new smaller class. In the original large class, create a delegation method that simply passes the request along. This is a good first step that only involves changing the big object. It doesn't reduce the number of methods, but it can greatly reduce the amount of logic needed in the large class.

After a few rounds of doing this, you can begin to remove some of the delegation by pointing other objects directly at the newer, smaller objects, rather than going through the previously-huge object that was in the middle of everything.

See Wikipedia's Delegation pattern discussion for example.

As a simple example, if you have a personnel object to represent staff at a company, then you could create a payroll object to keep track of payroll-related values, a ratings object to keep track of employee ratings, an awards object to keep track of awards that the person has won, and so on.

To wit, if you started out with one big class containing the following methods, each containing business logic, among many other methods:

... public boolean isManagement() { ... } public boolean isExecutive() { ... } public int getYearsOfService() { ... } public Date getHireDate() { ... } public int getDepartment() { ... } public BigDecimal getBasePay() { ... } public BigDecimal getStockShares() { ... } public boolean hasStockSharePlan() { ... } ...

then this big object could, in its constructor, create a newly created objectStaffTypeand a newly created objectPayInformationand a newly created objectStaffInformation, and initially these methods in the big object would look like:

// Newly added variables, initialized in the constructor (or as appropriate) private final StaffType staffType; private final PayInformation payInformation; private final PayInformation payInformation; ... public boolean isManagement() { return staffType.isManagement(); } public boolean isExecutive() { return staffType.isExecutive(); } public int getYearsOfService() { return staffInformation.getYearsOfService(); } public Date getHireDate() { return staffInformation.getHireDate(); } public int getDepartment() { return staffInformation.getDepartment(); } public BigDecimal getBasePay() { return payInformation.getBasePay(); } public BigDecimal getStockShares() { return payInformation.getStockShares(); } public boolean hasStockSharePlan() { return payInformation.hasStockSharePlan(); } ...

where the full logic that used to be in the big object has been moved to these three new smaller objects. With this change, you can break the big object into smaller parts without having to touch anything that makes use of the big object. However, as you do this over time, you'll find that some clients of the big object may only need access to one of the divisible components. For these clients, instead of them using the big object and delegating to the specific object, they can make direct use of the small object. But even if this refactoring never occurs, you've improved things by separating the business logic of unrelated items into different classes.

本文转载自:http://stackoverflow.com/questions/579454/big-class-decomposition-in-java

共有 人打赏支持
银月光海

银月光海

粉丝 34
博文 341
码字总数 46223
作品 0
浦东
项目经理
CentOS 6.5 安装JDK(包含卸载原有默认JDK)

卸载原有1.7 JDK 查看是否安装了JDK 若有内容就进一步查看JDK信息 卸载 安装jdk ===================================== 安装wget 新建目录 进入目录 下载JDK 安装JDK 配置环境变量 往文件内...

阿白 ⋅ 05/23 ⋅ 0

Java基础|Java特性与HelloWorld运行流程

【Java基础】 Java语言特点:(着重了解两个)开源、跨平台。 跨平台如何实现:通过JVM实现,JVM充当Java和不同OS之间的翻译器,不同OS对应不同JVM。 Java语言的平台:JavaSE、JavaME(Androi...

darlingwood2013 ⋅ 05/29 ⋅ 0

bash: /home/jdk1.8.0_161/bin/java: Permission denied

bash: /home/jdk1.8.0_161/bin/java: Permission denied 2018年03月09日 09:04:59 阅读数:508 在ubuntu上安装jdk. 1.解压通过cuteFtp传到ubuntu服务器。 2.vim /etc/profile 编辑profile 文......

linjin200 ⋅ 05/11 ⋅ 0

win10下java的jdk安装和环境变量配置

首先需要去JAVA官网下载jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html 下载的是jdk 9 与以往的环境配置不一样 jdk是java的核心 是java软件的开...

codingcoge ⋅ 03/09 ⋅ 0

PHP调用java的class

PHP调用java的class 转:http://hi.baidu.com/lei0827/blog/item/28439a4e923234ced1c86a18.html PHP调用java的class有2种方法 1、种是用php自身带的模块调用java(不过经过网友的证明发现这...

thinkyoung ⋅ 2015/07/02 ⋅ 0

Java虚拟机标准(第10版)第一章(节选)翻译与评注

英文原文链接:https://docs.oracle.com/javase/specs/jvms/se10/html/jvms-1.html 评注是括在鱼尾号之间的文字,其余均为翻译 Java虚拟机是Java平台的基石,这种技术实现了诸如跨平台、生成...

Jelif ⋅ 06/03 ⋅ 0

sharding-jdbc源码分析—准备工作

原文作者:阿飞Javaer 原文链接:https://www.jianshu.com/p/7831817c1da8 接下来对sharding-jdbc源码的分析基于tag为源码,根据sharding-jdbc Features深入学习sharding-jdbc的几个主要特性...

飞哥-Javaer ⋅ 05/03 ⋅ 0

android -------- java虚拟机和Dalvik虚拟机

java虚拟机 虚拟机是一种抽象化的计算机,通过在实际的计算机上仿真模拟各种计算机功能来实现的。Java虚拟机有自己完善的硬体架构,如处理器、堆栈、寄存器等,还具有相应的指令系统。Java虚...

切切歆语 ⋅ 04/29 ⋅ 0

Java编程基础知识点和技术点归纳

Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互...

Java小辰 ⋅ 05/23 ⋅ 0

JavaWeb07-HTML篇笔记(二)

1.1 案例一:使用JDBC完成CRUD的操作:1.1.1 需求: 对分类管理使用JDBC进行CRUD的操作. 1.1.2 分析:1.1.2.1 技术分析: 【JDBC的概述】 Ø JDBC:Java DataBase Connectivity Java数据库的连...

我是小谷粒 ⋅ 05/16 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

从 Confluence 5.3 及其早期版本中恢复空间

如果你需要从 Confluence 5.3 及其早期版本中的导出文件恢复到晚于 Confluence 5.3 的 Confluence 中的话。你可以使用临时的 Confluence 空间安装,然后将这个 Confluence 安装实例升级到你现...

honeymose ⋅ 今天 ⋅ 0

用ZBLOG2.3博客写读书笔记网站能创造今日头条的辉煌吗?

最近两年,著名的自媒体网站今日头条可以说是火得一塌糊涂,虽然从目前来看也遇到了一点瓶颈,毕竟发展到了一定的规模,继续增长就更加难了,但如今的今日头条规模和流量已经非常大了。 我们...

原创小博客 ⋅ 今天 ⋅ 0

MyBatis四大核心概念

本文讲解 MyBatis 四大核心概念(SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession、Mapper)。 MyBatis 作为互联网数据库映射工具界的“上古神器”,训有四大“神兽”,谓之:Sql...

waylau ⋅ 今天 ⋅ 0

以太坊java开发包web3j简介

web3j(org.web3j)是Java版本的以太坊JSON RPC接口协议封装实现,如果需要将你的Java应用或安卓应用接入以太坊,或者希望用java开发一个钱包应用,那么用web3j就对了。 web3j的功能相当完整...

汇智网教程 ⋅ 今天 ⋅ 0

2个线程交替打印100以内的数字

重点提示: 线程的本质上只是一个壳子,真正的逻辑其实在“竞态条件”中。 举个例子,比如本题中的打印,那么在竞态条件中,我只需要一个方法即可; 假如我的需求是2个线程,一个+1,一个-1,...

Germmy ⋅ 今天 ⋅ 0

Springboot2 之 Spring Data Redis 实现消息队列——发布/订阅模式

一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式,这里利用redis消息“发布/订阅”来简单实现订阅者模式。 实现之前先过过 redis 发布订阅的一些基础概念和操...

Simonton ⋅ 今天 ⋅ 0

error:Could not find gradle

一.更新Android Studio后打开Project,报如下错误: Error: Could not find com.android.tools.build:gradle:2.2.1. Searched in the following locations: file:/D:/software/android/andro......

Yao--靠自己 ⋅ 昨天 ⋅ 0

Spring boot 项目打包及引入本地jar包

Spring Boot 项目打包以及引入本地Jar包 [TOC] 上篇文章提到 Maven 项目添加本地jar包的三种方式 ,本篇文章记录下在实际项目中的应用。 spring boot 打包方式 我们知道,传统应用可以将程序...

Os_yxguang ⋅ 昨天 ⋅ 0

常见数据结构(二)-树(二叉树,红黑树,B树)

本文介绍数据结构中几种常见的树:二分查找树,2-3树,红黑树,B树 写在前面 本文所有图片均截图自coursera上普林斯顿的课程《Algorithms, Part I》中的Slides 相关命题的证明可参考《算法(第...

浮躁的码农 ⋅ 昨天 ⋅ 0

android -------- 混淆打包报错 (warning - InnerClass ...)

最近做Android混淆打包遇到一些问题,Android Sdutio 3.1 版本打包的 错误如下: Android studio warning - InnerClass annotations are missing corresponding EnclosingMember annotation......

切切歆语 ⋅ 昨天 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部