版权声明:转载注明出处 https://blog.csdn.net/jy02268879/article/details/81637198
目录
CarFactory.java
public interface CarFactory {
Engine createEngine();
Seat createSeat();
Tyre createTyre();
}
LowCarFactory.java
public class LowCarFactory implements CarFactory{
@Override
public Engine createEngine() {
return new LowEngine();
}
@Override
public Seat createSeat() {
return new LowSeat();
}
@Override
public Tyre createTyre() {
return new LowTyre();
}
}
LuxuryCarFactory.java
public class LuxuryCarFactory implements CarFactory{
@Override
public Engine createEngine() {
return new LuxuryEngine();
}
@Override
public Seat createSeat() {
return new LuxurySeat();
}
@Override
public Tyre createTyre() {
return new LuxuryTyre();
}
}
Engine.java
public interface Engine {
void run();
void start();
}
class LuxuryEngine implements Engine{
@Override
public void run() {
System.out.println("ת�ÿ�");
}
@Override
public void start() {
System.out.println("�����죡�����Զ���ͣ��");
}
}
class LowEngine implements Engine{
@Override
public void run() {
System.out.println("ת����");
}
@Override
public void start() {
System.out.println("�������������Զ���ͣ��");
}
}
Seat.java
public interface Seat {
void massage();
}
class LuxurySeat implements Seat{
@Override
public void massage() {
System.out.print("�����Զ���Ħ");
}
}
class LowSeat implements Seat{
@Override
public void massage() {
System.out.print("�����Զ���Ħ");
}
}
Tyre.java
public interface Tyre {
void revolve();
}
class LuxuryTyre implements Tyre{
@Override
public void revolve() {
System.out.println("��ת��ĥ��");
}
}
class LowTyre implements Tyre{
@Override
public void revolve() {
System.out.println("��תĥ���");
}
}
Client.java
public class Client {
public static void main(String[] args) {
CarFactory luxuryFactory = new LuxuryCarFactory();
Engine luxuryEngine = luxuryFactory.createEngine();
luxuryEngine.run();
luxuryEngine.start();
Seat luxurySeat = luxuryFactory.createSeat();
Tyre luxuryTyre = luxuryFactory.createTyre();
}
}