Skip to content

Commit d174c87

Browse files
committed
Proxy design pattern example - iPhone Proxy
1 parent 83f012f commit d174c87

4 files changed

Lines changed: 80 additions & 2 deletions

File tree

patternBonus/src/com/premaseem/Client.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,28 @@
44
@author: Aseem Jain
55
@title: Design Patterns with Java 9
66
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7-
@copyright: 2018 Packt Publication
87
*/
98
public class Client {
109
public static void main (String[] args) {
11-
System.out.println("Singleton cook example ");
10+
System.out.println("Proxy iPhone example");
11+
12+
// Programming against interface
13+
IPhone iPhone;
14+
15+
// Loading proxy iPhone for selection purpose
16+
iPhone = new ProxyiPhone();
17+
phoneOperation(iPhone);
18+
19+
// Loading real iPhone for operational and sales purpose
20+
iPhone = new RealiPhone();
21+
phoneOperation(iPhone);
22+
23+
}
24+
25+
public static void phoneOperation(IPhone iPhone){
26+
System.out.println("### Operations running on - "+ iPhone.getClass().getSimpleName());
27+
iPhone.getColor();
28+
iPhone.getDimension();
29+
iPhone.operateDisplay();
1230
}
1331
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.premaseem;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
public interface IPhone {
9+
10+
void getColor();
11+
void getDimension();
12+
void operateDisplay();
13+
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.premaseem;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
public class ProxyiPhone implements IPhone{
9+
@Override
10+
public void getColor () {
11+
System.out.println(this.getClass().getSimpleName() +" Colors: Gold, Rose Gold, Carbon Black");
12+
}
13+
14+
@Override
15+
public void getDimension () {
16+
System.out.println(this.getClass().getSimpleName() +" Dimension: Normal and Plus");
17+
}
18+
19+
@Override
20+
public void operateDisplay () {
21+
System.out.println(this.getClass().getSimpleName() +" Sorry, This is proxy object, display will work only in Real object");
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.premaseem;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
public class RealiPhone implements IPhone{
9+
@Override
10+
public void getColor () {
11+
System.out.println(this.getClass().getSimpleName() +" Colors: Gold, Rose Gold, Carbon Black");
12+
}
13+
14+
@Override
15+
public void getDimension () {
16+
System.out.println(this.getClass().getSimpleName() + " Dimension: Normal and Plus");
17+
}
18+
19+
@Override
20+
public void operateDisplay () {
21+
System.out.println(this.getClass().getSimpleName() +" Multi color display working in real Phone object :-) ");
22+
}
23+
}

0 commit comments

Comments
 (0)