Skip to content

Commit d62b1a6

Browse files
committed
software design pattern codes
abstract factory, factory pattern, singleton pattern
1 parent 98073d6 commit d62b1a6

File tree

15 files changed

+244
-0
lines changed

15 files changed

+244
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sdp.factory;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class AndroidButton implements Button{
8+
9+
@Override
10+
public void onClick(){
11+
System.out.println("clicked on android");
12+
}
13+
14+
@Override
15+
public Button getButton(){
16+
return this;
17+
}
18+
19+
@Override
20+
public String toString(){
21+
return "Android button";
22+
}
23+
24+
public void androMethod(){
25+
System.out.println("based on linux");
26+
}
27+
}
28+

design-pattern/factory/Button.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package sdp.factory;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public interface Button {
8+
public void onClick();
9+
public Button getButton();
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sdp.factory;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class ButtonManager {
8+
public static final int BUTTON_ANDROID = 1;
9+
public static final int BUTTON_IOS = 2;
10+
11+
public static Button build(int type){
12+
switch(type){
13+
case BUTTON_ANDROID: return new AndroidButton();
14+
case BUTTON_IOS: return new IOSButton();
15+
default: return null;
16+
}
17+
}
18+
}

design-pattern/factory/IOSButton.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package sdp.factory;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class IOSButton implements Button{
8+
9+
@Override
10+
public void onClick(){
11+
System.out.println("clicked on ios");
12+
}
13+
14+
@Override
15+
public Button getButton(){
16+
return this;
17+
}
18+
19+
@Override
20+
public String toString(){
21+
return "IOS Button";
22+
}
23+
}

design-pattern/factory/Main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package sdp.factory;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class Main {
8+
9+
public static void main(String[] args) {
10+
11+
Button bt1 = ButtonManager.build(ButtonManager.BUTTON_ANDROID);
12+
Button bt2 = ButtonManager.build(ButtonManager.BUTTON_IOS);
13+
14+
System.out.println(bt1);
15+
System.out.println(bt2);
16+
17+
bt1.onClick();
18+
bt2.onClick();
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package sdp.factory.abstracts;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public interface Car {
8+
public int seat();
9+
public int horsePower();
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package sdp.factory.abstracts;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public interface Factory {
8+
public Car build();
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package sdp.factory.abstracts;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class Main {
8+
public static void main(String[] args) {
9+
Car alion = Toyota.getCar(Toyota.SEDAN);
10+
Car prado = Toyota.getCar(Toyota.SUV);
11+
12+
System.out.println("Alion has "+alion.seat()+" seat");
13+
System.out.println("Prado has "+prado.seat()+" seat");
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sdp.factory.abstracts;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class SUVCar implements Car{
8+
9+
@Override
10+
public int seat(){
11+
return 6;
12+
}
13+
14+
@Override
15+
public int horsePower(){
16+
return 1000;
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package sdp.factory.abstracts;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class SUVFactory implements Factory{
8+
9+
@Override
10+
public Car build(){
11+
return new SUVCar();
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sdp.factory.abstracts;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class SedanCar implements Car{
8+
@Override
9+
public int seat(){
10+
return 4;
11+
}
12+
13+
@Override
14+
public int horsePower(){
15+
return 700;
16+
}
17+
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package sdp.factory.abstracts;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class SedanFactory implements Factory{
8+
9+
@Override
10+
public Car build(){
11+
return new SedanCar();
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sdp.factory.abstracts;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class Toyota {
8+
public static final int SUV = 1;
9+
public static final int SEDAN = 2;
10+
public static Car getCar(int type){
11+
switch(type){
12+
case SUV: return new SUVCar();
13+
case SEDAN: return new SedanCar();
14+
default: return null;
15+
}
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package sdp.singleton;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class DateTime {
8+
9+
private static DateTime obj = new DateTime();
10+
11+
private DateTime(){}
12+
13+
public static DateTime init(){
14+
return obj;
15+
}
16+
17+
public long now(){
18+
return System.currentTimeMillis();
19+
}
20+
}

design-pattern/singleton/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package sdp.singleton;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class Main {
8+
public static void main(String[] args) {
9+
DateTime dt = DateTime.init();
10+
System.out.println(dt.now());
11+
}
12+
}

0 commit comments

Comments
 (0)