Yesterday I used to be making an attempt to refactor some code which has a number of code duplication, static kind casting between sub varieties and cohesive. After some refactoring I made a decision to make use of java’s technique design sample to take away code duplication and make it extendable.
The motive behind utilizing technique sample was to maneuver conduct out of the category, make it maximize cohesion and reduce coupling and use of interface, summary class to keep away from tide coupling.
So let’s talk about this with an instance…
You might have an Advert company which has a service which rotate various kinds of advert in some other ways. So I transfer out the conduct of rotating advert into one interface.
1
2
3
4
5
|
bundle org.techzoo.technique;
public interface AdRotable<T extends Advert> {
T rotate(T x);
}
|
Now, since we now have various kinds of adverts so it an excellent design to create one summary advert class with some fundamental performance…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
bundle org.techzoo.technique;
public summary class Advert<T extends Advert<T>> {
personal AdRotable<T> advert;
protected summary T getAdType();
protected summary boolean isRotable();
public Advert(AdRotable<T> advert)
{this.advert = advert;}
protected T rotate()
{return advert.rotate(getAdType());}
}
|
Right here you possibly can see I’ve created one summary getType() technique which return the sub-class kind. This manner we don’t have to kind case courses in rotate() technique…
Now create our Advert class which extends Advert and implements given summary strategies…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
bundle org.techzoo.technique;
import static java.lang.String.format;
public class BannerAd extends Advert<BannerAd> {
personal String title;
public BannerAd(AdRotable<BannerAd> advert,
String title) {
tremendous(advert);
this.title = title;
}
@Override
protected BannerAd getAdType() {
return this;
}
@Override
protected boolean isRotable() {
return true;
}
public String getTitle()
{return title;}
public void setTitle(String title)
{this.title = title;}
@Override
public String toString()
{ return format(“BannerAd [Title = %s]”,title);}
}
|
Have a look on rotate() technique, which takes BannedAd as a parameter and keep away from Sort-casting.
Create one class which has the Advert rotate conduct which we decouple from our major BannerAd class. This manner I’ve moved the conduct of rotating advert and make it minimal coupling.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
bundle org.techzoo.technique;
public class BannedAdRotable implements AdRotable<BannerAd> {
@Override
public BannerAd rotate(BannerAd x) {
if(x.isRotable()){
String textual content = new StringBuffer(x.getTitle())
.reverse().toString();
x.setTitle(textual content);
return x;
}
return x;
}
}
|
Now it time to check our Advert Service…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
bundle org.techzoo.technique;
import static org.junit.Assert.*;
import org.junit.Take a look at;
public class RotateAdTest {
@Take a look at
public void rotateAd() throws Exception {
String anticipated = “nahK fisuoT”;
BannerAd advert = new BannerAd(new BannedAdRotable(),
“Tousif Khan”);
advert.rotate();
assertEquals(anticipated, advert.getTitle());
}
}
|
Screenshot of JUnit..
Supply techzoo.org