nicklowkc February 2016

Some questions about java coding style

I have been studying java for like 2 months and I'm familiar with classes, inheritance and also a little bit about swing. I have seen some experts' coding style and I do fell a little bit unfamiliar with it. For example: 1)

import java.util.Calendar;

    class testIni2{
      public static void main(String args[]){
        Calendar cal = Calendar.getInstance();

        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);

        System.out.println("Today's date is" + month + " " + day );
      }
    }

From my previous knowledge, if you want to create an instace of a class, most of the style I have seen is like below:

Calendar cal = new Calendar();

followed by calling the methods, in this case I think its like this:

cal.getInstance();

I understand the rest of it. Does it mean that I can directly call the method when I am creating the instance of the class? Lets say if I have a dog class and inside it has a method called 'bark', I can do something like this:

Dog cutie = Dog.bark();      // Like this??

2) http://zetcode.com/tutorials/javaswingtutorial/firstprograms/

I read a tutorial about swing from this site, and another question about its coding style.

import java.awt.EventQueue;
import javax.swing.JFrame;

public class SimpleEx extends JFrame {

    public SimpleEx() {

        initUI();
    }

    private void initUI() {

        setTitle("Simple example");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SimpleEx ex = new SimpleEx();
                       

Answers


radai February 2016

what youre seeing is the factory pattern. the class author (whoever wrote Calendar) decided that, for whatever reason, they dont want the class' user (you, in this case) to create a Calendar directly.

usually this is because:

  1. there are various kinds of calendars, and which one you get is basically none of your business :-)
  2. maybe building a new Calendar is a lot of work and the library only has one, so everyone who calls the factory method gets the same one.
  3. building a Calendar is too complicated for most mere mortals.

as for your Dog line:

Dog cutie = Dog.bark();

bark() is a bad name for a method that returns a dog - very unintuitive. lets say, for example that your Dog class is very complicated - it has age, sex, breed, color, and size. and every dog must have all those specified. you could make it easier for developers to create dogs by either implementing a DogFactory:

public class DogFactory {
   public static Dog newDog() {
      return new Dog(1, Sex.MALE, Breed.PUDLE, brown, 40);
   }
}

a related design pattern would be the builder, where code using a builder could look like this:

Dog lassie = new DogBuilder().called("lassie").ofBreed(Breed.COLLIE).ofHeight(60).build();

the internal implementation of a builder is slightly more complicated than a factory

Post Status

Asked in February 2016
Viewed 1,205 times
Voted 8
Answered 1 times

Search




Leave an answer