码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之工厂模式 Factory实现

时间:2017-09-02 12:10:37      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:none   启动慢   sage   rac   stat   ati   out   closed   sea   

simpleFactory

技术分享
//car接口
public interface Car {
    void run();
}

//两个实现类
public class Audi implements Car{

    public void run() {
        System.out.println("奥迪在跑");
    }
    
}

public class Byd implements Car{

    public void run() {
        System.out.println("Byd在跑");
    }
    
}
Car接口以及两个实现类
技术分享
//第一种实现
public class CarFactory {
    public static Car createCar(String type){
        if("奥迪".equals(type)){
            return new Audi();
        }else if("比亚迪".equals(type)){
            return new Byd();
        }else{
            return null;
        }
    }
}

//第二种实现
public class CarFactory2 {
    public static Car createAudi(){
        return new Audi();
    }
    
    public static Car createByd(){
        return new Byd();
    }    
}
工厂类实现
技术分享
/**
 * 普通模式,无工厂模式
 * @author benny
 *
 */

public class Client01 {
    public static void main(String[] args) {
        Car c1 = new Audi();
        Car c2 = new Byd();
        
        c1.run();
        c2.run();
    }
}


/**
 * 简单工厂模式
 * @author benny
 *
 */

public class Client02 {
    public static void main(String[] args) {
        Car c1 = CarFactory.createCar("奥迪");
        Car c2 = CarFactory.createCar("比亚迪");
        
        c1.run();
        c2.run();
    }
}
调用

 factoryMethod

技术分享
public interface Car {
    void run();
}

public class Audi implements Car{

    public void run() {
        System.out.println("奥迪在跑");
    }
    
}

public class Benz implements Car{

    public void run() {
        System.out.println("奔驰在跑");
    }
    
}

public class Byd implements Car{

    public void run() {
        System.out.println("Byd在跑");
    }
    
}
car接口及其实现
技术分享
public interface CarFactory {
    Car createCar();
}

public class AudiFactory implements CarFactory {

    @Override
    public Car createCar() {
        return new Audi();

    }

}

public class BenzFactory implements CarFactory {

    @Override
    public Car createCar() {
        return new Benz();

    }

}

public class BydFactory implements CarFactory {

    @Override
    public Car createCar() {
        return new Byd();

    }

}
工厂接口及其实现
技术分享
/**
 * 最大的好处实现了开闭原则
 * @author benny
 *
 */
public class Client {
    public static void main(String[] args) {
        Car c1 = new AudiFactory().createCar();
        Car c2 = new BydFactory().createCar();
        Car c3 = new BenzFactory().createCar();
        
        c1.run();
        c2.run();
        c3.run();
    }
}
调用

abstractFactory

技术分享
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("启动慢,不可以自动启停");
        
    }
    
}
引擎接口及其实现
技术分享
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("磨损快");
        
    }
    
}
轮胎接口及其实现
技术分享
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("不能按摩");
        
    }
    
}
座椅接口及其实现
技术分享
public interface CarFactory {
    Engine createEngine();
    Seat createSeat();
    Tyre createTyre();
}

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();
    }

}


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();
    }

}
工厂接口及其实现
技术分享
public class Client {
    public static void main(String[] args) {
        CarFactory factory = new LuxuryCarFactory();
        Engine e =factory.createEngine();
        e.run();
        e.start();
    }
}
调用

 

设计模式之工厂模式 Factory实现

标签:none   启动慢   sage   rac   stat   ati   out   closed   sea   

原文地址:http://www.cnblogs.com/qingdaofu/p/7466063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!