Skip to content

Commit 0562eb6

Browse files
committed
Factory Design Pattern
1 parent 1b35f71 commit 0562eb6

16 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"java.project.sourcePaths": ["src"],
3+
"java.project.outputPath": "bin",
4+
"java.project.referencedLibraries": [
5+
"lib/**/*.jar"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Getting Started
2+
3+
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
4+
5+
## Folder Structure
6+
7+
The workspace contains two folders by default, where:
8+
9+
- `src`: the folder to maintain sources
10+
- `lib`: the folder to maintain dependencies
11+
12+
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
13+
14+
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
15+
16+
## Dependency Management
17+
18+
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class AndroidAppButton implements Button {
2+
3+
@Override
4+
public void onDoubleTap() {
5+
6+
System.out.println("Android app use onDoubleTap Button");
7+
}
8+
9+
@Override
10+
public void onLongPressed() {
11+
12+
System.out.println("Android app use onLongPressed Button");
13+
}
14+
15+
@Override
16+
public void onPressed() {
17+
18+
System.out.println("Android app use onPressed Button");
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class App {
2+
public static void main(String[] args) throws Exception {
3+
4+
Button btn1 = ButtonFactory.createButton("Android");
5+
btn1.onDoubleTap();
6+
btn1.onLongPressed();
7+
btn1.onPressed();
8+
9+
Button btn2 = ButtonFactory.createButton("ios");
10+
btn2.onDoubleTap();
11+
btn2.onLongPressed();
12+
btn2.onPressed();
13+
14+
Button btn3 = ButtonFactory.createButton("web");
15+
btn3.onDoubleTap();
16+
btn3.onLongPressed();
17+
btn3.onPressed();
18+
19+
// Button btn4 = ButtonFactory.createButton("Windows");
20+
// btn4.onLongPressed();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public interface Button {
2+
3+
void onDoubleTap();
4+
void onPressed();
5+
void onLongPressed();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
public class ButtonFactory {
3+
4+
public static Button createButton(String appType) {
5+
6+
if(appType.trim().equalsIgnoreCase("Android")) {
7+
8+
return new AndroidAppButton();
9+
} else if(appType.trim().equalsIgnoreCase("ios")) {
10+
11+
return new IOSAppButton();
12+
} else if(appType.trim().equalsIgnoreCase("web")) {
13+
14+
return new WebAppButton();
15+
}
16+
throw new IllegalAppTypeException("Unsupported type");
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class IOSAppButton implements Button {
2+
3+
@Override
4+
public void onDoubleTap() {
5+
6+
System.out.println("ios app use onDoubleTap Button");
7+
}
8+
9+
@Override
10+
public void onLongPressed() {
11+
12+
System.out.println("ios app use onLongPressed Button");
13+
}
14+
15+
@Override
16+
public void onPressed() {
17+
18+
System.out.println("ios app use onPressed Button");
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class IllegalAppTypeException extends RuntimeException {
2+
3+
IllegalAppTypeException(String msg) {
4+
5+
super(msg);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class WebAppButton implements Button{
2+
3+
@Override
4+
public void onDoubleTap() {
5+
6+
System.out.println("Web App use onDoubleTap Button");
7+
}
8+
9+
@Override
10+
public void onLongPressed() {
11+
12+
System.out.println("Web App use onLongPressed Button");
13+
}
14+
15+
@Override
16+
public void onPressed() {
17+
18+
System.out.println("Web App use onPressed Button");
19+
}
20+
}

0 commit comments

Comments
 (0)