I am trying to understand the principle of information hiding. Suppose that I have a vehicle class with methods such as getSpeed, setSpeed, getEngine, setEngine, getVIN, setVIN, etc. To enforce info hiding, I wouldn't want to give client classes the ability to setVIN since a vehicle only has one VIN (I might be wrong). I am kind of confused on how to make this class apply info hiding. I wouldn't want to make setVIN to private. But how do you set the VIN once and not allow it to be set again afterwards? Or should I even do it that way?

有帮助吗?

解决方案

Information hiding means you're not exposing the internal VIN field for direct modification from the outside. Having a setter does not violate the hiding principle, because you have the control over the fields modification.

In your case, if you want to make sure the VIN is only set once, best way to do it is by setting it in the constructor, and removing the setVIN.

BTW, although this is a general question (which is fine), if you have a specific language in mind, it might be worth mentioning. Some languages do not allow non-default constructors, for instance. In that kind of language, I'd leave the setVIN, but have it check whether the VIN has already been set when called. If it had, either ignore the call, or throw an exception.

其他提示

Just because a class / object has a property, conceptually speaking, it doesn't mean it should be public. A "property" can be assigned & changed with "getter" & "setter" functions, but you may only expose as public the ones you need.

You may say, "Show me the code":

public class JavaClass {

    // hidden field "name"
    protected String name;  

    // start property "Name"
    // note: uses "name" field to store the property
    public void setName(String value) {
        //set passed parameter as name
        name = value;
    }
    public String getName() {
        return name;
    }
    // finish property "Name"

    // start property "Id"
    // note: uses "name" field to store the property
    public void setId(String value) {
        //set passed parameter as name
        name = value;
    }
    public String getId() {
        return name;
    }
    // finish property "Id" 

    // hidden field "years"
    protected int years
    // functions works as "read-only" properties
    public int Years() {
      return years;
    }   

    // start constructor
    public JavaClass() {
            name  = "unknown";
            years = 1;
    }
    // finish constructor

    public static void main(String args[]) {     
        JavaClass javaObject = new JavaClass();

        //set name member of this object
        javaObject.setName("Visitor");

        // print the name
        System.out.println("Hello: " + javaClassExample.getName());  

        //set name member of this object
        javaObject.setId("Employee");

        // print the name, not the Id, but are the same
        System.out.println("Hello: " + javaClassExample.getName());  

        // and current years of age
        System.out.println("Years: " + javaClassExample.Years());       
    } // public static void

} // class JavaClass

Its not tested, but, I think, it explains my point.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top