Jak to poukładać poniży kod według wytycznych GRASP i Agile
Głównie chciałbym aby klasy miały wysoką specjalizację, czyli charakteryzować się wysokim współczynnikiem kohezji i być luźno powiązane (coupling).
Poniżej mam kod który jest napisany jak leci a chciałbym aby był zgrabny.
także nie czuję kiedy używać dziedziczenia a kiedy kompozycji.



  1. package SQL;
  2.  
  3.  
  4. public class ConnectToDB
  5. {
  6. public static SqlConnector connector = new SqlConnector();
  7.  
  8. public static int dbSize(){
  9.  
  10. int dbSize1= connector.dbSize();
  11. return dbSize1;
  12. }
  13. /**
  14. * metoda statyczna uruchamiająca polaczenie do DB
  15. */
  16. public static SqlConnector startConnectToBD()
  17. {
  18. String nazwaHosta = "xxx";
  19. String nazwaBazy = "xxx";
  20. String uzytkownik = "xxx";
  21. String haslo = "xx";
  22. //static SqlConnector connector = new SqlConnector();
  23.  
  24. /*
  25. * Wczytujemy sterownik bazy danych, łączymy się z naszą bazą danych
  26. */
  27.  
  28. connector.loadDriver();
  29. connector.connectToDB(nazwaHosta, nazwaBazy, uzytkownik, haslo);
  30. return connector;
  31. }
  32.  
  33.  
  34.  
  35. }
  36.  




