Last active
August 20, 2018 20:15
-
-
Save fabio-filho/1a5d41aa90d3ad47c16d69953771a4d2 to your computer and use it in GitHub Desktop.
SOLID Principles - SRP, OCP, ISP, DIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Contracts | |
interface IBookRepository{ | |
@Nullable | |
String getBook(Long id); | |
List<Object> getBooksByDescription(String description); | |
List<Object> getBooksByDescriptionOrder(String description); | |
Object post(Object book); | |
} | |
interface IBookApplication{ | |
@Nullable | |
Object createBook(Object book); | |
List<Object> searchBooks(String description); | |
List<Object> searchBooks(String description); | |
} | |
interface IValidationModel{ | |
boolean isValidated(Object book); | |
} | |
// ======================================================== | |
class BookRepository extends IBookRepository(){ | |
@Nullable | |
public String getBook(Long id){ | |
// sql -> select Description, Price ... from Book where id = id ... | |
} | |
public List<Object> getBooksByDescription(String description){ | |
// sql -> select Description, Price ... from Book where description like description ... | |
} | |
public Object post(Object book){ | |
// sql -> insert into Book values (object) ... | |
} | |
} | |
class BookValidation : IValidationModel{ | |
@Override | |
public boolean isValidated(Object book){ | |
// ... checks if the model is correct ... | |
if(book.year == 1994) | |
if(book.price < 60) | |
return false; | |
if(book.year >= 2000) | |
if(book.price < 80) | |
return false; | |
return true; | |
} | |
} | |
class NotebookValidation : IValidationModel{ | |
@Override | |
public boolean isValidated(Object notebook){ | |
// ... checks if the model is correct ... | |
if(book.year == 2000) | |
if(book.price < 90) | |
return false; | |
return true; | |
} | |
} | |
@Test | |
public void IsModel_Correct_When_1994(){ | |
Object book = new Object("Desc", 1994, 34.6); | |
IValidationModel validator = new BookValidation(); | |
assert.Equal(false, validator.isValidated(book)); | |
} | |
@Test | |
public void IsModel_Correct(){ | |
Object book = new Object("Desc", 2000, 80); | |
IValidationModel validator ;//= new BookValidation(); | |
assert.Equal(true, validator.isValidated(book)); | |
} | |
@Test | |
public void IsModel_Correct2(){ | |
Object book = new Object("Uyeausdy", 1994, 70); | |
IValidationModel validator = new NotebookValidation(); | |
assert.Equal(true, validator.isValidated(book)); | |
} | |
// Implementation class | |
class BookApplication extends IBookApplication { | |
private IBookRepository repository; | |
private IValidationModel bookValidation; | |
public BookApplication(IBookRepository repository, IValidationModel bookValidation){ | |
this.repository = repository; | |
this.bookValidation = bookValidation; | |
} | |
@Override @Nullable | |
public Object createBook(Object book) { | |
if(bookValidation.isValidated(book)) | |
return repository.post(book); | |
else | |
return null; | |
} | |
@Override | |
public List<Object> searchBooks(String description) { | |
List<Object> books = new ArrayList<?>(); | |
if(!description.equals("")) | |
books = repository.getBooksByDescription(description); | |
return books; | |
} | |
@Override | |
public List<Object> searchBooksOrder(String description) { | |
List<Object> books = new ArrayList<?>(); | |
if(!description.equals("")) | |
books = repository.getBooksByDescription(description); | |
return books; | |
} | |
} | |
fun main(){ | |
// Dependencies | |
IBookRepository repository = new MockBookRepository(); | |
IValidationModel bookValidation = new BookValidation(); | |
IBookApplication application = new BookApplication(repository, bookValidation); | |
// Model | |
Object myBook = new Object("The art of doing twice the work in half the time"); | |
// Create the book | |
application.createBook(myBook); | |
// Query | |
List<Objects> books = application.searchBooks("twice the work"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment