Skip to content

Commit 39f41f3

Browse files
committed
feat(patterns): added abstract factory and factory method pattern
1 parent 15b41ba commit 39f41f3

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

abstract-factory/abstract-factory.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ConcreteFactory1, ConcreteFactory2, clientCode} from './index'
1+
import { ConcreteFactory1, ConcreteFactory2, clientCode } from './index';
2+
import { Creator, clientCode as client } from './my-abstract-factory';
23

34
describe('abstract-factory', (): void => {
45
it('should execute abstract factory', (): void => {
@@ -12,5 +13,9 @@ describe('abstract-factory', (): void => {
1213

1314
console.log('Client: Testing the same client code with the second factory type...');
1415
clientCode(new ConcreteFactory2());
16+
});
17+
18+
it('should create different products from factory', (): void => {
19+
client(new Creator());
1520
})
1621
})
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
interface IApple {
2+
price: number;
3+
quantity: number;
4+
color: string;
5+
}
6+
7+
interface IBanana {
8+
price: number;
9+
quantity: number;
10+
color: string;
11+
}
12+
13+
interface ICreator {
14+
15+
createBanana(props: IBanana): IBanana;
16+
createApple(props: IApple): IApple;
17+
18+
}
19+
20+
class Apple implements IApple {
21+
constructor(
22+
public price: number,
23+
public quantity: number,
24+
public color: string,
25+
){}
26+
}
27+
28+
class Banana implements IBanana {
29+
constructor(
30+
public price: number,
31+
public quantity: number,
32+
public color: string,
33+
){}
34+
}
35+
36+
export class Creator implements ICreator {
37+
createBanana({ price, color, quantity }: IBanana): IBanana {
38+
return new Banana(price, quantity, color);
39+
}
40+
createApple({ price, color, quantity }: IApple): IApple {
41+
return new Apple(price, quantity, color)
42+
}
43+
}
44+
45+
export const clientCode = (creator: ICreator): void => {
46+
const apple = creator.createApple({
47+
color: 'red',
48+
price: 2.8,
49+
quantity: 20
50+
});
51+
52+
console.log(apple);
53+
54+
const banana = creator.createBanana({
55+
color: 'yellow',
56+
price: 3.9,
57+
quantity: 3
58+
});
59+
60+
console.log(banana);
61+
}

factory-method/factory-method.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { clientCode, ConcreteCreator1, ConcreteCreator2 } from "./index";
22
import { clientDelivery, BikeCreator, TruckCreator } from './my-factory-method';
33

44
describe('factory-method', (): void => {
5-
it('should create a valid instance', (): void => {
5+
it('should execute factory method with success', (): void => {
66
/**
77
* The Application picks a creator's type depending on the configuration or
88
* environment.
@@ -15,8 +15,11 @@ describe('factory-method', (): void => {
1515
clientCode(new ConcreteCreator2());
1616
})
1717

18-
it('should create car delivery', () => {
18+
it('should execute bike delivery', () => {
1919
clientDelivery(new BikeCreator()); // Delivery by bike
20+
})
21+
22+
it('should execute truck delivery', () => {
2023
clientDelivery(new TruckCreator()); // Delivery by truck
2124
})
2225
})

0 commit comments

Comments
 (0)