-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-single-responsability-principle.ts
67 lines (53 loc) · 1.6 KB
/
01-single-responsability-principle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(() => {
interface Product {
id: number;
name: string;
}
class ProductService {
getProduct(id: number) {
console.log('Product: ', { id, name: 'OLED Tv' });
}
saveProduct(product: Product) {
console.log('Saving in database', product);
}
}
class Mailer {
private masterEmail: string = '[email protected]';
sendEmail(emailList: string[], template: 'to-clients' | 'to-admins') {
console.log('Sending mail to customers', template);
}
}
// Usually, this is a class to control the view that is displayed to the user
// Remember that we can have many views to do this same job.
class ProductBloc {
private productService: ProductService;
private mailer: Mailer;
constructor(productService: ProductService, mailer: Mailer) {
this.productService = productService;
this.mailer = mailer;
}
loadProduct(id: number) {
this.productService.getProduct(id);
}
saveProduct(product: Product) {
this.productService.saveProduct(product);
}
notifyClients() {
this.mailer.sendEmail(['[email protected]'], 'to-clients');
}
}
class CartBloc {
private itemsInCart: Object[] = [];
addToCart(productId: number) {
console.log('Adding to the cart ', productId);
}
}
const productService = new ProductService();
const mailer = new Mailer();
const productBloc = new ProductBloc(productService, mailer);
const cartBloc = new CartBloc();
productBloc.loadProduct(10);
productBloc.saveProduct({ id: 10, name: 'OLED TV' });
productBloc.notifyClients();
cartBloc.addToCart(10);
})();