抽象工厂(例子)

CarFactory.java

package com.jzh.abstractFactory;

/**
 * Created by nameJuzhihua on 2017/9/10.
 */
public interface CarFactory {
    Engine createEngine();
    Seat createSeat();
    Tyre createTyre();
}

LuxuryCarFactory.java

package com.jzh.abstractFactory;

/**
 * Created by nameJuzhihua on 2017/9/10.
 */
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();
    }
}

LowCarFactory.java

package com.jzh.abstractFactory;

/**
 * Created by nameJuzhihua on 2017/9/10.
 */
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();
    }
}

Engine.java

package com.jzh.abstractFactory;

/**
 * Created by nameJuzhihua on 2017/9/10.
 */
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

package com.jzh.abstractFactory;

/**
 * Created by nameJuzhihua on 2017/9/10.
 */
public interface Seat {
    void massage();
}

class LuxurySeat implements Seat{
    @Override
    public void massage() {
        System.out.println("按摩");
    }
}

class LowSeat implements Seat{
    @Override
    public void massage() {
        System.out.println("没按摩");
    }
}

Tyre.java

package com.jzh.abstractFactory;

/**
 * Created by nameJuzhihua on 2017/9/10.
 */
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

package com.jzh.abstractFactory;

/**
 * Created by nameJuzhihua on 2017/9/10.
 */
public class Client {
    public static void main(String[] args) {
        CarFactory factory=new LuxuryCarFactory();
        Engine e=factory.createEngine();
        e.run();
        e.start();
    }
}

你可能感兴趣的:(设计模式)