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
}
}