-
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.
Closes #5 - Implemented Adapter Exercise
- Loading branch information
Showing
6 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
src/main/java/design/patterns/adapter/caching/AdapterCachingDemo.java
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
2 changes: 1 addition & 1 deletion
2
.../caching/adapters/LineToPointAdapter.java → ...s/adapter/caching/LineToPointAdapter.java
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
17 changes: 17 additions & 0 deletions
17
src/main/java/design/patterns/adapter/exercise/SimpleAdapterDemo.java
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 design.patterns.adapter.exercise; | ||
|
||
import design.patterns.adapter.exercise.model.Square; | ||
|
||
//uses a simple adapter to use the Rectangle interface with a Square | ||
|
||
public class SimpleAdapterDemo { | ||
public static void main(String[] args) { | ||
|
||
SquareToRectangleAdapter adapter = | ||
new SquareToRectangleAdapter(new Square(10)); | ||
|
||
System.out.println("Height: " + adapter.getHeight()); | ||
System.out.println("Width: " + adapter.getWidth()); | ||
System.out.println("Area: " + adapter.getArea()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/design/patterns/adapter/exercise/SquareToRectangleAdapter.java
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,22 @@ | ||
package design.patterns.adapter.exercise; | ||
|
||
import design.patterns.adapter.exercise.model.Rectangle; | ||
import design.patterns.adapter.exercise.model.Square; | ||
|
||
public class SquareToRectangleAdapter implements Rectangle { | ||
|
||
private int side; | ||
|
||
public SquareToRectangleAdapter(Square square) | ||
{ | ||
this.side = square.side; | ||
} | ||
|
||
public int getWidth() { | ||
return side; | ||
} | ||
|
||
public int getHeight() { | ||
return side; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/design/patterns/adapter/exercise/model/Rectangle.java
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,12 @@ | ||
package design.patterns.adapter.exercise.model; | ||
|
||
public interface Rectangle { | ||
|
||
int getWidth(); | ||
int getHeight(); | ||
|
||
default int getArea() | ||
{ | ||
return getWidth() * getHeight(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/design/patterns/adapter/exercise/model/Square.java
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,10 @@ | ||
package design.patterns.adapter.exercise.model; | ||
|
||
public class Square { | ||
|
||
public int side; | ||
|
||
public Square(int side) { | ||
this.side = side; | ||
} | ||
} |