8000 designing pattered added · minloved/JavaStudSpringMVCWeb@fe5ee6b · GitHub
[go: up one dir, main page]

Skip to content

Commit fe5ee6b

Browse files
designing pattered added
1 parent 9cfe29b commit fe5ee6b

File tree

18 files changed

+288
-0
lines changed

18 files changed

+288
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
public interface Bank {
4+
void info();
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
public class BankFactory {
4+
public Bank getBank(BankType bankType) {
5+
6+
if (bankType == null) {
7+
return null;
8+
}
9+
10+
if (bankType == BankType.GLOBAL) {
11+
return new Global();
12+
} else if (bankType == BankType.NABIL) {
13+
return new Nabil();
14+
}
15+
16+
return null;
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
public enum BankType {
4+
GLOBAL, NABIL
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
public class BuilderPatternExample {
4+
public static void main(String[] args) {
5+
User user = User.UserBuilder()
6+
.name("RAM")
7+
.age(20)
8+
.email("ram@gmail.com")
9+
.build();
10+
System.out.println(user);
11+
}
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
public class Company {
4+
5+
private static Company instance = new Company();
6+
7+
private Company() {
8+
9+
}
10+
11+
public static Company getInstance() {
12+
return instance;
13+
}
14+
15+
public String getName() {
16+
return "NGSOFT";
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "NGSOFT CORP.";
22+
}
23+
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
public class FactoryPatterenExample {
4+
public static void main(String[] args) {
5+
BankFactory bankFactory = new BankFactory();
6+
7+
Bank global = bankFactory.getBank(BankType.GLOBAL);
8+
global.info();
9+
10+
Bank nabil = bankFactory.getBank(BankType.NABIL);
11+
nabil.info();
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
public class Global implements Bank {
4+
5+
@Override
6+
public void info() {
7+
System.out.println("Global Bank is largest venture bank in Nepal since 2005.");
8+
}
9+
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
public class Nabil implements Bank {
4+
5+
@Override
6+
public void info() {
7+
System.out.println("Nabil Bank is Pioneer bank in Nepal.");
8+
9+
}
10+
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
public class SingletonPatternExample {
4+
public static void main(String[] args) {
5+
Company comp1 = Company.getInstance();
6+
System.out.println(comp1);
7+
8+
Company comp2 = Company.getInstance();
9+
System.out.println(comp2);
10+
11+
System.out.println(comp1 == comp2);
12+
13+
}
14+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.javastud.springmvcweb.designpatterns;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class User {
11+
12+
private String name;
13+
private int age;
14+
private String email;
15+
16+
public static UserBuilder UserBuilder() {
17+
return new UserBuilder();
18+
}
19+
20+
public static class UserBuilder {
21+
private String name;
22+
private int age;
23+
private String email;
24+
25+
public UserBuilder() {
26+
}
27+
28+
public UserBuilder name(String name) {
29+
this.name = name;
30+
return this;
31+
}
32+
33+
public UserBuilder age(int age) {
34+
this.age = age;
35+
return this;
36+
}
37+
38+
public UserBuilder email(String email) {
39+
this.email = email;
40+
return this;
41+
}
42+
43+
public User build() {
44+
return new User(this.name, this.age, this.email);
45+
}
46+
47+
}
48+
49+
}

0 commit comments

Comments
 (0)
0