-3

Can somebody tell me or show me something that would make me understand the get and set methods completely? I know some of it already but it still confuses me.

I am trying to learn the MVC Design Pattern but I find it hard because I haven't completely understand this. I thought it was easy but it's not really that easy. Well, at least for me.

Your own example would be appreciated. Thank you in advance guys :)

Juju
  • 59
  • 1
  • 1
  • 8

3 Answers3

2

The Model, View, Controller design pattern is a useful way of decoupling the various components of an GUI driven application. It improves cohesion, which essentially emphasises the responsibility of discrete elements of your software and helps avoid unnecessary overlapping of functionality.

The Model stores what is referred to as 'business logic'. This means it houses all of the data which is core to your application.

The View is what handles the graphical interface. Everything responsible for managing how your graphics are rendered is defined here.

Finally, the Controller handles events. This includes asynchronous events such as whenever a key has been pressed, or the mouse has been moved, or the user has touched their screen. It receives these events and decides what to do with them.

So, how they come together is as follows; the Model defines what needs to be drawn. Any graphics the View needs to render is therefore housed in the Model. This means that any modifications to the Model's data will in turn effect what is drawn on the screen; however, the Model is only really defining what elements need to be drawn, it has no clue how to draw them; just how to manage them and manipulate them. It's the View which can take these elements and in turn use them within a graphical context. The controller on the other hand, handles events and in turn manipulates the contents of the Model. It does this by using a defined set of rules on how each input event will affect certain parts of the Model.

So, in this regard, the Model, View, Controller can be looked at like this:

final Model m      = new Model();
final View  v      = new View(m);
final Controller c = new Controller(m);

Both the Controller and View need access to the Model in order to manage and render the application respectively, but the Model doesn't care about either of them. This is because the Model defines the core data dependencies of your application, which should be transferrable, and work independently of whether it's a component of a GUI or not.

In terms of getter and setter methods, all these do are provide access to a member variable sitting inside a class. So if we were to look inside the View, we would see something like this:

public final class View {

    /* Member Variables. */
    private final Model mModel;

    public View(final Model pModel) {
        /* Initialize Member Variables. */
        this.mModel = pModel;
    }

    public final Model getModel() {
        return this.mModel;
    }

}

The method getModel() is referred to as a getter method; it's sole responsibility is to return a variable; in this case it returns the View's mModel variable. What's useful about getter and setter methods is that you can control access to that variable; the method can be declared public, protected and private for example, which all change just who else inside your application can get access to the Model. The same goes for a setter method, whose only responsibility should be to change the value of a specific variable belonging to the owning Object.

Community
  • 1
  • 1
Mapsy
  • 4,192
  • 1
  • 37
  • 43
0

Getter and setter methods used when we define instance variables private so from outside class we can't access private instance variables directly which is useful for encapsulation(hiding data from outside world). So for accessing private variables we required some methods which is basically getter and setter.

public class Employee
{
  private int empNum;

  public Employee(int empNum) {
    this.empNum = empNum;
  }
  public int getEmpNum() {
    return empNum;   
  }
  public void setEmpNum(int empNum) {
    this.empNum = empNum;   
  }
}

for more reasons why we use getter/setter read this answer
There is no direct relation between MVC and getter/setter methods. MVC is basically design patter for software development where we divide task between different modules(model, view and controller)

model -> Data layer

view -> Representational layer

controller -> Controller layer between model and view

So when you create model class in mvc you define multiple instance variables(attributes) for model but from controller you don't want to access that variables directly so in that case you should use getter setter methods.

Actually getter/setter concept in not limited to just mvc it is use as a codding standard and for abstraction purposes.

Community
  • 1
  • 1
nilesh virkar
  • 429
  • 2
  • 12
0

I know this should be a comment, but I currently don't have the reputation required to post a comment.

First of all, could you please provide more information about what specifically you'd like to know? Are you confused about how getters and setters work in general? Are you confused about how the work in an MVC pattern? (getters and setters work the same way in MVC as they do in other design patterns).

If the link posted in the comment above doesn't solve your answer, then hopefully I can help. Getters and setters (getVarName and setVarName) are used to provide additional functionality (like ensuring that a value fits a desired range, etc) and also to provide encapsulation of your code. Besides the additional functionality (like validation), encapsulation also helps avoid errors like accidentally changing the value of a class's variable when you don't mean to. Take a Customer class for example:

public class Customer {
private int empNo;
private int deptNo;
//additional class variables

public Customer() {
//default constructor }

public Customer (int emp, int dept) {
empNo = emp;
deptNo = dept;
}

public int getEmpNo() {
return empNo;
}

public void setEmpNo(int emp) {
empNo = emp;
}

//other methods
}

Let's say that all employee numbers must be 5 digits long and not start with a 0. If we don't use a setter, then there's no way to check if the number given is a valid number (or that it was even given). For that, we could write a simple validation requirement in our setEmpNo method.

public void setEmpNo(int emp) {
    if(emp >= 10000 && emp <= 99999) {
        empNo = emp;
    }
//code to handle invalid numbers
}

Encapsulation also helps us avoid certain errors, like changing the value of empNo when we mean to just check the value in a condition, etc. For instance, if we don't use getters and setters and just have a public empNo, the following typo would change the value of the employee's employee number:

if(employee1.empNo = 12345) { //checking if this is employee 12345 would use ==
    //perform action for specified employee
}

However, if we use getters and setters, we'd still run into a problem because we're not checking if the desired employee's employee number is 12345, but that employee's number would NOT be permanently changed to 12345 and would still retain his/her correct employee number. Does this make sense?

It looks like someone already posted a pretty good answer about MVC, so I won't repeat any info on that. One thing I will point out is that MVC is usually (if not always) used for server-based apps. If you have an app that contains a website that users interact with and a database, then there's a good chance you'll use some variant of the MVC pattern. However, you're not going to use MVC for something like a Hello World app.

I hope my answer isn't too basic. It's hard to judge a user's knowledge level without getting additional info. If you'd like me to clarify or give further explanation on anything I've posted, let me know.

Best of luck going forward.

jeffkempf
  • 288
  • 1
  • 3
  • 11