i kolejna klasa:

  1. package SQL;
  2.  
  3. import java.sql.*;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. /**
  8.  * @author Ewa
  9.  *
  10.  */
  11. /**
  12.  * @author Ewa
  13.  *
  14.  */
  15. public class SqlConnector
  16. {
  17.  
  18. /*
  19. * To jest nasz obiekt, dzięki któremu wgramy sterownik, połączymy się z
  20. * bazą danych, wykonamy zapytania.
  21. */
  22.  
  23. private Connection connection = null;
  24. private Statement statement = null;
  25. private PreparedStatement preparedStatement;
  26. private ResultSet resultset;
  27.  
  28. /*
  29. * Ładujemy sterownik bazy danych, sterownik MySQL.
  30. */
  31.  
  32. public void loadDriver()
  33. {
  34.  
  35. try
  36. {
  37.  
  38. Class.forName("com.mysql.jdbc.Driver");
  39.  
  40. } catch (ClassNotFoundException e)
  41. {
  42.  
  43. System.out
  44. .println("Class :"
  45. + this.getClass().getPackage()
  46. + this.getClass().getName()
  47. + "Wyglda na to, że nie załadowałeś sterownika bazy danych. Sprawdz czy posiaasz <a href="http://dev.mysql.com/downloads/&quot%3b%29;" target="_blank">http://dev.mysql.com/downloads/");</a>
  48. e.printStackTrace();
  49. return;
  50.  
  51. }
  52.  
  53. System.out.println("Sterownik MySQL JDBC załadowany. # "
  54. + this.getClass().getName());
  55. }
  56.  
  57. /*
  58. * Łączymy się z danych za pomocą zadeklarowanych zminnych
  59. */
  60.  
  61. public void connectToDB(String nazwaHosta, String nazwaBazy,
  62. String uzytkownik, String haslo)
  63. {
  64.  
  65. String host = nazwaHosta;
  66. String db = nazwaBazy;
  67. String user = uzytkownik;
  68. String password = haslo;
  69. System.out.printf("jdbc:mysql://" + host + ":3306/" + db, user,
  70. password);
  71. try
  72. {
  73. connection = DriverManager.getConnection("jdbc:mysql://" + host
  74. + ":3306/" + db, user, password);
  75.  
  76. } catch (SQLException e)
  77. {
  78.  
  79. System.out
  80. .println("\nCoś poszło nie tak w SqlConnector.connectToDB()");
  81. e.printStackTrace();
  82. return;
  83.  
  84. }
  85.  
  86. if (connection != null)
  87. {
  88. System.out.println("\n Połączenie udane.");
  89. } else
  90. {
  91. System.out.println("\nPołączenie nieudane.");
  92. }
  93. }
  94.  
  95.  
  96.  
  97. public void setCountInToFILM(int id_slowka_ang, int ilosc_wystapien)
  98. {
  99. System.out.println("Start setCountInToFILM()"
  100. + this.getClass().getName());
  101. try
  102. {
  103.  
  104. String query = "INSERT INTO Film (id_slowka_ang, ilosc_wystapien) VALUES ('"
  105. + id_slowka_ang + "','" + ilosc_wystapien + "' )";
  106. System.out.println(query);
  107. statement = connection.createStatement();
  108.  
  109. statement.execute(query);
  110.  
  111. }
  112.  
  113. catch (NullPointerException e)
  114. {
  115.  
  116. System.out.println("problem w NullPointerException");
  117. e.printStackTrace();
  118.  
  119. }
  120.  
  121. catch (SQLException e)
  122. {
  123. System.out.println("Coś poszło nie tak.w setFilm "
  124. + this.getClass().getName());
  125. e.printStackTrace();
  126. }
  127.  
  128. }
  129.  
  130. public Word getWordFromDictionary(int id_slowka)
  131. {
  132.  
  133. String slowko_ang = null;
  134. String slowko_pl = null;
  135.  
  136. try
  137. {
  138.  
  139. String query = "SELECT slowko_ang, slowko_pl FROM dictionary WHERE id_slowka = "
  140. + id_slowka;
  141.  
  142. preparedStatement = connection.prepareStatement(query);
  143. resultset = preparedStatement.executeQuery();
  144.  
  145. while (resultset.next())
  146. {
  147. slowko_ang = resultset.getString("slowko_ang");
  148. slowko_pl = resultset.getString("slowko_pl");
  149.  
  150. }
  151.  
  152. resultset.close();
  153. preparedStatement.close();
  154.  
  155. } catch (SQLException e)
  156. {
  157. System.out.println("Coś poszło nie tak w SqlConnector.getword()");
  158. e.printStackTrace();
  159. }
  160. return new Word(id_slowka, slowko_ang, slowko_pl);
  161. }
  162.  
  163. public Film getWordsFromFilmDB(Integer id_film)
  164. {
  165.  
  166. Integer id_slowka_ang = 000;
  167. Integer ilosc_wystapien = 000;
  168.  
  169. try
  170. {
  171. String query = "SELECT * FROM film WHERE id_film = " + id_film;
  172. preparedStatement = connection.prepareStatement(query);
  173. resultset = preparedStatement.executeQuery();
  174. while (resultset.next())
  175. {
  176. id_slowka_ang = resultset.getInt("id_slowka_ang");
  177. ilosc_wystapien = resultset.getInt("ilosc_wystapien");
  178. }
  179. resultset.close();
  180. preparedStatement.close();
  181. } catch (SQLException e)
  182. {
  183. System.out
  184. .println("Coś poszło nie tak w SqlConnector.geWordsFromFilmDB()");
  185. e.printStackTrace();
  186. }
  187. return new Film(id_film, id_slowka_ang, ilosc_wystapien);
  188. }
  189.  
  190. public void eraseTable(String table)
  191. {
  192. int ile = dbSize(table);
  193. try
  194. {
  195. String query = "TRUNCATE TABLE " + table;
  196.  
  197. preparedStatement = connection.prepareStatement(query);
  198. // resultset = preparedStatement.executeQuery();
  199. preparedStatement.executeUpdate("TRUNCATE " + table);
  200.  
  201. resultset.close();
  202. preparedStatement.close();
  203.  
  204. } catch (SQLException e)
  205. {
  206. System.out.println("Coś poszło nie tak eraseTable. "
  207. + this.getClass().getName());
  208. e.printStackTrace();
  209. } catch (NullPointerException e)
  210. {
  211. System.out.println("Nul w eraseTable " + this.getClass().getName());
  212. e.printStackTrace();
  213. }
  214. System.out.println("Usunięto zawartość tabeli " + table
  215. + " o wielkości " + ile + "Teraz zawiera wierszy: "
  216. + dbSize(table));
  217. }
  218.  
  219. // /////////////////////////table start
  220. public void copyTable()
  221. {
  222. String query = "INSERT INTO tmp SELECT * FROM Film";
  223.  
  224. try
  225. {
  226.  
  227. statement = connection.createStatement();
  228. statement.execute(query);
  229.  
  230. resultset.close();// finaly?
  231. preparedStatement.close();
  232.  
  233. } catch (SQLException e)
  234. {
  235. System.out.println("Coś poszło nie tak copyTable " + query + " "
  236. + this.getClass().getName());
  237. e.printStackTrace();
  238. } catch (NullPointerException e)
  239. {
  240. System.out.println("Nul w copyTable() " + query + " "
  241. + this.getClass().getName());
  242. e.printStackTrace();
  243.  
  244. }
  245. System.out.println("copytable finis");
  246. }
  247.  
  248.