Skip to content

Commit 8de49a4

Browse files
committed
Abstract factory pattern
1 parent 8ab5fe1 commit 8de49a4

File tree

7 files changed

+142
-0
lines changed

7 files changed

+142
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.factory;
2+
3+
/**
4+
* Created by sdmg15 on 21/03/17.
5+
*/
6+
public interface AbstractFactory {
7+
8+
Computer createComputer();
9+
}

com/abstractFactory/Computer.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.factory;
2+
3+
/**
4+
* Created by sdmg15 on 18/03/17.
5+
*/
6+
public abstract class Computer {
7+
8+
9+
public abstract String getRam();
10+
public abstract String getCpu();
11+
public abstract String getHdd();
12+
13+
@Override
14+
15+
public String toString(){
16+
return "Config :: RAM = " + this.getRam() + " CPU = " + this.getCpu() + " HDD = " + this.getHdd();
17+
}
18+
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.factory;
2+
3+
/**
4+
* Created by sdmg15 on 21/03/17.
5+
*/
6+
public class ComputerFactory {
7+
8+
public static Computer getComputer(AbstractFactory fact){
9+
return fact.createComputer();
10+
}
11+
}

com/abstractFactory/PC.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.factory;
2+
3+
/**
4+
* Created by sdmg15 on 18/03/17.
5+
*/
6+
public class PC extends Computer {
7+
8+
private String ram;
9+
private String cpu;
10+
private String hdd;
11+
12+
13+
public PC(String ram,String hdd, String cpu){
14+
this.ram = ram;
15+
this.hdd= hdd;
16+
this.cpu = cpu;
17+
}
18+
public String getRam(){
19+
return this.ram;
20+
}
21+
public String getCpu(){
22+
return this.cpu;
23+
}
24+
public String getHdd(){return this.hdd;}
25+
26+
27+
28+
29+
}

com/abstractFactory/PCFactory.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.factory;
2+
3+
/**
4+
* Created by sdmg15 on 21/03/17.
5+
*/
6+
public class PCFactory implements AbstractFactory {
7+
8+
private String ram;
9+
private String hdd;
10+
11+
private String cpu;
12+
13+
14+
public PCFactory(String r, String h, String c){
15+
this.ram = r;
16+
this.hdd = h;
17+
this.cpu = c;
18+
}
19+
20+
public Computer createComputer(){
21+
return new PC(ram,hdd,cpu);
22+
}
23+
}

com/abstractFactory/Server.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.factory;
2+
3+
/**
4+
* Created by sdmg15 on 18/03/17.
5+
*/
6+
public class Server extends Computer{
7+
private String ram;
8+
private String cpu;
9+
private String hdd;
10+
11+
12+
public Server(String ram,String hdd, String cpu){
13+
this.ram = ram;
14+
this.hdd= hdd;
15+
this.cpu = cpu;
16+
}
17+
public String getRam(){
18+
return this.ram;
19+
}
20+
public String getCpu(){
21+
return this.cpu;
22+
}
23+
24+
public String getHdd(){
25+
return this.hdd;
26+
}
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.factory;
2+
3+
/**
4+
* Created by sdmg15 on 21/03/17.
5+
*/
6+
public class ServerFactory {
7+
8+
9+
private String ram;
10+
private String hdd;
11+
12+
private String cpu;
13+
14+
15+
public ServerFactory(String r, String h, String c){
16+
this.ram = r;
17+
this.hdd = h;
18+
this.cpu = c;
19+
}
20+
21+
public Computer createComputer(){
22+
return new Server(ram,hdd,cpu);
23+
}
24+
}

0 commit comments

Comments
 (0)