Skip to main content
Tweeted twitter.com/StackSoftEng/status/1323505200083177472
Small touchups.
Source Link
Carcigenicate
  • 2.7k
  • 3
  • 25
  • 39

I've read a lot about dependency inversion principle but still, I can't apply it to my case. I just don't know when I should apply it and when not. I write a simple application in Java to generate invoices. For now, I have basic classes: Client, Product, InvoiceLine and Invoice. Should these classes communicate through interfaces? For instance, I have a method in Product for getting name of the prductproduct:

public String getName() {
        return name;
    }

And I use this method in class Invoice

public void addLineToInvoice(Product product, int quantity) {
        rows.add(new InvoiceLine(rows.size(), product.getName(), quantity, product.getPrice()));
    }

Now, should I create an interface for Product? Or is it unnecessary?

I've read a lot about dependency inversion principle but still, I can't apply it to my case. I just don't know when I should apply it and when not. I write a simple application in Java to generate invoices. For now, I have basic classes: Client, Product, InvoiceLine and Invoice. Should these classes communicate through interfaces? For instance, I have a method in Product for getting name of the prduct:

public String getName() {
        return name;
    }

And I use this method in class Invoice

public void addLineToInvoice(Product product, int quantity) {
        rows.add(new InvoiceLine(rows.size(), product.getName(), quantity, product.getPrice()));
    }

Now, should I create an interface for Product? Or is it unnecessary?

I've read a lot about dependency inversion principle but still, I can't apply it to my case. I just don't know when I should apply it and when not. I write a simple application in Java to generate invoices. For now, I have basic classes: Client, Product, InvoiceLine and Invoice. Should these classes communicate through interfaces? For instance, I have a method in Product for getting name of the product:

public String getName() {
    return name;
}

And I use this method in class Invoice

public void addLineToInvoice(Product product, int quantity) {
    rows.add(new InvoiceLine(rows.size(), product.getName(), quantity, product.getPrice()));
}

Now, should I create an interface for Product? Or is it unnecessary?

Question Protected by Thomas Owens
Became Hot Network Question
Source Link
Awerde
  • 327
  • 2
  • 5

Is dependency inversion principle necessary?

I've read a lot about dependency inversion principle but still, I can't apply it to my case. I just don't know when I should apply it and when not. I write a simple application in Java to generate invoices. For now, I have basic classes: Client, Product, InvoiceLine and Invoice. Should these classes communicate through interfaces? For instance, I have a method in Product for getting name of the prduct:

public String getName() {
        return name;
    }

And I use this method in class Invoice

public void addLineToInvoice(Product product, int quantity) {
        rows.add(new InvoiceLine(rows.size(), product.getName(), quantity, product.getPrice()));
    }

Now, should I create an interface for Product? Or is it unnecessary?