博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
新手编程入门二:使用“模板模式”减少重复代码
阅读量:7053 次
发布时间:2019-06-28

本文共 4595 字,大约阅读时间需要 15 分钟。

hot3.png

理论就不讲了,网上一搜一大把。直接上栗子。

假设我们有个博客系统,有两个简单的业务:新建博文和上传图片集。代码大概是这样的

public static long newBlog(String title, String content, long userId) throws Exception{		User user = User.ME.loadById(userId);		if(user==null){			throw new Exception("用户不存在");		}		if(User.isFobbiden()){			throw new Exception("用户已被禁止");		}		long id = 0;		try{			if(StringUtils.isBlank(title)){				throw new Exception("博客标题不能为空");			}			if(StringUtils.isBlank(content)){				throw new Exception("博客内容不能为空");			}			id = Blog.ME.save(title, content, user.getId());			List
friends = User.ME.myFriends(user.getId()); UserService.noticeUsers(friends); //通知用户朋友有更新 }catch(Exception e){ throw new Exception(e.getMessage()); } return id; } public static long newPic(String[] picUrls, long userId) throws Exception{ User user = User.ME.loadById(userId); if(user==null){ throw new Exception("用户不存在"); } if(User.isFobbiden()){ throw new Exception("用户已被禁止"); } long id = 0; try{ if(picUrls==null || picUrls.length==0){ throw new Exception("图片地址不能为空"); } id = Pic.ME.save(picUrls, user.getId()); List
friends = User.ME.myFriends(user.getId()); UserService.noticeUsers(friends); //通知用户朋友有更新 }catch(Exception e){ throw new Exception(e.getMessage()); } return id; }

可以很容易的看到,这两个业务方法重复代码太多了!怎么优化呢?

讲讲我在菜鸟时期的优化。首先肯定是把重复代码提取出来,写到一个公有的方法。然后其它方法再调用这个公有方法。这里发现头尾都有重复代码,所以会提取出两个公有方法。

优化后的代码如下:

private static User userAuth(long userId) throws Exception{		User user = User.ME.loadById(userId);		if(user==null){			throw new Exception("用户不存在");		}		if(User.isFobbiden()){			throw new Exception("用户已被禁止");		}		return User;	}		private static void noticeMyFriends(long userId){		List
friends = User.ME.myFriends(userId); UserService.noticeUser(friends); //通知用户朋友有更新 } public static long newBlog(String title, String content, long userId) throws Exception{ User user = userAuth(userId); long id = 0; try{ if(StringUtils.isBlank(title)){ throw new Exception("博客标题不能为空"); } if(StringUtils.isBlank(content)){ throw new Exception("博客内容不能为空"); } id = Blog.ME.save(title, content, user.getId()); noticeMyFriends(user.getId()); //通知用户朋友有更新 }catch(Exception e){ throw new Exception(e.getMessage()); } return id; } public static long newPic(String[] picUrls, long userId) throws Exception{ User user = userAuth(userId); long id = 0; try{ if(picUrls==null || picUrls.length==0){ throw new Exception("图片地址不能为空"); } id = Pic.ME.save(picUrls, user.getId()); noticeMyFriends(user.getId()); }catch(Exception e){ throw new Exception(e.getMessage()); } return id; }

感觉哪里不太对。

没错,这些都是重复代码!但是怎么优化呢?第一个方法好像在这里没法派上用途。

设计模式是时候登场了!模板模式:减少大量重复代码的必备良药!

首先,我们定义一个接口。

public interface ObjHelper{	public long invoke(User user, Object obj);}
然后,定义一个模版方法。基本思路是把差异的代码拿出来,抽象为一个接口,而重复的代码则像是一个模板那样。
private static long newObj(long userId, ObjHelper helper, Object obj){		User user = User.ME.loadById(userId);		if(user==null){			throw new Exception("用户不存在");		}		if(User.isFobbiden()){			throw new Exception("用户已被禁止");		}		long id = 0;		try{			//这里是关键的一步			id = helper.invoke(user, obj);			List
friends = User.ME.myFriends(user.getId()); UserService.noticeUsers(friends); //通知用户朋友有更新 }catch(Exception e){ throw new Exception(e.getMessage()); } return id; }

这时候再写新建博文和上传图片集这两个业务方法,就是这么写的了:实现接口,然后调用模板方法。

public static long newBlog(long userId, String title, String content) throws Exception{		ObjHelper helper = new ObjHelper() {						@Override			public long invoke(User user, Object obj) {				long id = 0;				if(obj==null){					throw new Exception("博客标题不能为空");				}				Blog blog = (Blog)obj;				if(StringUtils.isBlank(blog.getTitle())){					throw new Exception("博客标题不能为空");				}				if(StringUtils.isBlank(blog.getContent())){					throw new Exception("博客内容不能为空");				}				id = Blog.ME.save(blog.getTitle(), blog.getContent(), user.getId());				return id;			}		};		Blog blog = new Blog(title, content);		return newObj(userId, helper, blog);	}		public static long newPic(long userId, List
picUrls) throws Exception{ ObjHelper helper = new ObjHelper() { @Override public long invoke(User user, Object obj) { long id = 0; if(obj==null){ throw new Exception("图片地址不能为空"); } List
picUrls = (List
)obj; if(picUrls.size()==0){ throw new Exception("图片地址不能为空"); } id = Pic.ME.save(picUrls, user.getId()); return id; } }; return newObj(userId, helper, picUrls); }

至此,我们终于消灭重复代码了!

代码大概就是这么写的了,当然最重要的是举一反三,灵活应用。欢迎讨论:-)

转载于:https://my.oschina.net/cevin15/blog/609061

你可能感兴趣的文章
从零开始实现一个简易的Java MVC框架
查看>>
【java解惑】Unicode转义符的使用
查看>>
spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue
查看>>
visio图片导入word和PPT的最清晰的方式
查看>>
DataGuard 环境rman恢复主库坏块一例
查看>>
交换机真的只工作在第二层吗?
查看>>
15年编程生涯,资深架构师总结的7条经验
查看>>
echo命令
查看>>
图形语言 Kgo
查看>>
兄弟连第10节课
查看>>
调整Virtual Box硬盘大小
查看>>
Windows下Apache服务器中自动配置二级子域名
查看>>
Transform Map - Ignore Row if any fields are empty
查看>>
iEclipse-不只是Eclipse的开发者社区
查看>>
Oracle个人的一些记录
查看>>
20.分屏查看命令 less命令
查看>>
感谢付费客户不覺流年似水(271558528) 对C#ASP.NET通用权限管理组件的改进意见,已修正...
查看>>
android 让 TextView 自带滚动条
查看>>
win2003远程桌面不自动注销,自动锁定时间
查看>>
Shell脚本
查看>>