确定构造函数,初始化和重置方法的任务的最佳实践是什么 [英] What are the best practices for determining the tasks of Constructor, Initialization and Reset methods

查看:214
本文介绍了确定构造函数,初始化和重置方法的任务的最佳实践是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个一般的OOP问题,虽然我在Java设计。我不想解决一个特定的问题,只是想通过一些设计原则。

从我的经验,我已经达到的习惯将对象设置分为三个阶段。

This is a general OOP question although I am designing in Java. I'm not trying to solve a particular problem, just to think through some design principles.
From my experience I have reached the habit segregating object setup into three phases.

目标是最大限度地减少:额外的工作,模糊的代码和瘫痪的可扩展性。

The goal is to minimize: extra work, obfuscated code and crippled extensibility.


构造



  1. 所需的最少操作创建一个有效的对象,传递
    存在测试

  2. 实例化和初始化只有一次,不会被覆盖,非变量对象不会改变/改变对象的生命

  3. 初始化最终成员

  4. 基本上是运行时存根

  1. The minimal actions necessary to create a valid Object, passes an existence test
  2. Instantiate and initialize only "one time", never to be over-ridden, non variable objects that will not change/vary for the life of the Object
  3. Initialize final members
  4. Essentially a runtime stub

初始化

Initialization


  1. 使对象有用

  2. 实例化并初始化可公开访问的成员

  3. 实例化和初始化变量值的私有成员

  4. 对象现在应该通过外部测试而不生成异常(假设代码是正确的)

  1. Make the Object useful
  2. Instantiate and initialize publicly accessible members
  3. Instantiate and initialize private members that are variable values
  4. Object should now pass external tests with out generating exceptions (assuming code is correct)

重置


  1. 不实例化任何内容

  2. 为所有变量公共/私有成员分配默认值

  3. 将对象返回到确切状态

玩具示例:

public class TestObject {
   private int priv_a;
   private final int priv_b;
   private static int priv_c;
   private static final int priv_d = 4;

   private Integer priv_aI;
   private final Integer priv_bI;
   private static Integer priv_cI;
   private static final Integer priv_dI = 4;  

   public int pub_a;
   public final int pub_b;
   public static int pub_c;
   public static final int pub_d = 4;

   public Integer pub_aI;
   public final Integer pub_bI;
   public static Integer pub_cI;
   public static final Integer pub_dI = 4;   

   TestObject(){
        priv_b = 2;
        priv_bI = new Integer(2);
        pub_b = 2;
        pub_bI = new Integer(2);
   }

   public void init() {
       priv_a = 1;
       priv_c = 3;
       priv_aI = new Integer(1);
       priv_cI = new Integer(3);

       pub_a = 1;
       pub_c = 3;
       pub_aI = new Integer(1);
       pub_cI = new Integer(3);
   }

   public void reset() {
       priv_a = 1;
       priv_c = 3;
       priv_aI = 1;
       priv_cI = 3;

       pub_a = 1;
       pub_c = 3;
       pub_aI = 1;
       pub_cI = 3;
   }  
}


推荐答案

I将以一种方式设计我的类,使得不需要init方法。我认为一个类的所有方法,特别是公共方法,应该保证对象在它们成功完成后始终处于有效状态,并且不需要调用其他方法。

I would design my classes in a way so that an "init" method is not required. I think that all methods of a class, and especially public methods, should guarantee that the object is always left in a "valid" state after they complete successfully and no call to other methods is required.

同样适用于构造函数。当一个对象被创建,它应该被认为是初始化和准备使用(这是什么构造函数,有很多技巧来实现这一点)。否则,你可以安全地使用它的唯一方法是检查对象是否已在每个其他公共方法的开头初始化。

The same holds for constructors. When an object is created, it should be considered initialized and ready to be used (this is what constructors are for and there are numerous tricks to achieve this). Otherwise, the only way that you can safely use it is checking if the object has been initialized in the beginning of every other public method.

这篇关于确定构造函数,初始化和重置方法的任务的最佳实践是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