mardi 23 juin 2020

Java, Business Logic Encapsulation: Utility vs Inner vs Pojo

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 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.

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);

Aucun commentaire:

Enregistrer un commentaire