扩展bean类并编写一个包含逻辑的类来填充我们扩展的bean是正确的吗?

时间:2014-04-09 10:55:55

标签: java design-patterns javabeans

我需要从ManagedBeans中提取一些指标,并将收集的指标存储在对象bean中。 使用成员变量定义对象bean以保存度量。在这种情况下,扩展ObjectBean并编写提取器类是否正确?

例如:

class ThreadMetricsBean {
    private long liveCount, daemonCount;

    public void record(long liveCount, long daemonCount) {
        this.liveCount = liveCount;
        this.daemonCount = daemonCount;
    }
}

**// Here to avoid code redundancy I feel it's correct to extend the  ThreadMetricsBean class
// Am I wrong ??** 
class ThreadMetricsExtractor {
    private long liveCount, daemonCount;

    public void run() throws Exception{
        extractMetrics();
        recordToBean();
    }

    private void collectMetrics() {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        this.liveCount = threadMXBean.getThreadCount();
        this.daemonCount = threadMXBean.getDaemonThreadCount();
    }

    private void recordToBean() {
        new ThreadMetricsBean().record(liveCount, daemonCount);
    }
}

0 个答案:

没有答案