forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Proxy] Added images examples and atm machine example
- Loading branch information
1 parent
bacd531
commit 365f40e
Showing
8 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package proxy.examples.atm; | ||
|
||
/** | ||
* Created by luisburgos on 21/09/15. | ||
*/ | ||
public class ATMClient { | ||
|
||
public static void main(String[] args) { | ||
|
||
GetATMData realAtmMachine = new ATMMachine(1000); | ||
GetATMData atmProxy = new ATMProxy(realAtmMachine); | ||
|
||
System.out.println("ATM Machine, cash available: " + atmProxy.getCashInMachine()); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package proxy.examples.atm; | ||
|
||
/** | ||
* Created by luisburgos on 21/09/15. | ||
*/ | ||
public class ATMMachine implements GetATMData { | ||
|
||
private int cashInMachine; | ||
|
||
public ATMMachine(int cashInMachine){ | ||
this.cashInMachine = cashInMachine; | ||
} | ||
@Override | ||
public int getCashInMachine() { | ||
return cashInMachine; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package proxy.examples.atm; | ||
|
||
/** | ||
* Created by luisburgos on 21/09/15. | ||
*/ | ||
public class ATMProxy implements GetATMData { | ||
|
||
private GetATMData atmMachine; | ||
|
||
public ATMProxy(GetATMData atmMachine){ | ||
this.atmMachine = atmMachine; | ||
} | ||
|
||
@Override | ||
public int getCashInMachine() { | ||
return atmMachine.getCashInMachine(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package proxy.examples.atm; | ||
|
||
/** | ||
* Created by luisburgos on 21/09/15. | ||
*/ | ||
public interface GetATMData { | ||
public int getCashInMachine(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package proxy.examples.images; | ||
|
||
/** | ||
* Created by luisburgos on 21/09/15. | ||
*/ | ||
public class ClientImage { | ||
|
||
public static void main(String[] args) { | ||
|
||
Image image = new ProxyImage("img_10.jpg"); | ||
|
||
//image will be loaded from disk | ||
image.display(); | ||
System.out.println(""); | ||
|
||
//image will NOT be loaded from disk | ||
image.display(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package proxy.examples.images; | ||
|
||
/** | ||
* Created by luisburgos on 21/09/15. | ||
*/ | ||
public interface Image { | ||
public void display(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package proxy.examples.images; | ||
|
||
/** | ||
* Created by luisburgos on 21/09/15. | ||
*/ | ||
public class ProxyImage implements Image { | ||
|
||
private RealImage realImage; | ||
private String imageFileName; | ||
|
||
public ProxyImage(String fileName){ | ||
this.imageFileName = fileName; | ||
} | ||
|
||
@Override | ||
public void display() { | ||
if(realImage == null){ | ||
realImage = new RealImage(imageFileName); | ||
} | ||
realImage.display(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package proxy.examples.images; | ||
|
||
/** | ||
* Created by luisburgos on 21/09/15. | ||
*/ | ||
public class RealImage implements Image { | ||
|
||
private String imageFileName; | ||
|
||
public RealImage(String imageFileName){ | ||
this.imageFileName = imageFileName; | ||
loadFromDisk(imageFileName); | ||
} | ||
|
||
@Override | ||
public void display() { | ||
System.out.println("Displaying " + imageFileName); | ||
} | ||
|
||
private void loadFromDisk(String imageFileName){ | ||
System.out.println("Loading " + imageFileName); | ||
} | ||
|
||
} |