解决条件分支
解决蛋糕制作问题
前言
在程序中,避免不了过多的条件分支,if() else… 这会让整个程序看起来不是那么优雅,不美观,当然,我们也可以采用设计模式中的策略模式来解决,这期主要讲的是通过注解的方式自定义tag标识去找寻需要类从而实现解决if else,采用原生的方式 不使用spring的注解
提示:以下是本篇文章正文内容,下面案例可供参考
编写注解
@IfMapper
用于扫描实现类的位置
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface IfMapper {
String pack() default "";
}
@tag
自定义标识注解,用于找寻实现类
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface tag {
String name() default "";
}
工具类
public class ClassUtils {
public ClassUtils() {
}
public static Set<Class<?>> getClasses(String pack) throws Exception {
Set<Class<?>> classes = new LinkedHashSet();
boolean recursive = true;
String packageName = pack;
String packageDirName = pack.replace('.', '/');
try {
Enumeration dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
while(dirs.hasMoreElements()) {
URL url = (URL)dirs.nextElement();
String protocol = url.getProtocol();
if ("file".equals(protocol)) {
String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
findClassesInPackageByFile(packageName, filePath, recursive, classes);
} else if ("jar".equals(protocol)) {
try {
JarFile jar = ((JarURLConnection)url.openConnection()).getJarFile();
Enumeration<JarEntry> entries = jar.entries();
findClassesInPackageByJar(packageName, entries, packageDirName, recursive, classes);
} catch (IOException var10) {
var10.printStackTrace();
}
}
}
} catch (IOException var11) {
var11.printStackTrace();
}
return classes;
}
private static void findClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, Set<Class<?>> classes) {
File dir = new File(packagePath);
if (dir.exists() && dir.isDirectory()) {
File[] dirfiles = dir.listFiles(new FileFilter() {
public boolean accept(File file) {
return recursive && file.isDirectory() || file.getName().endsWith(".class");
}
});
File[] var6 = dirfiles;
int var7 = dirfiles.length;
for(int var8 = 0; var8 < var7; ++var8) {
File file = var6[var8];
if (file.isDirectory()) {
findClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive, classes);
} else {
String className = file.getName().substring(0, file.getName().length() - 6);
try {
classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className));
} catch (ClassNotFoundException var12) {
var12.printStackTrace();
}
}
}
}
}
private static void findClassesInPackageByJar(String packageName, Enumeration<JarEntry> entries, String packageDirName, boolean recursive, Set<Class<?>> classes) {
while(entries.hasMoreElements()) {
JarEntry entry = (JarEntry)entries.nextElement();
String name = entry.getName();
if (name.charAt(0) == '/') {
name = name.substring(1);
}
if (name.startsWith(packageDirName)) {
int idx = name.lastIndexOf(47);
if (idx != -1) {
packageName = name.substring(0, idx).replace('/', '.');
}
if ((idx != -1 || recursive) && name.endsWith(".class") && !entry.isDirectory()) {
String className = name.substring(packageName.length() + 1, name.length() - 6);
try {
classes.add(Class.forName(packageName + '.' + className));
} catch (ClassNotFoundException var10) {
var10.printStackTrace();
}
}
}
}
}
}
编写反射类
public final class GetObject {
public GetObject() {
}
public static <E> E getObject(String name, String packageName) {
Object t = null;
try {
Set<Class<?>> classes = ClassUtils.getClasses(packageName);
Iterator var5 = classes.iterator();
while(var5.hasNext()) {
Class c = (Class)var5.next();
if (c.isAnnotationPresent(tag.class) && classNameAndObject(c).equals(name)) {
t = Class.forName(c.getName()).getDeclaredConstructor().newInstance();
}
}
} catch (Exception var7) {
System.out.println("没有找到该类");
}
return (E) t;
}
public static <E> E getEnumObject(String name, String packageName) {
Object t = null;
try {
Set<Class<?>> classes = ClassUtils.getClasses(packageName);
Iterator var5 = classes.iterator();
while(var5.hasNext()) {
Class c = (Class)var5.next();
if (c.isAnnotationPresent(tag.class) && classNameAndObject(c).equals(name)) {
Object[] enumConstants = c.getEnumConstants();
t = enumConstants[0];
}
}
} catch (Exception var8) {
var8.printStackTrace();
}
return (E) t;
}
private static tag objectPattern(Class c) {
return (tag)c.getAnnotation(tag.class);
}
private static String className(Class c) {
String str = c.getName();
str = str.substring(str.lastIndexOf(".") + 1);
return str;
}
private static String classNameAndObject(Class c) {
String name = objectPattern(c).name();
return !"".equals(name) ? name : className(c);
}
}
编写执行类
public final class ReturnObject {
private static String packageName;
public ReturnObject() {
}
public static <E> E getObj(String pack, String name) {
packageName = pack;
return (E) object(name);
}
public static <E> E getObj(Class<?> cls, String name) {
// 扫描实现类所在的包名
IfMapper annotation = cls.getAnnotation(IfMapper.class);
packageName = annotation.pack();
return (E) object(name);
}
public static <E> E object(String name) {
return GetObject.getObject(name,packageName);
}
public static <E> E enumObject(String name) {
return GetObject.getEnumObject(name,packageName);
}
}
工具代码基本编写完成,下面开始编写制作蛋糕部分的实现代码
演示代码
蛋糕接口
/**
* @author tanyongpeng
* <p>蛋糕接口</p>
**/
public interface Cake {
/**
* 制作蛋糕
*/
void makeCake();
}
实现类🎂奶油蛋糕
/**
* @author tanyongpeng
* <p>奶油蛋糕</p>
**/
@tag(name = "1")// 自定义标识
public class CreamCake implements Cake {
@Override
public void makeCake() {
System.out.println("正在制作一个奶油蛋糕");
}
}
实现类🎂花生蛋糕
/**
* @author tanyongpeng
* <p>花生蛋糕</p>
**/
@tag(name = "2")
public class PeanutCake implements Cake {
@Override
public void makeCake() {
System.out.println("正在制作一个花生蛋糕");
}
}
实现类🎂臭豆腐蛋糕
tips: 你吃过臭豆腐味的蛋糕吗?
/**
* @author tanyongpeng
* <p>臭豆腐蛋糕</p>
**/
@tag(name = "3")
public class StinkyTofuCake implements Cake {
@Override
public void makeCake() {
System.out.println("正在制作一个臭豆腐蛋糕");
}
}
制作蛋糕
/**
* @author tanyongpeng
* <p>当前code为前端传过来的值,不同的逻辑传不同的值,制作不同的蛋糕</p>
**/
@IfMapper(pack = "com.demo.iftest.inter.impl")
public class Test {
public static void main(String[] args) {
String code = "1";
Cake cake = ReturnObject.getObj(Test.class,code);
cake.makeCake();
}
}
总结
这种方式只是实现的一种思路而已,稍微比较复杂,实际开发中我们可以借助spring的注解帮我们实现策略模式,从而更简单
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/112548.html