Java一个接口多个实现类的调用方式

本文介绍了Java中多态实例化、工厂模式(包括单例和基于接口的工厂)、以及枚举模式的使用,通过实例演示了如何通过这些模式创建和管理对象实例。
摘要由CSDN通过智能技术生成

1、多态实例化对象

1.1、定义一个接口

public interface AnimalService {
    public void sound();
}

1.2、创建多个实现类

public class CatService implements AnimalService {
    @Override
    public void sound() {
        System.out.println("喵喵喵");
    }
}
public class DogService implements AnimalService {
    @Override
    public void sound() {
        System.out.println("汪汪汪");
    }
}

1.3、使用多态实例化对象

public class Main {
    public static void main(String[] args) {
        AnimalService animalService1 = new DogService(); // 使用多态实例化Dog对象
        AnimalService animalService2 = new CatService(); // 使用多态实例化Cat对象
        animalService1.sound(); // 调用Dog类的sound()方法
        animalService2.sound(); // 调用Cat类的sound()方法
    }
}

 2、工厂模式

2.1、定义一个接口

public interface AnimalService {
    public void sound();
}

2.2、创建多个实现类

public class CatService implements AnimalService {
    private static CatService instance;
    // 私有构造函数
    private CatService() {
        // 防止通过反射创建实例
        if (instance != null) {
            throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
        }
    }
    // 获取单例实例的方法
    public static CatService getInstance() {
        if (instance == null) {
            synchronized (CatService.class) {
                if (instance == null) {
                    instance = new CatService();
                }
            }
        }
        return instance;
    }
    @Override
    public void sound() {
        System.out.println("喵喵喵");
    }
}
public class DogService implements AnimalService {
    private static DogService instance;
    // 私有构造函数
    private DogService() {
        // 防止通过反射创建实例
        if (instance != null) {
            throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
        }
    }
    // 获取单例实例的方法
    public static DogService getInstance() {
        if (instance == null) {
            synchronized (DogService.class) {
                if (instance == null) {
                    instance = new DogService();
                }
            }
        }
        return instance;
    }
    @Override
    public void sound() {
        System.out.println("汪汪汪");
    }
}

2.3、创建工厂

public class AnimalServiceFactory {
    public static AnimalService createAnimalService(String productType) {
        if (productType.equalsIgnoreCase("cat")) {
            return CatService.getInstance();
        } else if (productType.equalsIgnoreCase("dog")) {
            return DogService.getInstance();
        } else {
            throw new IllegalArgumentException("Invalid product type");
        }
    }
}

2.4、使用工厂实例化对象

public class Main {
    public static void main(String[] args) {
        AnimalService animalService1 = AnimalServiceFactory.createAnimalService("cat");
        animalService1.sound(); // 输出:Animal A info
        AnimalService animalService2 = AnimalServiceFactory.createAnimalService("dog");
        animalService2.sound(); // 输出:Animal B info
    }
}

 3、枚举模式

3.1、定义一个接口

public interface AnimalService {
    public void sound();
}

3.2、创建多个实现类

public class CatService implements AnimalService {
    private static CatService instance;
    // 私有构造函数
    private CatService() {
        // 防止通过反射创建实例
        if (instance != null) {
            throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
        }
    }
    // 获取单例实例的方法
    public static CatService getInstance() {
        if (instance == null) {
            synchronized (CatService.class) {
                if (instance == null) {
                    instance = new CatService();
                }
            }
        }
        return instance;
    }
    @Override
    public void sound() {
        System.out.println("喵喵喵");
    }
}
public class DogService implements AnimalService {
    private static DogService instance;
    // 私有构造函数
    private DogService() {
        // 防止通过反射创建实例
        if (instance != null) {
            throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
        }
    }
    // 获取单例实例的方法
    public static DogService getInstance() {
        if (instance == null) {
            synchronized (DogService.class) {
                if (instance == null) {
                    instance = new DogService();
                }
            }
        }
        return instance;
    }
    @Override
    public void sound() {
        System.out.println("汪汪汪");
    }
}

3.3、创建工厂

public enum AnimalFactory {
    IMPLEMENTATION_A {
        @Override
        public AnimalService create() {
            return CatService.getInstance();
        }
    },
    IMPLEMENTATION_B {
        @Override
        public AnimalService create() {
            return DogService.getInstance();
        }
    };
    public abstract AnimalService create();
}

3.4、使用工厂实例化对象

public class Main {
    public static void main(String[] args) {
        AnimalService animalService1 = AnimalFactory.IMPLEMENTATION_A.create();
        animalService1.sound(); // 输出:Implementation A
        AnimalService animalService2 = AnimalFactory.IMPLEMENTATION_B.create();
        animalService2.sound(); // 输出:Implementation B
    }
}

  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值