//--------------------------------------发动机配置------------------------
package java_231_GOF23设计模式_抽象工厂模式详解_练习;
/**

  • Engine发动机接口
    */
    public interface Engine {
    void run();//run运转
    void start();//start启动
    }
    //实现类1;LuxuryEngine高端发动机
    class LuxuryEngine implements Engine{

    public void run() {
    System.out.println(“运转平稳”);
    }
    public void start() {
    System.out.println(“启动快”);
    }
    }
    //实现类2;LowEngine低端发动机
    class LowEngine implements Engine{

    public void run() {
    System.out.println(“运转稳定性差”);
    }
    public void start() {
    System.out.println(“启动慢”);
    }
    }
    //------------------------座椅-配置--------------------
    package java_231_GOF23设计模式_抽象工厂模式详解_练习;
    /**

  • Seat座椅接口
    */
    public interface Seat {
    void massage();//massage按摩
    }
    //高配座椅
    class LuxurySeat implements Seat{

    public void massage() {
    System.out.println(“带按摩功能”);
    }
    }
    //低配座椅
    class LowSeat implements Seat{

    public void massage() {
    System.out.println(“无按摩功能”);
    }
    }
    //-------------------------轮胎–配置-------------------------
    package java_231_GOF23设计模式_抽象工厂模式详解_练习;
    /**

  • Tyre轮胎接口
    */
    public interface Tyre {
    void revolve();//revolve旋转
    }
    //高配轮胎
    class LuxuryTyre implements Tyre{

    public void revolve() {
    System.out.println(“轮胎耐磨性好”);
    }
    }
    //低配轮胎
    class LowTyre implements Tyre{

    public void revolve() {
    System.out.println(“轮胎耐磨性差”);
    }
    }
    //-------------------------汽车工厂接口----------
    package java_231_GOF23设计模式_抽象工厂模式详解_练习;
    /**

  • CarFactory汽车工厂接口
    */
    public interface CarFactory {
    //创建对应的3个方法;因为要返回相关产品 所以不能使用void;如:void createEngine();
    Engine createEngine();
    Seat createSeat();
    Tyre createTyre();

}
//-----------------------高配汽车工厂类-----------------------------
package java_231_GOF23设计模式_抽象工厂模式详解_练习;
/**

  • LuxuryCarFactory高配汽车工厂
    */
    public class LuxuryCarFactory implements CarFactory{
    //重写方法
    public Engine createEngine() {

     return new LuxuryEngine();
    

    }

    public Seat createSeat() {

     return new LuxurySeat();
    

    }

    public Tyre createTyre() {

     return new LuxuryTyre();
    

    }

}
//-----------------------低配汽车工厂类----------------
package java_231_GOF23设计模式_抽象工厂模式详解_练习;
/**

  • LowCarFactory低配汽车工厂
    */
    public class LowCarFactory implements CarFactory{
    //重写方法
    public Engine createEngine() {

     return new  LowEngine();
    

    }

    public Seat createSeat() {

     return new LowSeat();
    

    }

    public Tyre createTyre() {

     return new LowTyre();
    

    }
    }
    //-----------------------------测试-----------------------------
    package java_231_GOF23设计模式_抽象工厂模式详解_练习;

public class Client {

/**
 * 测试
 */
public static void main(String[] args) {
	CarFactory factory = new LuxuryCarFactory();
	Engine e = factory.createEngine();
	e.run();
	e.start();
}

}
//结果------------------------------------


更多推荐

java_231_GOF23设计模式_抽象工厂模式详解_练习