您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > Java中23种设计模式(个人整理实用)
Java设计模式Java设计模式()1目录1.设计模式........................................................................................................................................21.1创建型模式..........................................................................................................................21.1.1工厂方法...................................................................................................................21.1.2抽象工厂...................................................................................................................41.1.3建造者模式...............................................................................................................81.1.4单态模式.................................................................................................................111.1.5原型模式.................................................................................................................131.2结构型模式........................................................................................................................151.2.1适配器模式.............................................................................................................151.2.2桥接模式.................................................................................................................171.2.3组合模式.................................................................................................................201.2.4装饰模式.................................................................................................................241.2.5外观模式.................................................................................................................261.2.6享元模式.................................................................................................................301.2.7代理模式.................................................................................................................321.3行为型模式........................................................................................................................351.3.1责任链模式.............................................................................................................351.3.2命令模式.................................................................................................................381.3.3解释器模式.............................................................................................................411.3.4迭代器模式.............................................................................................................431.3.5中介者模式.............................................................................................................471.3.6备忘录模式.............................................................................................................501.3.7观察者模式.............................................................................................................521.3.8状态模式.................................................................................................................561.3.9策略模式.................................................................................................................591.3.10模板方法...............................................................................................................611.3.11访问者模式...........................................................................................................63Java设计模式()21.设计模式(超级详细)1.1创建型模式AbstractFactory(抽象工厂)FactoryMethod(工厂方法)Singleton(单态模式)Builder(建造者模式)Protot*pe*原型模式)1.1.1工厂方法*义一个用于创建对象的接口,让子类决定实例化哪一个类。FactoryMethod使一个类的实例*延迟到其子类。适用性1.当一个类不知道它所必须创建的对象的类的时候。2.当一个类希望由它的子类来指定它所创建的对象的时候。3.当*将创建对象的职责委托给多个帮助*类中的某一个,并且*希望将哪一个帮助子类是代理者这一信息局部化的时候。参与者1.Product定义工厂方法所创建的对象的接口。2.ConcreteProduct实现Product接口。3.Creator声明工厂方法,该方法返回一个Product类型的对象*Creator也可以定义一个工厂方法的缺省实现,它返回一个缺省的ConcreteProduct对象。可以调用工厂方法以创建一个Product对象。4.ConcreteCreator重定义工厂方法以返回一个ConcreteProduct实例。Java设计模式()3类图例子*roductpublicinterfaceWork{voiddoWork();}ConcreteProductpublicclassStudentWorkimplementsWork{publicvoiddoWork(){System.out.println(学生*作业!);}}publicclassTeacherWorkimplementsWork{publicvoiddoWork(){System.out.println(老师审批作业!);}}CreatorpublicinterfaceIWorkFactory{Workget*ork();}Concre*eCreatorpu*licclassStudentWorkFactoryimplementsIWorkFactory{Java设计模式()4publicWorkgetWork(){*eturnnewStudentWork();}}publicclassTeacherWorkFactoryimplementsIWorkFactory{publicWorkgetWork(){returnnewTeacherWork();}}TestpublicclassTest{publicstaticvoidm*in(Strin*[]args){IWorkFactorystudentWorkFactory=newStudentWorkFactory();studentWorkFactory.getWork().d*Work();IWorkFactoryteacherWorkFactory*newTeacherWorkFactory();teacherWorkFactory.g*tWork().*oWork();}}result学生做作业!老师审批作业!1.1.2抽象工厂提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。适用性1.一个系统要独立于它的*品的创建、组合和表示时。2.一个系统要由多个产品系列中的一个来配置时。3.当你要强调一系列相关的产品对象的设计以便进行联合使用时*Java设计模式()54*当你提供一个产品类库,而只想显示它们*接口而不是实现时。参与者1.Ab*tractFactory声明一个创建抽象产品对象的操作接口。2.ConcreteFactory实现创建具体产品对象的操作。*.AbstractProduct为一类产品对象声明一个接口。4.ConcreteProdu*t定义一个将被相应的具体工厂创建的产品*象。实现*bstractProduct接口。5.Client仅使用由AbstractF
本文标题:Java中23种设计模式(个人整理实用)
链接地址:https://www.777doc.com/doc-6148756 .html