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 d62b1a6

Browse files
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+
}

0 commit comments

Comments
(0)

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