Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Looking for good guide or advice on how to split up classes and methods?  RSS feed

 
Andrew Great
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know any good tutorials, ideally in YouTube type format, about how to use classes and method correctly?
While I think I've understood the basics of how to create them, I'm not sure if I'm using them correctly.
I think in the sample application I've built, I might have over "methodized" it a little, while only using 2 classes.

I've created a sample application which uses MySQL, and JSoup. I created 3 classes, Service from which main() runs, MySQL for all MySQL related stuff, JSoup for all JSoup relaterd stuff.

Within the MySQL class, I have created methods for every single MySQL action, for example:
insertCustomer - Adding customer to db
updateCustomer - Updating customer details
getCustomer - Returning customer details
Plus about 40 more....

Likewise within the JSoup class, I've got methods like getPage(), sendForm(), etc...

I appeared to have treated these more like PHP functions than Java methods.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naming a class MySQL or JSoup is both dangerous and misleading. It may potentially create a name conflict with an imported library. Also, it doesn't really describe what it is used for. If you have a "customer" database set up in MySQL then I might name the class "CustomerDB". Often you'll have an enclosing database that contains a Customer table. In that case I'll have something like EnclosingDB and CustomerTable classes. When working with SQL the goal should be database independence. Because databases are not 100% compatible this is not always achievable but if you keep that as a goal then you'll have fewer problems should you need to migrate to a different database.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt there's any step-by-step guide that can cover all possible scenarios.  IMO, it's better to study principles and understand how and when to apply them in different situations.

My recommendation would be to read a couple of books, Clean Code and Agile Software Development - Principles, Patterns, and Practices (also known as the PPP book) written by Robert C. Martin -- pay particular attention to the parts about principles.

Take what you wrote, for example:
Andrew Great wrote:
Within the MySQL class, I have created methods for every single MySQL action, for example:
insertCustomer - Adding customer to db
updateCustomer - Updating customer details
getCustomer - Returning customer details
Plus about 40 more....

You are probably already violating some design principles here.  One principle is that of keeping abstractions separate from implementations. MySQL is an implementation detail; it's a specific database technology. The abstraction is a DataStore or Repository.  You have, however, mixed the two together. On the surface, it may seem perfectly fine to you. However, consider what happens if you decide to switch to MongoDB or some other technology in the future.  Any code that directly depends on the MySQL class must now also be modified and re-tested. If, however, you use an implementation-agnostic interface that defines a contract for behavior of a CustomerDataStore, it would be easier to replace a MySQL-based implementation of that interface with say a MongoDB-based implementation as long as it adhered to the contract specified by the CustomerDataStore interface.

This is the kind of robustness and flexibility that separating abstractions from implementations can give you.

Another design principle, which is discussed in the PPP book, is the Interface Segregation Principle (ISP) which encourages small, focused interfaces. With 40+ methods in your data access class, you are probably in violation of ISP.

This may seem above and beyond what you asked regarding how to split up classes and methods but they really are connected. Deciding how to apportion code into classes and methods is about identifying appropriate abstractions and properly assigning responsibilities to those abstractions. Design principles like those that Bob Martin discusses in his books provide the best guidelines that I know for doing that properly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!