Skip to content

Instantly share code, notes, and snippets.

@malalanayake
Created April 11, 2016 18:13
Show Gist options
  • Save malalanayake/8520b6422e7552d845bb40082c129833 to your computer and use it in GitHub Desktop.
Save malalanayake/8520b6422e7552d845bb40082c129833 to your computer and use it in GitHub Desktop.
Single Responsibility Principal - Good sample code
package sample.design.single.responsibility.good;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* @author dmalalan
* @created Apr 11, 2016 11:20:19 AM
*
* @blog https://malalanayake.wordpress.com/
*/
public class BookNew implements ReadingMaterial {
private static final String BOOK = "Book";
private int bookId;
private String name;
public int getId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return BOOK;
}
}
package sample.design.single.responsibility.good;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* @author dmalalan
* @created Apr 11, 2016 12:25:33 PM
*
* @blog https://malalanayake.wordpress.com/
*/
public class Magazine implements ReadingMaterial {
private static final String MAGAZINE = "Magazine";
private int magazineId;
public void setMagazineId(int magazineId) {
this.magazineId = magazineId;
}
public int getId() {
return magazineId;
}
public String getType() {
return MAGAZINE;
}
}
package sample.design.single.responsibility.good;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* @author dmalalan
* @created Apr 11, 2016 12:16:01 PM
*
* @blog https://malalanayake.wordpress.com/
*/
public interface ReadingMaterial {
public int getId();
public String getType();
}
package sample.design.single.responsibility.good;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* @author dmalalan
* @created Apr 11, 2016 11:35:44 AM
*
* @blog https://malalanayake.wordpress.com/
*/
public class ReadingMaterialDAO {
/**
* 1. Get DB connection and 2. Create transaction 3. Save the RM 4. Commit
* transaction
*/
public void saveRM(ReadingMaterial readingMaterial) {
System.out.println("[SAVE:RM " + readingMaterial.getId() + "]");
}
/**
* 1. Get DB connection and 2. Create transaction 3. Update the RM 4.
* Commit transaction
*/
public void updateRM(ReadingMaterial readingMaterial) {
System.out.println("[UPDATE:RM " + readingMaterial.getId() + "]");
}
/**
* 1. Get DB connection and 2. read the RM
*
*/
public void readRM(ReadingMaterial readingMaterial) {
System.out.println("[READ:RM " + readingMaterial.getId() + "]");
}
/**
* 1. Get DB connection and 2. Create transaction 3. Delete the RM 4.
* Commit transaction
*/
public void deleteRM(ReadingMaterial readingMaterial) {
System.out.println("[DELETE:RM " + readingMaterial.getId() + "]");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment