飞檐走壁符石,李慕豪个人资料,格言 杂志
设计模式(design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的一块块砖石一样。项目中合理的运用设计模式可以完美的解决很多问题,每种模式在现在中都有相应的原理来与之对应,每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的核心解决方案,这也是它能被广泛应用的原因。本章系java之美[从菜鸟到高手演变]系列之设计模式,我们会以理论与实践相结合的方式来进行本章的学习,希望广大程序爱好者,学好设计模式,做一个优秀的软件工程师!
总体来说设计模式分为三大类:
创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
其实还有两类:并发型模式和线程池模式。
public interface meal { /** * 做饭 */ void cookie(); }
public class noddle implements meal { @override public void cookie() { system.out.println("煮面条"); } }
public class rice implements meal { @override public void cookie() { system.out.println("煮米饭"); } }
public class dumpling implements meal { @override public void cookie() { system.out.println("包饺子"); } }
public class cookiefactory { private static final string noddle = "noddle"; private static final string rice = "rice"; private static final string dumpling = "dumpling"; public meal produce(string meal) { if (stringutils.isnotempty(meal)) { if (meal.equals(noddle)) { return new noddle(); } if (meal.equals(rice)) { return new rice(); } if (meal.equals(dumpling)) { return new dumpling(); } } return null; } public static void main(string[] args) { cookiefactory cookiefactory=new cookiefactory(); meal rice = cookiefactory.produce("rice"); rice.cookie(); } }
public class cookiefactory1 { private static final string noddle = "noddle"; private static final string rice = "rice"; private static final string dumpling = "dumpling"; public static meal producenoddle(string meal) { return new noddle(); } public static meal producerice(){ return new rice(); } public static meal producedumpling(){ return new dumpling(); } public static void main(string[] args) { meal meal = cookiefactory1.producedumpling(); } }
mealfactory接口
public interface mealfactory { meal produce(); }
ricefactory
public class ricefactory implements mealfactory { @override public meal produce() { return new rice(); } }
noddlerfactory
public class noddlefactory implements mealfactory { @override public meal produce() { return new noddle(); } }
这样,当我们后期需要增加功能的时候就可以直接创建就行了,而不需要改变现有的代码。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
java多线程定时器和java判断一个时间是否在时间区间内和用正则表达式获取String字符串之间的数据
网友评论