frazman
frazman

Reputation: 33223

Understanding design pattern in java

I read in somewhere that developer should always be developing interfaces and then implement that interface. I am just trying to learn these builder patterns,I guess.. but here is my use case...

I have a central class... say ProcessFile

Now, I am writing two formats it can process, one csv another json.

How do I design these classes.

I am guessing it would be something liek this:

 public interface CustomFormat{
  //couple of signatures
}
public class csv implements CustomFormat{

}

public class json implements CustomFormat{
}
public class ProcessFiles{
//somehow uses that CustomFormat interface??
}

Whats the best way to implement this?

Upvotes: 0

Views: 133

Answers (2)

Franklin
Franklin

Reputation: 1801

To build on javaPlease42's answer. I would rename ProcessFiles to FileProcessor or something along those lines. A good rule of thumb is to name classes using nouns and methods using verbs to describe what the noun can do. Below is an example of what it could look like.

public class FileProcessor {

    public void process(CustomFormat file) {
        // pass in the interface, that way you can use any implementing class 
        // like csv or json
    }

}

Upvotes: 1

javaPlease42
javaPlease42

Reputation: 4963

Whats the best way to implement this?

I would not use the word Custom in the interface name. Maybe FileFormat.java

public interface FileFormat {

    public void setFormat(int quantity);
}

CSV.java

public class CSV implements FileFormat{

    @Override
    public void setFormat(int quantity) {
        // TODO Auto-generated method stub

    }
}

JSON.java

public class JSON implements FileFormat{

    @Override
    public void setFormat(int quantity) {
        // TODO Auto-generated method stub

    }
}   

Upvotes: 0

Related Questions