一、设计模式的分类
总体来说设计模式分为三大类:
创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访
问者模式、中介者模式、解释器模式。
1.工厂模式
1).普通工厂模式
public interface Sender{ public void Send(); } public class MailSender implements Sender{ @Override public void Send() { // TODO Auto-generated method stub System.out.println("我是Main"); } } public class SmsSender implements Sender{ @Override public void Send() { // TODO Auto-generated method stub System.out.println("我是Sms"); } }
public class Fractory { public Sender product(String type) { // TODO Auto-generated method stub if("mail".equals(type)){ return new MailSender(); }else if("sms".contentEquals(type)){ return new SmsSender(); }else { System.out.println("输入正确的类型"); return null; } } } public static void main(String[] args){ Fractory factory=new Fractory(); Sender sender=factory.product("mail"); sender.Send(); } }
2).多个工厂方法模式
public class SendFactory{ public Sender produceMail(){ return new MailSender(); } public Sender produceSms(){ return new SmsSender(); } } } public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); Sender sender = factory.produceMail(); sender.Send(); } }
3).静态工厂方法模式
public class SendFactory { public static Sender produceMail(){ return new MailSender(); } public static Sender produceSms(){ return new SmsSender(); } } public class FactoryTest { public static void main(String[] args) { Sender sender = SendFactory.produceMail(); sender.Send(); }
2.单例模式
1).普通单例模式
public class Singleton { //普通单例模式 // 主要优点: // 1、提供了对唯一实例的受控访问。 // 2、由于在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁创建和销毁的对象单例模式无疑可以提高系统的性能。 // 3、允许可变数目的实例。 // // 主要缺点: // 1、由于单利模式中没有抽象层,因此单例类的扩展有很大的困难。 // 2、单例类的职责过重,在一定程度上违背了“单一职责原则”。 // 3、滥用单例将带来一些负面问题,如为了节省资源将数据库连接池对象设计为的单例类,可能会导致共享连接池对象的程序过多而出现连接池溢出;如果实例化的对象长时间不被利用,系统会认为是垃圾而被回收,这将导致对象状态的丢失。 private static Singleton instance=null; private Singleton(){ } //线程安全 public static Singleton Instance(){ if(instance ==null){ synchronized (instance) { if(instance==null){ instance = new Singleton(); } } } return instance; } public Object readResolve(){ return instance; } }
2).饿汉模式
public class Hungrymode { //饿汉模式 private static Hungrymode singleton = new Hungrymode(); private Hungrymode(){ } public static Hungrymode getInstance(){ return singleton; } }
3).饱汉模式
public class Fullmode { //饱汉模式 锁 private static Fullmode instance=null; private Fullmode(){ } //线程安全 public static Fullmode Instance(){ if(instance ==null){ synchronized (instance) { if(instance==null){ instance = new Fullmode(); } } } return instance; } }
3.代理模式
public interface Sourceable { public void method(); } public class Source implements Sourceable { @Override public void method() { // TODO Auto-generated method stub System.err.println("我是中心"); } } public class Proxy implements Sourceable { private Source source; public Proxy() { // TODO Auto-generated constructor stub //super(); this.source=new Source(); } @Override public void method() { // TODO Auto-generated method stub // System.err.println("为什么"); after(); source.method(); before(); } public void after(){ System.out.println("在什么之后"); } public void before(){ System.out.println("在什么之前"); } } public class ProxyText { public static void main(String[] args){ Sourceable source=new Proxy(); source.method(); } }