设计模式(Design pattern):优秀经验的总结
1:设计模式分类:
1.1 创建型模式;
对象的创建,主要包括:简单工厂模式;工厂方法模式;抽象工厂模式;建造者模式;原型模式;单例模式
1.1.1 简单工厂模式
又叫静态工厂方法模式,定义一个专门创建对象的工厂类.
优点:明确了类的职责,客户端不需要在负责对象的创建
缺点:静态工厂类负责所有对象的创建,如果有新的对象增加,需要不断修改工厂类,不利于维护.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public abstract class Animal { public abstract void eat(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class Dog extends Animal { @Override public void eat() { System.out.println("狗吃肉!"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class Cat extends Animal { @Override public void eat() { System.out.println("猫吃鱼!"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class AnimalFactory { public static Animal createAnimal(String animalType) { if ("dog" == animalType) { return new Dog(); } else if ("cat" == animalType) { return new Cat(); } //缺点:如果增加其他动物的话,需要不断修改该工具类 return null; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class SimpleFactoryTest { public static void main(String[] args) { // 1.0 普通调用 Dog d = new Dog(); d.eat(); /*输出结果 *狗吃肉! *? */ //2.0 创建简单工厂类,用于对象的创建 Animal c = AnimalFactory.createAnimal("cat"); if (d!=null) { c.eat(); } /*输出结果 *猫吃鱼! *? */ } }
1.1.2 工厂方法模式
抽象工厂类负责定义创建对象的接口,具体对象的创建工作有继承抽象工厂类的具体类实现
优点:与 简单工厂类相比,如果有新的对象增加,只需要增加一个具体的类和具体的工厂类即可,扩展性强
缺点:增加代码量
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public abstract class Animal { public abstract void eat(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class Dog extends Animal { @Override public void eat() { System.out.println("狗吃肉!"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class Cat extends Animal { @Override public void eat() { System.out.println("猫吃鱼!"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public interface Factory { public abstract Animal createAnimal(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class DogFactory implements Factory { @Override public Animal createAnimal() { return new Dog(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public interface FactoryMethodTest { public static void main(String[] args) { Factory f = new DogFactory(); Animal a = f.createAnimal(); a.eat(); } }
1.1.3 单例模式
确保类在内存中只有一个对象,该类必须自动实现,并且对外提供
优点:在内存中只有一个对象
缺点:扩展性差
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; //01 构造方法私有化 //02 自己创建对象 //03 对外界提供 public class Student { //单利模式--饿汉式 private Student() {} //02 自己创建 ,为了让外界直接访问加static,为了让外界不能修改添加private private static Student s = new Student(); //03 对外界提供 public static Student getStudent() { return s; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; //01 构造方法私有化 //02 自己创建对象 //03 对外界提供 public class Student { //单利模式--懒汉式 private Student() {} //02 自己创建 ,为了让外界直接访问加static,为了让外界不能修改添加private private static Student s = null; //03 对外界提供 public static Student getStudent() { if (s==null) { s = new Student(); } return s; } }
懒汉式存在线程安全问题
解决方法,添加同步
1.2 结构型模式;
对象的组成结构,主要包括:外观模式;适配器模式;代理模式;装饰模式;桥接模式;组合模式;享元模式
1.2.1 适配器模式
两个不兼容的接口之间的桥梁.
优点:灵活性好
缺点:过多地使用适配器,会让系统非常零乱,不易整体进行把握
场景:接口(方法比较多) == 适配器(实现接口,仅仅是空实现)==实现类(实现其中的某个或部分方法)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public interface UserDao { void add(); void delete(); void update(); void select(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public abstract class UserAdapter implements UserDao { @Override public void add() { } @Override public void delete() { } @Override public void update() { } @Override public void select() { } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class UserDaoImpl extends UserAdapter { @Override public void add() { // TODO Auto-generated method stub System.out.println("添加方法!"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class UserDaoTest { public static void main(String[] args) { // TODO Auto-generated method stub UserDao ud = new UserDaoImpl(); ud.add(); } }
1.2.2 装饰模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public abstract class Phone { //手机类 的子类中都有Call方法 public abstract void call(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class Huawei extends Phone { @Override public void call() { System.out.println("华为手机:可以打电话"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public abstract class PhoneDecorate extends Phone { private Phone p; public PhoneDecorate(Phone p) { this.p = p; } @Override public void call() { p.call(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class RingDecorate extends PhoneDecorate { public RingDecorate(Phone p) { super(p); // TODO Auto-generated constructor stub } @Override public void call() { System.out.println("彩铃装饰"); super.call(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class MusicDecorate extends PhoneDecorate { public MusicDecorate(Phone p) { super(p); } @Override public void call() { super.call(); System.out.println("音乐装饰"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; import java.io.BufferedReader; import java.io.InputStreamReader; public class PhoneTest { public static void main(String[] args) { // TODO Auto-generated method stub Huawei hw = new Huawei(); PhoneDecorate rd = new RingDecorate(hw); rd.call(); /*输出结果 彩铃装饰 华为手机:可以打电话 */ //同理 添加听音乐装饰 PhoneDecorate md = new MusicDecorate(hw); md.call(); /*输出结果 华为手机:可以打电话 音乐装饰 */ //注意 同时装饰两个 彩铃+音乐 //new MusicDecorate(hw) 继承自PhoneDecorate 继承自Phone PhoneDecorate mwd = new RingDecorate(new MusicDecorate(hw)); mwd.call(); /*输出结果 彩铃装饰 华为手机:可以打电话 音乐装饰 */ //这种用法 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); } }
1.3 行为型模式
对象的行为,主要包括:模板设计模式;观察者模式;状态模式;职责链模式;命令模式;访问者模式;策略模式;备忘录模式;迭代器模式;解释器模式
1.3.1 模板设计模式
定义一个算法的骨架,而将具体的算法延迟到子类中来实现
优点:灵活多改变
缺点:如果算法骨架有修改的话,需要修改抽象类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public abstract class GetTime { public long getTime() { long startTime = System.currentTimeMillis(); code(); long endTime = System.currentTimeMillis(); return endTime-startTime; } public abstract void code(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; import java.util.Iterator; public class GetTimeDemo extends GetTime { @Override public void code() { for (int i = 0; i < 100000; i++) { System.out.println(i); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package d12; public class GetTimeTest { public static void main(String[] args) { GetTime d =new GetTimeDemo(); System.out.println("用时: "+d.getTime()+"毫秒"); } }