Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Constructor Functionality

  • 21-05-2013 5:43pm
    #1
    Registered Users, Registered Users 2 Posts: 2,815 ✭✭✭


    I'm studying OO in college and we've only ever used constructors to set variable values.

    It is acceptable / best practice to perform more complex operations within a constructor such as querying a db to retrieve data and than setting variable values based on that data?

    For example, if I have a class called carsCollection which contains an ArrayList of class Car, could the constructor retrieve from the db the data about cars, create a Car object for each car and add it to the carsCollection ArrayList?

    If this isn't a good technique, what are the better alternatives?

    Thanks.


Comments

  • Registered Users, Registered Users 2 Posts: 1,686 ✭✭✭RealistSpy


    Well it really depends on what you are doing and the situation. If you are creating a library or a framework you might want to have a method for initialising different variables. The reason behind this is to make your code more extensible. Sometime you need properties (getter and setters). In most cases I personally have multiple constuctors for libraries. In one case a constructor that accepts nothing (default) and initialise all veriable to null or 0 *depending on what you have. Another is an overloaded constructor.

    Hope that helps
    Good luck.


  • Registered Users, Registered Users 2 Posts: 2,815 ✭✭✭SimonTemplar


    Thanks for your reply.

    I suppose what I'm really asking is this - if an object requires data from an external source (such as a db or xml file), I would prefer for that data request to be abstracted within the class creation / object instantiation process.

    For example, I could write

    requiredData = db.getRequiredData();
    Class myClass = new Class(requiredData);

    However, it seems much better to simple write

    Class myClass = newClass();

    and let the class itself worry about getting its required data.


    If you agree with this approach, my question is should you put the db.getRequiredData() method call in the classes constructor. Something tells me this isn't a good idea so I'm wondering what is best practice here?

    Thanks.


  • Registered Users, Registered Users 2 Posts: 1,686 ✭✭✭RealistSpy


    I believe Class myClass = newClass(); is the best approach and requiredData would be class property. Look into the singleton pattern (lazy vs eager initialisation). I can't really say the best practice because there are different patterns you can apply.


  • Registered Users, Registered Users 2 Posts: 6,334 ✭✭✭OfflerCrocGod


    Yes look into the Singleton pattern, so that you learn never to use it.

    The best style, at least in my opinion is to inject as many dependencies as you can into your classes. Try and keep your class code dealing with domain concerns and concepts not with implementation details such as where the data/state it requires comes from.

    Read up a bit on separation of concerns and dependency injection. I think these things always matter in that they help reason about your code but they matter even more when you expect to work with others and if you want to test your classes.


  • Registered Users, Registered Users 2 Posts: 2,815 ✭✭✭SimonTemplar


    Thanks. I did a bit of research on dependency injection and that solves the problem.


  • Advertisement
Advertisement