Skip to content

Commit 3f48149

Browse files
committed
Implements abstract factory
1 parent 74f8455 commit 3f48149

File tree

13 files changed

+191
-87
lines changed

13 files changed

+191
-87
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"editor.formatOnSave": false,
3+
"files.autoSave": true
4+
}

README.md

Lines changed: 9 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,15 @@
11
# ☕ Java Design Patterns
22

3-
## About
3+
## Abstract Factory
44

5-
This application is a simple Java Console Application that aims to implement design pattern examples to a Programmin Language Class. A set of design patterns will be developed
6-
each week, and they are divided in different [branches](https://github.com/LBeghini/Java-Design-Patterns/branches).
5+
Abstract Factory is a pattern that provide an interafce to create objects that have a common theme without specifying their concrete classes.
76

8-
The main branch is just a template for every other branch.
7+
For example, let's say we have a store that makes on demand mobiles:
8+
- chair
9+
- couch
910

10-
Also, to make it easier to download the source code, [releases](https://github.com/LBeghini/Java-Design-Patterns/releases) are created related to the task of the week, giving a snapshot of the code for that specific implementation.
11+
There's two types of mobiles:
12+
- wooden
13+
- fabric
1114

12-
## Implemented design patterns
13-
14-
### Behavioural patterns
15-
16-
- [x] [Chain of responsibility](https://github.com/LBeghini/Java-Design-Patterns/tree/4-chain-of-responsibility)
17-
- [x] [Command](https://github.com/LBeghini/Java-Design-Patterns/tree/6-command)
18-
- [x] [Interpreter](https://github.com/LBeghini/Java-Design-Patterns/tree/9-interpreter)
19-
- [x] [Iterator](https://github.com/LBeghini/Java-Design-Patterns/tree/4-iterator)
20-
- [x] [Mediator](https://github.com/LBeghini/Java-Design-Patterns/tree/9-mediator)
21-
- [x] [Memento](https://github.com/LBeghini/Java-Design-Patterns/tree/5-memento)
22-
- [x] [Observer](https://github.com/LBeghini/Java-Design-Patterns/tree/5-observer)
23-
- [x] [State](https://github.com/LBeghini/Java-Design-Patterns/tree/3-state)
24-
- [x] [Strategy](https://github.com/LBeghini/Java-Design-Patterns/tree/6-strategy)
25-
- [x] [Template method](https://github.com/LBeghini/Java-Design-Patterns/tree/4-template-method)
26-
- [x] [Visitor](https://github.com/LBeghini/Java-Design-Patterns/tree/11-visitor)
27-
28-
### Creational patterns
29-
30-
- [ ] Abstract factory
31-
- [x] [Builder](https://github.com/LBeghini/Java-Design-Patterns/tree/1-builder)
32-
- [x] [Factory method](https://github.com/LBeghini/Java-Design-Patterns/tree/2-factory-method)
33-
- [x] [Prototype](https://github.com/LBeghini/Java-Design-Patterns/tree/2-prototype)
34-
- [x] [Singleton](https://github.com/LBeghini/Java-Design-Patterns/tree/1-singleton)
35-
36-
### Structural patterns
37-
38-
- [x] [Adapter](https://github.com/LBeghini/Java-Design-Patterns/tree/7-adapter)
39-
- [x] [Bridge](https://github.com/LBeghini/Java-Design-Patterns/tree/7-bridge)
40-
- [x] [Composite](https://github.com/LBeghini/Java-Design-Patterns/tree/8-composite)
41-
- [x] [Decorator](https://github.com/LBeghini/Java-Design-Patterns/tree/11-decorator)
42-
- [x] [Facade](https://github.com/LBeghini/Java-Design-Patterns/tree/8-facade)
43-
- [x] [Flyweight](https://github.com/LBeghini/Java-Design-Patterns/tree/10-flyweight)
44-
- [x] [Proxy](https://github.com/LBeghini/Java-Design-Patterns/tree/10-proxy)
45-
46-
47-
## Technologies
48-
49-
- Java
50-
- JUnit
51-
- Maven
52-
53-
## Requirements
54-
55-
To run and edit the project, be sure to have installed in your computer the following softwares:
56-
- A code editor
57-
58-
After that, you'll need to clone this repo:
59-
60-
```bash
61-
git clone https://github.com/LBeghini/Java-Design-Patterns.git
62-
```
63-
64-
## Change branch
65-
66-
To change to a different branch, run the command:
67-
68-
```bash
69-
git checkout name-of-the-branch
70-
```
71-
72-
The branch names have the pattern:
73-
74-
```bash
75-
{number-of-the-week}-{pattern-name}
76-
```
77-
78-
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern
79-
80-
## Testing
81-
82-
This project has no aim to run any of the implemented classes, as the goal is the code itself. However, the classes will be tested to visualize the behaviour and implementation
83-
of the patterns.
84-
85-
You can run the tests using the maven wrapper:
86-
87-
```bash
88-
./mvnw test
89-
```
90-
91-
## :balance_scale: License
92-
93-
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE)
15+
The store can be specialized on selling wooden mobiles or fabric mobiles, and depending on the specialty it will require a specific factory.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.language.programming.src;
2+
3+
public interface AbstractFactory {
4+
5+
Chair buildChair();
6+
Couch buildCouch();
7+
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.language.programming.src;
2+
3+
public interface Chair {
4+
5+
String build();
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.language.programming.src;
2+
3+
public interface Couch {
4+
5+
String build();
6+
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.language.programming.src;
2+
3+
public class Store {
4+
5+
private Chair chair;
6+
private Couch couch;
7+
8+
public Store (AbstractFactory abstractFactory) {
9+
this.chair = abstractFactory.buildChair();
10+
this.couch = abstractFactory.buildCouch();
11+
}
12+
13+
public String getChair() {
14+
return this.chair.build();
15+
}
16+
17+
public String getCouch() {
18+
return this.couch.build();
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.language.programming.src.factories;
2+
3+
import com.language.programming.src.AbstractFactory;
4+
import com.language.programming.src.Chair;
5+
import com.language.programming.src.Couch;
6+
import com.language.programming.src.objects.FabricChair;
7+
import com.language.programming.src.objects.FabricCouch;
8+
9+
public class FabricFactory implements AbstractFactory {
10+
11+
@Override
12+
public Chair buildChair() {
13+
return new FabricChair();
14+
}
15+
16+
@Override
17+
public Couch buildCouch() {
18+
return new FabricCouch();
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.language.programming.src.factories;
2+
3+
import com.language.programming.src.AbstractFactory;
4+
import com.language.programming.src.Chair;
5+
import com.language.programming.src.Couch;
6+
import com.language.programming.src.objects.WoodenChair;
7+
import com.language.programming.src.objects.WoodenCouch;
8+
9+
public class WoodenFactory implements AbstractFactory {
10+
11+
@Override
12+
public Chair buildChair() {
13+
return new WoodenChair();
14+
}
15+
16+
@Override
17+
public Couch buildCouch() {
18+
return new WoodenCouch();
19+
}
20+
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.language.programming.src.objects;
2+
3+
import com.language.programming.src.Chair;
4+
5+
public class FabricChair implements Chair {
6+
7+
@Override
8+
public String build() {
9+
return "Fabric Chair";
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.language.programming.src.objects;
2+
3+
import com.language.programming.src.Couch;
4+
5+
public class FabricCouch implements Couch {
6+
7+
@Override
8+
public String build() {
9+
return "Fabric Couch";
10+
}
11+
12+
}

0 commit comments

Comments
 (0)