Split one big class on sub-classes

I have a generic question and I don’t understand how create what I want.
I writing a basic program they can access to one database an do some SQL request, etc. But at the moment I have one big class with all SQL for all the tables from the database. And I want slipt this class in different classes for make the code more easy to read and implement stuff. I think the best solution is create one subclass for each table on the database and on for this class put all the methods related on this class. But later I don’t understand how I can call this methods from the class. Because for access to the database I create an object DataBase on the main application and if I want call a method just I do a database.methodIWant().

At the moment I have:

  • DataBase (this class have all the methods for the databases) extends DataBaseUtil

  • DataBaseUtil (this class contain all methods and information for connect to the database)

and I crate the database object with DataBase db = new DataBase();

And I want something like:

  • DataBase (this class have all the methods for the databases) extends DataBaseUtil

  • DataBaseUtil (this class contain all methods and information for connect to the database)

and classes for example:

  • DogSQL (all the methods for the table dog on the database)

  • CatSQL (all the methods for the table cat on the database)

and I create the database object with DataBase db = new DataBase(); and call methods like db.methodFromDogSQL();

I don’t understand how do this. I don’t understand if a need use abstract, interface, inheritance for this or which is the best implementation for this. I’m starting and my knowledge in java is a bit basic, so I want learn how do this type of stuff.

2

There is no need for using subclasses here, Dog and Cat should not be derived from Database (in fact, this would result in a typical example of how to not to use inheritance).

Instead, you could have the Create/Read/Update/Delete methods of the classes Dog or Cat just have a parameter of type DataBase (or IDataBase, an interface, if you want to be able to mock the database object for testing). For example, there should not be a method in DataBase like db.SelectDog(int Id), only a (maybe static) method in the class Dog like Dog.Select(IDatabase db, int Id). That method could return a Dog object, which corresponds to a row in your Dog table.

The “Dog.Select” method may contain the SQL code needed and call some general methods of IDataBase for selecting data, but in no way should your database class have knowledge about the SQLs needed for specific business objects.

I suggest you study Inversion of Control and the dependency injection pattern. Take a look at this Martin Fowler article on dependency injection. Using an IoC container will help you break up your class into more manageable components and make your solution less brittle than it is now.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật