Skip to content

Commit f046614

Browse files
authored
Merge pull request mouredev#4768 from AmadorQuispe/aqh
#10 - GO
2 parents 9a9b41a + f9b2bc7 commit f046614

File tree

3 files changed

+337
-0
lines changed

3 files changed

+337
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strconv"
7+
)
8+
9+
//De manera general, GO representa cualquier tipo de error mediante la interfaz error:
10+
11+
func main() {
12+
//División por cero
13+
if res, err := divideTwoNumbers(10, 0); err == nil {
14+
fmt.Println(res)
15+
} else {
16+
fmt.Println(err)
17+
}
18+
19+
//Indice fuera de rango
20+
numbers := []int{1, 2, 3, 4, 5, 6}
21+
val, err := getElementByIndex(numbers, 6)
22+
if err != nil {
23+
fmt.Println(err)
24+
} else {
25+
fmt.Println(val)
26+
27+
}
28+
29+
//Error personalizado
30+
result, err := divisionPositive(-34, 3)
31+
if err != nil {
32+
fmt.Println(err)
33+
} else {
34+
fmt.Println(result)
35+
}
36+
}
37+
38+
func divideTwoNumbers(a, b int) (int, error) {
39+
if b == 0 {
40+
return 0, errors.New("cannot divide by zero")
41+
}
42+
return a / b, nil
43+
}
44+
45+
func getElementByIndex[T any](slice []T, i int) (T, error) {
46+
if len(slice) >= i {
47+
var zeroValue T
48+
return zeroValue, errors.New("index of range! it must be less than " + strconv.Itoa(i))
49+
}
50+
return slice[i], nil
51+
}
52+
53+
type CustomError struct {
54+
msg string
55+
}
56+
57+
func (ce *CustomError) Error() string {
58+
return ce.msg
59+
}
60+
61+
func divisionPositive(numerator, denominator int) (int, error) {
62+
if numerator < 0 || denominator < 0 {
63+
return 0, &CustomError{msg: "not valid numbers negatives"}
64+
}
65+
return numerator / denominator, nil
66+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"math"
7+
)
8+
9+
type ReportServiceBad[T any] struct {
10+
data T
11+
}
12+
13+
func (rs *ReportServiceBad[T]) ReportGenerator(typeReport string) {
14+
if typeReport == "PDF" {
15+
fmt.Println(rs.data)
16+
fmt.Println("Generando reporte PDF")
17+
} else if typeReport == "HTML" {
18+
fmt.Println(rs.data)
19+
fmt.Println("Generando reporte HTML")
20+
}
21+
}
22+
23+
// Re factorización para cumplir el OCP
24+
// ReportGenerator, interfaz que define el comportamiento de un generador de reportes
25+
type ReportGenerator interface {
26+
GenerateReport(data interface{})
27+
}
28+
29+
// PDFReportGenerator, implementación de ReportGenerator para reportes PDF
30+
type PDFReportGenerator struct{}
31+
32+
func (p *PDFReportGenerator) GenerateReport(data interface{}) {
33+
fmt.Println(data)
34+
fmt.Println("Generando reporte PDF")
35+
}
36+
37+
// HTMLReportGenerator, implementación de ReportGenerator para reportes HTML
38+
type HTMLReportGenerator struct{}
39+
40+
func (h *HTMLReportGenerator) GenerateReport(data interface{}) {
41+
fmt.Println(data)
42+
fmt.Println("Generando reporte HTML")
43+
}
44+
45+
// ReportService usa un generador de reportes para generar reportes
46+
type ReportService struct {
47+
data interface{}
48+
reportGen ReportGenerator
49+
}
50+
51+
func (rs *ReportService) GenerateReport() {
52+
rs.reportGen.GenerateReport(rs.data)
53+
}
54+
55+
// EXTRA
56+
type Operation interface {
57+
Apply(num1, num2 float32) (float32, error)
58+
}
59+
60+
// SUMA
61+
type Addition struct{}
62+
63+
func (a *Addition) Apply(num1, num2 float32) (float32, error) {
64+
return num1 + num2, nil
65+
}
66+
67+
// RESTA
68+
type Subtraction struct{}
69+
70+
func (a *Subtraction) Apply(num1, num2 float32) (float32, error) {
71+
return num1 - num2, nil
72+
}
73+
74+
// MULTIPLICACIÓN
75+
type Multiplication struct{}
76+
77+
func (a *Multiplication) Apply(num1, num2 float32) (float32, error) {
78+
return num1 * num2, nil
79+
}
80+
81+
// DIVISIÓN
82+
type Division struct{}
83+
84+
func (a *Division) Apply(num1, num2 float32) (float32, error) {
85+
if num2 == 0 {
86+
return 0, errors.New("division by zero")
87+
}
88+
return num1 / num2, nil
89+
}
90+
91+
// POTENCIA
92+
type Pow struct{}
93+
94+
func (a *Pow) Apply(num1, num2 float32) (float32, error) {
95+
return float32(math.Pow(float64(num1), float64(num2))), nil
96+
}
97+
98+
// Calculator usa una operación para calcular un resultado
99+
type Calculator struct{}
100+
101+
func (c *Calculator) Calculate(operation Operation, num1, num2 float32) (float32, error) {
102+
return operation.Apply(num1, num2)
103+
}
104+
105+
func main() {
106+
//VIOLA OCP
107+
reportServiceBad := ReportServiceBad[string]{data: "data de ejemplo bad"}
108+
reportServiceBad.ReportGenerator("PDF")
109+
110+
//OCP
111+
data := "Datos de ejemplo"
112+
pdfGenerator := &PDFReportGenerator{}
113+
htmlGenerator := &HTMLReportGenerator{}
114+
115+
pdfReportService := &ReportService{data: data, reportGen: pdfGenerator}
116+
htmlReportService := &ReportService{data: data, reportGen: htmlGenerator}
117+
118+
pdfReportService.GenerateReport()
119+
htmlReportService.GenerateReport()
120+
121+
//CALCULATOR
122+
num1 := float32(10)
123+
num2 := float32(5)
124+
125+
calculator := &Calculator{}
126+
127+
addition := &Addition{}
128+
subtraction := &Subtraction{}
129+
multiplication := &Multiplication{}
130+
division := &Division{}
131+
132+
result, err := calculator.Calculate(addition, num1, num2)
133+
if err != nil {
134+
fmt.Println("Error:", err)
135+
} else {
136+
fmt.Println("Suma:", result)
137+
}
138+
139+
result, err = calculator.Calculate(subtraction, num1, num2)
140+
if err != nil {
141+
fmt.Println("Error:", err)
142+
} else {
143+
fmt.Println("Resta:", result)
144+
}
145+
146+
result, err = calculator.Calculate(multiplication, num1, num2)
147+
if err != nil {
148+
fmt.Println("Error:", err)
149+
} else {
150+
fmt.Println("Multiplicación:", result)
151+
}
152+
153+
result, err = calculator.Calculate(division, num1, num2)
154+
if err != nil {
155+
fmt.Println("Error:", err)
156+
} else {
157+
fmt.Println("División:", result)
158+
}
159+
160+
//Potencia
161+
pow := &Pow{}
162+
result, err = calculator.Calculate(pow, num1, num2)
163+
if err != nil {
164+
fmt.Println("Error:", err)
165+
} else {
166+
fmt.Println("Potencia:", result)
167+
}
168+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.amsoft.roadmap.example27;
2+
3+
import java.util.Map;
4+
5+
public class Example27 {
6+
public static void main(String[] args) {
7+
Report report = new HTMLReport();
8+
report.generate();
9+
10+
Calculator calculator = new Calculator();
11+
System.out.println("La suma de 5 + 6 es: " + calculator.calculate(new Addition(),5,6));
12+
System.out.println("La resta de 10 - 6 es: " + calculator.calculate(new Subtraction(),10,6));
13+
System.out.println("La multiplicación de 10 * 6 es: " + calculator.calculate(new Multiplication(),10,6));
14+
System.out.println("La división de 10 / 2 es: " + calculator.calculate(new Division(),10,2));
15+
System.out.println("La potencia de 9 elevado 3 es: " + calculator.calculate(new Pow(),9,3));
16+
}
17+
}
18+
//Viola OCP, si queremos agregar un reporte Excel debemos modificar la clase, agregando un if
19+
/*class ReportGenerator{
20+
public void generateReport(String type){
21+
if(type.equals("PDF")){
22+
System.out.println("Generando reporte PDF");
23+
} else if (type.equals("HTML")) {
24+
System.out.println("Generando reporte HTML");
25+
}
26+
}
27+
}*/
28+
29+
30+
//Cumple OCP, declaramos un interfaz con un método que debe implementar las clases concretas
31+
//Si queremos agregar un reporte Excel simplemente creamos una clase que implementa la interfaz
32+
interface Report{
33+
void generate();
34+
}
35+
36+
class PDFReport implements Report{
37+
38+
@Override
39+
public void generate() {
40+
System.out.println("Generando reporte PDF");
41+
}
42+
}
43+
44+
class HTMLReport implements Report{
45+
46+
@Override
47+
public void generate() {
48+
System.out.println("Generando reporte HTML");
49+
}
50+
}
51+
52+
53+
54+
55+
//EXTRA
56+
interface Operable {
57+
double calculate(double num1,double num2);
58+
}
59+
60+
class Addition implements Operable {
61+
62+
@Override
63+
public double calculate(double num1, double num2) {
64+
return num1 + num2;
65+
}
66+
}
67+
68+
class Subtraction implements Operable {
69+
70+
@Override
71+
public double calculate(double num1, double num2) {
72+
return num1 - num2;
73+
}
74+
}
75+
76+
class Multiplication implements Operable{
77+
78+
@Override
79+
public double calculate(double num1, double num2) {
80+
return num1 * num2;
81+
}
82+
}
83+
84+
class Division implements Operable {
85+
86+
@Override
87+
public double calculate(double num1, double num2) {
88+
return num1/num2;
89+
}
90+
}
91+
92+
class Calculator {
93+
public double calculate(Operable operation, double num1, double num2){
94+
return operation.calculate(num1,num2);
95+
}
96+
}
97+
class Pow implements Operable {
98+
99+
@Override
100+
public double calculate(double num1, double num2) {
101+
return Math.pow(num1,num2);
102+
}
103+
}

0 commit comments

Comments
 (0)