1、总体来说设计模式分为三大类:
Java 中一般认为有 23 种设计模式,我们不需要所有的都会,但是其中常用的几种设计模式应该去掌握。下面列出了所有的设计模式。需要掌握的设计模式已单独列出来了,当然能掌握的越多越好。
创建型模式(5种):单例模式、工厂模式(包含:工厂方法模式和抽象工厂模式)、建造者模式、原型模式。
结构型模式(7种):适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式(11种):策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
2、单例模式
2.1饿汉式单例
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//饿汉式 public class Singleton1 { //直接创建对象 public static Singleton1 instance=new Singleton1(); //私有化构造方法 private Singleton1(){} //返回对象实例 public static Singleton1 getInstance(){ return instance; } }
2.2懒汉式单例
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//懒汉式: public class Singleton2 { // 声明变量 private static volatile Singleton2 singleton = null; // 私有构造函数 private Singleton2() { } // 提供对外方法 public static Singleton2 getInstance() { if (singleton == null) { synchronized (Singleton2.class) { if (singleton == null) { singleton = new Singleton2(); } } } return singleton; } public static void main(String[] args){ System.out.println(getInstance()); } }
3.工厂模式分为:工厂方法模式和抽象工厂模式
3.1)工厂方法模式分为三种:
1、普通工厂模式:就是建立一个工厂类, 对实现了同一接口的一些类进行实例的创建。示列:SendFactory
a)同一接口类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public interface Sender { public void send(); }
b1)同一接口的实现类MailSender:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class MailSender implements Sender { public void send() { System.out.println("this is mail sender!"); } }
b2)同一接口的实现类SmsSender:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class SmsSender implements Sender { public void send() { System.out.println("this is Sms sender!"); } }
c)普通工厂模式类SendFactory
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//普通工厂模式 public class SendFactory { public Sender produce(String type){ if("mail".equals(type)){ return new MailSender(); }else if ("sms".equals(type)) { return new SmsSender(); }else { System.out.println("请输入正确的类型!"); return null; } } }
d)测试类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class FacteryTest { public static void main(String[] args) { SendFactory sendFactory=new SendFactory(); sendFactory.produce("mail").send(); sendFactory.produce("sms").send(); } }
2、多个工厂方法模式:是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。
a)多个工厂方法模式类SendFactory2
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//多个工厂方法模式: public class SendFactory2 { public Sender produceMail(){ return new MailSender(); } public Sender produceSms(){ return new SmsSender(); } }
b)测试类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class FacteryTest { public static void main(String[] args) { SendFactory2 factory = new SendFactory2(); Sender sender2 = factory.produceMail(); sender2.send(); } }
3、静态工厂方法模式,将上面的多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。
a)静态工厂方法模式类SendFactory3
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//静态工厂方法模式 public class SendFactory3 { public static Sender produceMail(){ return new MailSender(); } public static Sender produceSms(){ return new SmsSender(); } }
b)测试类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class FacteryTest { public static void main(String[] args) { Sender sender3=SendFactory3.produceSms(); sender3.send(); } }
3.2)抽象工厂模式
工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,这违背了闭包原则,所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码。
a)接口Provider
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public interface Provider { public Sender produce(); }
b)接口Sender
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public interface Sender { public void send(); }
c)功能类MailSender,实现了Sender接口
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class MailSender implements Sender { public void send() { System.out.println("this is mail sender!"); } }
d)功能类SmsSender,实现了Sender接口
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class SmsSender implements Sender { public void send() { System.out.println("this is sms sender!"); } }
e)工厂类SendMailFactory,实现了Provider接口
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class SendMailFactory implements Provider { public Sender produce() { return new MailSender(); } }
f)工厂类SendSmsFactory,实现了Provider接口
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class SendSmsFactory implements Provider { public Sender produce() { return new SmsSender(); } }
g)测试类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class Test { public static void main(String[] args) { Provider provider=new SendMailFactory(); Sender sender=provider.produce(); sender.send(); } }