0

I have gone through similar questions on this site and stackoverflow.com, but because I am still curious and want to cover all possible spectrums of this design, I am asking this question.

I am designing a new system and have lots of questions in my mind about which design works best. I have seen approach #1 and #3 used a lot, but not #2, although it is what I prefer because it is a combination of #1 and #3, also, it keeps my code clean and properly encapsulated.

If you look close enough, there are 3 variations of methods, which can fall in all 3 approaches. computePrice for example, is suitable for #3 (BookService.class), but isGraycale for #1. Is this a good design practice?

1:

class Book {
    // getters, setters
    
    public void addTOC(TOC toc) {}
    public boolean isGrayscale() {}
    public int computePrice(Conditions c) {}
}

book.addTOC(toc);

2:

class Book { 
    // getters, setters
    
    class Service {
        public void addTOC(TOC toc) {}
        public boolean isGrayscale() {}
        public int computePrice(Conditions c) {}
    }
}

book.service().addTOC(toc);

3:

class BookService {
    static void addTOC(Book b, TOC toc) {}
    static boolean isGrayScale(Book b) {}
    static int computePrice(Book b, Conditions c) {}
}

BookService.addTOC(book, toc);
  • 1
    This is pretty opinion-based, and you haven’t given much information about how these classes will be used, but #2 and #3 feel like they have additional complexity that provides no value. – James McLeod Jun 24 at 12:40
  • Thank you James – Karthik Jun 24 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.