- 用來生產(chǎn)不同產(chǎn)品族的全部產(chǎn)品
public interface Mouse {
void make();
}
public interface Keyboard {
void make();
}
public class DellMouse implements Mouse {
@Override
public void make() {
System.out.println("make dell house");
}
}
public class HpMouse implements Mouse {
@Override
public void make() {
System.out.println("make hp house");
}
}
public class DellKeyboard implements Keyboard {
@Override
public void make() {
System.out.println("make dell keyboard");
}
}
public class HpKeyboard implements Keyboard {
@Override
public void make() {
System.out.println("make hp keyboard");
}
}
public interface DeviceFactory {
Mouse produceMouse();
Keyboard produceKeyboard();
}
public class DellFactory implements DeviceFactory {
@Override
public Mouse produceMouse() {
return new DellMouse();
}
@Override
public Keyboard produceKeyboard() {
return new DellKeyboard();
}
}
public class HpFactory implements DeviceFactory {
@Override
public Mouse produceMouse() {
return new HpMouse();
}
@Override
public Keyboard produceKeyboard() {
return new HpKeyboard();
}
}
public class DeviceFactoryTest {
public static void main(String[] args) {
DeviceFactory factory = new DellFactory();
Mouse mouse = factory.produceMouse();
mouse.make();
Keyboard keyboard = factory.produceKeyboard();
keyboard.make();
}
}