Soldato
Hi All,
I've recently purchased a Pluralsight subscription. One of the tracks I'm currently following is their Design pattern library as it's not something I've particularly touched much of.
Anyway their examples are all .NET which I'm not familiar with so to get to grips I'm looking to create my own simple examples in Java.
First things first was the Adapter pattern, one of the simplest examples I could think of was UK Plug -> US Socket and US Socket -> UK Plug, an adaptor fitting in between both examples.
Here is what I have (please ignore my obvious lack of knowledge in plug wiring ), is my understanding correct?
USPlug.java
UKPlug.java
USSocket.java
UKSocket.java
IAdaptor.java
USUKAdaptor.java
UKUSAdaptor.java
Plugin.java
I've recently purchased a Pluralsight subscription. One of the tracks I'm currently following is their Design pattern library as it's not something I've particularly touched much of.
Anyway their examples are all .NET which I'm not familiar with so to get to grips I'm looking to create my own simple examples in Java.
First things first was the Adapter pattern, one of the simplest examples I could think of was UK Plug -> US Socket and US Socket -> UK Plug, an adaptor fitting in between both examples.
Here is what I have (please ignore my obvious lack of knowledge in plug wiring ), is my understanding correct?
USPlug.java
Code:
package com.adapter;
public class USPlug {
private int live;
private int neutral;
private IAdaptor adaptor;
public USPlug(int live, int neutral) {
this.live = live;
this.neutral = neutral;
}
public int getLive() {
return live;
}
public int getNeutral() {
return neutral;
}
public void setAdaptor(IAdaptor adaptor) {
this.adaptor = adaptor;
}
public String plugin() {
return adaptor.plugin();
}
}
UKPlug.java
Code:
package com.adapter;
public class UKPlug {
private int earth;
private int live;
private int neutral;
private IAdaptor adaptor;
public UKPlug(int earth, int live, int neutral) {
this.earth = earth;
this.live = live;
this.neutral = neutral;
}
public String plugin() {
return adaptor.plugin();
}
/**
* @return the earth
*/
public int getEarth() {
return earth;
}
/**
* @return the live
*/
public int getLive() {
return live;
}
/**
* @return the neutral
*/
public int getNeutral() {
return neutral;
}
/**
* @param adaptor the adaptor to set
*/
public void setAdaptor(IAdaptor adaptor) {
this.adaptor = adaptor;
}
}
USSocket.java
Code:
package com.adapter;
public class USSocket {
public String plugin(int live, int neutral) {
if(live == 1 && neutral == 1) {
return "We have Power!";
} else {
return "Uh Oh somethings not right, check your wiring!";
}
}
}
UKSocket.java
Code:
package com.adapter;
public class UKSocket {
public String plugin(int earth, int live, int neutral) {
if(earth == 1 && live == 1 && neutral ==1) {
return "We have Power!";
} else {
return "Uh Oh somethings not right, check your wiring!";
}
}
}
IAdaptor.java
Code:
package com.adapter;
public interface IAdaptor {
public String plugin();
}
USUKAdaptor.java
Code:
package com.adapter;
public class USUKAdaptor implements IAdaptor {
private USPlug plug;
private UKSocket socket;
public USUKAdaptor(UKSocket socket) {
this.socket = socket;
}
@Override
public String plugin() {
return socket.plugin(1, plug.getLive(), plug.getNeutral());
}
public void setPlug(USPlug plug) {
this.plug = plug;
}
}
UKUSAdaptor.java
Code:
package com.adapter;
public class UKUSAdaptor implements IAdaptor {
private UKPlug plug;
private USSocket socket;
public UKUSAdaptor(USSocket socket) {
this.socket = socket;
}
@Override
public String plugin() {
return socket.plugin(plug.getLive(), plug.getNeutral());
}
public void setPlug(UKPlug plug) {
this.plug = plug;
}
}
Plugin.java
Code:
package com.adapter;
/**
* This is the adaptor pattern
* @author pstatham
*
*/
public class Plugin {
public static void main(String[] args) {
//First test the USUKAdaptor
testUSUKAdaptor();
//Now test the UKUSAdaptor
testUKUSAdaptor();
}
private static void testUSUKAdaptor() {
System.out.println("Testing US->UK Adaptor");
USPlug goodUSPlug = new USPlug(1, 1);
USPlug badUSPlug = new USPlug(0, 1);
UKSocket ukSocket = new UKSocket();
USUKAdaptor usukadaptor = new USUKAdaptor(ukSocket);
usukadaptor.setPlug(goodUSPlug);
goodUSPlug.setAdaptor(usukadaptor);
badUSPlug.setAdaptor(usukadaptor);
System.out.println(goodUSPlug.plugin());
usukadaptor.setPlug(badUSPlug);
System.out.println(badUSPlug.plugin());
}
private static void testUKUSAdaptor() {
System.out.println("Testing UK->US Adaptor");
UKPlug goodUKPlug = new UKPlug(1, 1, 1);
UKPlug badUKPlug = new UKPlug(1, 0, 1);
USSocket usSocket = new USSocket();
UKUSAdaptor ukusadaptor = new UKUSAdaptor(usSocket);
ukusadaptor.setPlug(goodUKPlug);
goodUKPlug.setAdaptor(ukusadaptor);
badUKPlug.setAdaptor(ukusadaptor);
System.out.println(goodUKPlug.plugin());
ukusadaptor.setPlug(badUKPlug);
System.out.println(badUKPlug.plugin());
}
}