Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ab5fe1

Browse files
committed
🏭 Pattern Factory
1 parent 2594182 commit 8ab5fe1

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

‎README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,25 @@ Classes are in the package `com.Singleton`;
6565
}
6666
```
6767
# Pattern Factory
68+
69+
Factory Pattern is one of the Creational Design pattern and it's widely used in JDK as well as frameworks like
70+
Spring and Struts.
71+
This pattern is used when we have a super class with multiple sub-classes and based on the input, we need to return one of the sub-class.
72+
73+
```java
74+
import com.factory.FactoryClass ; //The factory class
75+
import com.factory.PC; //sub-class
76+
import com.factory.Server; //sub-class
77+
//PC and Server classes extend from Computer.
78+
79+
public static void main(String[] args){
80+
81+
FactoryClass fc = new FactoryClass();
82+
Computer comp1 = fc.getComputer("PC",16,499,4.3);
83+
System.out.println(comp1);
84+
85+
86+
Computer comp2 = fc.getComputer("Server",30,900,9);
87+
System.out.println(comp2);
88+
89+
}

‎com/factory/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+
}

‎com/factory/FactoryClass.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.factory;
2+
3+
/**
4+
* Created by sdmg15 on 18/03/17.
5+
*/
6+
public class FactoryClass {
7+
8+
public static Computer getComputer(String c, String ram, String hdd, String dd){
9+
if("PC".equalsIgnoreCase(c)) return new PC(ram,hdd,dd);
10+
else if("Server".equalsIgnoreCase(c)) return new Server(ram,hdd,dd);
11+
12+
return null;
13+
}
14+
}

‎com/factory/PC.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.factory;
2+
3+
/**
4+
* Created by sdmg15 on 18/03/17.
5+
*/
6+
public class PC extends Computer {
7+
8+
private int ram;
9+
private double cpu;
10+
private int hdd;
11+
12+
13+
public PC(int ram,int hdd, double 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+
25+
public String getHdd(){
26+
return this.hdd;
27+
}
28+
29+
30+
31+
32+
}

‎com/factory/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 int ram;
8+
private double cpu;
9+
private int hdd;
10+
11+
12+
public Server(int ram,int hdd,double 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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /