Skip to content

Commit 850e916

Browse files
committed
Ejemplo con ocp con el enunciado de la libreria usado en srp
1 parent 8a5b915 commit 850e916

File tree

1 file changed

+114
-1
lines changed

1 file changed

+114
-1
lines changed

Roadmap/27 - SOLID OCP/java/simonguzman.java

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,122 @@
44

55
public class simonguzman {
66
public static void main(String[] args) {
7-
libraryViolationOcp();
7+
//libraryViolationOcp();
8+
libraryFollowOCP();
89
}
910

11+
/*************************** Ejemplo con ocp(Correcto) ***************************/
12+
static void libraryFollowOCP(){
13+
List<String> books = new ArrayList<>();
14+
List<String> users = new ArrayList<>();
15+
LibraryOcp library = new LibraryOcp();
16+
17+
LibraryOperation registerBook = new RegisterBookOperation("El Quijote", books);
18+
library.performOperation(registerBook);
19+
20+
LibraryOperation registerUser = new RegisterUserOperation("Juan Perez", users);
21+
library.performOperation(registerUser);
22+
23+
LibraryOperation loanBook = new LoanBookOperation("El Quijote", "Juan Perez", books);
24+
library.performOperation(loanBook);
25+
26+
LibraryOperation renewLoan = new RenewLoanOperation("El Quijote", "Juan Perez");
27+
library.performOperation(renewLoan);
28+
}
29+
30+
interface LibraryOperation{
31+
void execute();
32+
}
33+
34+
static class RegisterBookOperation implements LibraryOperation{
35+
private String book;
36+
private List<String> books;
37+
38+
public RegisterBookOperation(){
39+
40+
}
41+
42+
public RegisterBookOperation(String book, List<String> books){
43+
this.book = book;
44+
this.books = books;
45+
}
46+
47+
@Override
48+
public void execute() {
49+
books.add(book);
50+
System.out.println("Libro registrado: " + book);
51+
}
52+
}
53+
54+
static class RegisterUserOperation implements LibraryOperation{
55+
private String user;
56+
private List<String> users;
57+
58+
public RegisterUserOperation(){
59+
60+
}
61+
62+
public RegisterUserOperation(String user, List<String> users){
63+
this.user = user;
64+
this.users = users;
65+
}
66+
@Override
67+
public void execute() {
68+
users.add(user);
69+
System.out.println("Usuario registrado: "+user);
70+
}
71+
}
72+
73+
static class LoanBookOperation implements LibraryOperation{
74+
private String book;
75+
private String user;
76+
private List<String> books;
77+
78+
public LoanBookOperation(){
79+
80+
}
81+
82+
public LoanBookOperation(String book, String user, List<String> books){
83+
this.book = book;
84+
this.user = user;
85+
this.books = books;
86+
}
87+
88+
@Override
89+
public void execute() {
90+
if(books.contains(book)){
91+
System.out.println("Libro: " + book + " usuario " + user);
92+
}else{
93+
System.out.println("El libro no esta disponible.");
94+
}
95+
}
96+
97+
}
98+
99+
static class RenewLoanOperation implements LibraryOperation{
100+
private String book;
101+
private String user;
102+
103+
public RenewLoanOperation(){
104+
105+
}
106+
107+
public RenewLoanOperation(String book, String user){
108+
this.book = book;
109+
this.user = user;
110+
}
111+
112+
@Override
113+
public void execute() {
114+
System.out.println("El prestamo del libro " + book + " a sido renovado por " + user);
115+
}
116+
}
117+
118+
static class LibraryOcp{
119+
public void performOperation(LibraryOperation operation){
120+
operation.execute();
121+
}
122+
}
10123
/*************************** Ejemplo sin ocp(Incorrecto) ***************************/
11124
static void libraryViolationOcp(){
12125
Library library = new Library();

0 commit comments

Comments
 (0)