Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitafe5d24

Browse files
committed
Added adapter pattern and its test case
1 parent7e33042 commitafe5d24

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
packagesrc.main.java.com.designpatterns.structural.adapter;
2+
3+
publicclassBugattiVeyronimplementsMovable {
4+
@Override
5+
publicdoublegetSpeed() {
6+
return268;
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packagesrc.main.java.com.designpatterns.structural.adapter;
2+
3+
publicinterfaceMovable {
4+
// Returns the speed of the movable in MPH
5+
doublegetSpeed();
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packagesrc.main.java.com.designpatterns.structural.adapter;
2+
3+
/**
4+
* An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected
5+
* directly. An Adapter wraps an existing class with a new interface so that it becomes compatible with the client’s
6+
* interface.
7+
* <br>
8+
* The main motive behind using this pattern is to convert an existing interface into another interface that the client
9+
* expects. It’s usually implemented once the application is designed.
10+
*
11+
* @see <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter Pattern</a>
12+
*/
13+
publicinterfaceMovableAdapter {
14+
// Returns the speed of the movable in KPH
15+
doublegetSpeed();
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagesrc.main.java.com.designpatterns.structural.adapter;
2+
3+
publicclassMovableAdapterImplimplementsMovableAdapter {
4+
privateMovableluxuryCars;
5+
6+
publicMovableAdapterImpl(MovableluxuryCars) {
7+
this.luxuryCars =luxuryCars;
8+
}
9+
10+
@Override
11+
publicdoublegetSpeed() {
12+
returnconvertMPHtoKMPH(luxuryCars.getSpeed());
13+
}
14+
15+
privatedoubleconvertMPHtoKMPH(doublemph) {
16+
returnmph *1.60934;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagesrc.test.java.com.designpatterns.structural;
2+
3+
importorg.junit.Test;
4+
importsrc.main.java.com.designpatterns.structural.adapter.BugattiVeyron;
5+
importsrc.main.java.com.designpatterns.structural.adapter.Movable;
6+
importsrc.main.java.com.designpatterns.structural.adapter.MovableAdapter;
7+
importsrc.main.java.com.designpatterns.structural.adapter.MovableAdapterImpl;
8+
9+
importstaticorg.junit.Assert.assertEquals;
10+
11+
publicclassMovableAdapterTest {
12+
@Test
13+
publicvoidtestMovableAdapter() {
14+
MovablebugattiVeyron =newBugattiVeyron();
15+
MovableAdapterbugattiVeyronAdapter =newMovableAdapterImpl(bugattiVeyron);
16+
assertEquals(bugattiVeyronAdapter.getSpeed(),431.30312,0.00001);
17+
}
18+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp