ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:790.17KB ,
资源ID:11397965      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/11397965.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(设计模式笔记(java版).docx)为本站上传会员【仙人****88】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

设计模式笔记(java版).docx

1、设计模式笔记 1、 单例模式 定义: 确保一个类只有一个实例,并提供一个全局访问点。 类图: 源代码: public class Singleton { private static Singleton instance = null; private Singleton(){ } public static Singleton getInstance() { if(instance == null){ synchronized (Singleton.class) { instance = new Singleton();

2、} } return instance; } } 2、 工厂模式 定义:工厂模式定义了一个创建对象的接口,由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。 (1). 简单工厂模式 类图: 源代码: public abstract class Product { public void method1() { } public void method2() { } } public class Product_A extends Product{ public Produ

3、ct_A() { } public void method1() { System.out.println("this is method1 in Product_A"); } public void method2() { System.out.println("this is method2 in Product_A"); } } public class Product_B extends Product{ public Product_B() { } public void method1() { System.out.printl

4、n("this is method1 in Product_B"); } public void method2() { System.out.println("this is method2 in Product_B"); } } public class SimpleFactory { public Product createProduct(String type) { Product product = null; if(type.equals("Product

5、A")){ product = new Product_A(); }else if (type.equals("Product_B")) { product = new Product_B(); } return product; } } (2). 抽象工厂模式 类图: 源代码: public interface Product { public void method1(); public void method2(); } public class Product_A impl

6、ements Product { @Override public void method1() { System.out.println("this is method1 in Product_A"); } @Override public void method2() { System.out.println("this is method2 in Product_A"); } } public class Product_B implements Product { @Override

7、 public void method1() { System.out.println("this is method1 in Product_B"); } @Override public void method2() { System.out.println("this is method2 in Product_B"); } } public interface AbstractFactory { public Product getProduct(); } public class Product_A_Factoty implements Abs

8、tractFactory { @Override public Product getProduct() { return new Product_A(); } } public class Product_B_Factory implements AbstractFactory { @Override public Product getProduct() { return new Product_B(); } } 3、 策略模式 定义:策略模

9、式定义了算法族,分别将其封装起来,让它们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。 类图: 源代码: public abstract class Processor { public Object process(Object obj){ return obj; } } /** * ConcreteProcessor * */ public class Upcase extends Processor{ public String process(Object obj){ return ((String)obj).toUpper

10、Case(); } } /** * ConcreteProcessor * */ public class Downcase extends Processor{ public String process(Object obj){ return ((String)obj).toLowerCase(); } } public class Strategy { public void process(Processor p,Object obj) { System.out.println(p.process(obj)); } }

11、 4、 观察者模式 定义: 观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。 类图: 源代码: public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObserver(); //当主题状态改变时,该方法会被调用,以通知

12、所有的观察者 } public class ConcreteSubject implements Subject { private List observers; private String parameter; public ConcreteSubject() { this.observers = new ArrayList<>(); } @Override public void registerObserver(Observer o) { this.observers.add(o); } @Override pub

13、lic void removeObserver(Observer o) { this.observers.remove(o); } @Override public void notifyObserver() { int i,n; n = this.observers.size(); for(i = 0;i < n;i++){ Observer observer = this.observers.get(i); observer.update(parameter); } }

14、 public void setParameter(String parameter) { this.parameter = parameter; notifyObserver(); } } public interface Observer { public void update(String parameter); } public class ConcreteObserver implements Observer { private String parameter; private Subject

15、subject; public ConcreteObserver(Subject subject) { this.subject = subject; this.subject.registerObserver(this); } @Override public void update(String parameter) { this.parameter = parameter; display(); } public void display() { System.out.println("Current state : " + para

16、meter); } } 5、 装饰者模式 定义: 动态地将责任附加到对象上。若要扩展功能,装饰着提供了比继承更有弹性的替代方法。 类图: 源代码: public abstract class Component { protected String description = "Component"; public String getDescription() { return description; } } public class Co

17、ncreteComponent_A extends Component{ public ConcreteComponent_A() { description = "ConcreteComponent_A"; } public String getDescription() { return description; } } public class ConcreteComponent_B extends Component{ public ConcreteComponent_B() { description = "ConcreteComponent

18、B"; } public String getDescription() { return description; } } public abstract class Decorator extends Component{ public abstract String getDescription(); } public class ConcreteDecorator_A extends Decorator{ Component component;

19、public ConcreteDecorator_A(Component component) { ponent = component; } @Override public String getDescription() { return component.getDescription() + ", ConcreteDecorator_A"; } } public class ConcreteDecorator_B extends Decorator{ Component component; public ConcreteDecorator_B(

20、Component component) { ponent = component; } @Override public String getDescription() { return component.getDescription() + ", ConcreteDecorator_B"; } } 6、 命令模式 定义: 将“请求”封装成对象,以便使用不同的请求、队列或日志来参数化其对象。命令模式也支持可撤销的操作。 类图: 源代码:

21、public interface Command { public void execute(); } /** 起到ConcreteCommand的作用 * */ public class LightOnCommand implements Command { private Light light; public LightOnCommand(Light light) { this.light = light; } @Override public void execute() { light.on(); } } /** 起到Concre

22、teCommand的作用 * */ public class LightOffCommand implements Command { private Light light; public LightOffCommand(Light light) { this.light = light; } @Override public void execute() { light.off(); } } /** 起到Receiver的作用 * */ p

23、ublic class Light { private boolean light; private String name; public Light(String name) { this.name = name; } public void on() { light = true; } public void off() { light = false; } @Override public String toString() { return "Light [name = " + name + ", is = " + li

24、ght + "]"; } } public class Invoker { private Command[] onCommands; private Command[] offCommands; public Invoker() { onCommands = new Command[2]; offCommands = new Command[2]; for(int i = 0;i < 2;i++){ onCommands[i] = null; offCommands[i] = null; } } public void se

25、tCommand(int index,Command on,Command off) { onCommands[index] = on; offCommands[index] = off; } public void onButtonWasPushed(int index) { onCommands[index].execute(); } public void offButtonWasPushed(int index) { offCommands[index].execute(); } @Override public String toS

26、tring() { String str = ""; for(int i = 0;i < onCommands.length;i++){ str += "index "+i+" : "+onCommands[i].getClass().getName()+" "+offCommands[i].getClass().getName()+"\n"; } return str; } }

27、7、 适配器模式 定义: 将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。 类图: 源代码: public interface Target { public void request(); } public class RealTarget implements Target { @Override public void request() { System.out.println("this is real do."); } } public class Adaptee { public void doi

28、ng() { System.out.println("this is Adaptee_do."); } } public class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void request() { adaptee.doing(); } }

29、 8、 外观模式 定义: 提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。 类图: 9、 模板方法模式 定义:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。 类图: 源代码: public abstract class AbstractClass { final void templateMethod(){ primitiveOperation1(); primitiveOperation2(); } a

30、bstract void primitiveOperation1(); abstract void primitiveOperation2(); } public class ConcreteClass_A extends AbstractClass{ @Override void primitiveOperation1() { System.out.println("this is primitiveOperation1 in ConcreteClass_A"); } @Override void primitiveOperation2() { Sys

31、tem.out.println("this is primitiveOperation2 in ConcreteClass_A"); } } public class ConcreteClass_B extends AbstractClass{ @Override void primitiveOperation1() { System.out.println("this is primitiveOperation1 in ConcreteClass_B"); } @Override void primitiveOperation2() { System.

32、out.println("this is primitiveOperation2 in ConcreteClass_B"); } } 10、 迭代器模式 定义: 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。 作用: 迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露其内部的表示。将游走的任务放在迭代器上,而不是聚合上,这样简化了聚合的接口和实现,也让责任各得其所。 类图: 源代码: public inte

33、rface Aggregate { public Iterator createIterator(); //我们使用java.util.Iterator作为Iterator类 } public class ConcreteAggregate implements Aggregate { List list; public ConcreteAggregate() { this.list = new ArrayList(); init(); } public void init() { addItem("first"); addItem("secon

34、d"); addItem("third"); } public void addItem(String parameter) { list.add(parameter); } @Override public Iterator createIterator() { return list.iterator(); } } 11、 组合模式 定义: 允许将对象组合成树形结构来表现“整体/部分”层次结构。 作用: 组合能让客户以一致的方式处理个别对象以及对

35、象组合。组合模式让我们能用树形方式创建对象的结构,树中包含了组合以及个别的对象。使用组合结构,我们能把相同的操作应用在组合和个别对象上。换句话说,在大多数情况下,我们可以忽略对象组合和个别对象之间的差异。 类图: 源代码: public abstract class Component { public void add(Component component) { throw new UnsupportedOperationException(); } public void remove(Component component) { throw new U

36、nsupportedOperationException(); } public Component getChild(int i) { throw new UnsupportedOperationException(); } public void operation() { } } public class Leaf extends Component { String description; public Leaf(String description) { this.description = description; } publ

37、ic void operation() { System.out.println("this is Leaf " + description + " operation."); } } public class Composite extends Component{ List components; String description; public Composite(String description) { components = new Array

38、List<>(); this.description = description; } public void add(Component component) { components.add(component); } public void remove(Component component) { components.remove(component); } public Component getChild(int i) { return components.get(i); } public void operation()

39、 { System.out.println("this is Composite " + description + " operation."); } } 12、 状态模式 定义: 允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。 类图: 源代码: public interface State { public void handle(); } public class OpenState implements State { @Overrid

40、e public void handle() { System.out.println("It's opening!"); } } public class CloseState implements State { @Override public void handle() { System.out.println("It's closed!"); } } public class Context { private State openState; private State closeState; public Context() {

41、 openState = new OpenState(); closeState = new CloseState(); } public void open() { openState.handle(); } public void close() { closeState.handle(); } } 13、 代理模式 定义: 为另一个对象提供一个替身或占位符以控制对这个对象的访问。 作用: 使用代理模式创建代表(

42、representative)对象,让代表对象控制某对象的访问,被代理的对象可以是远程的对象、创建开销大的对象或需要安全控制的对象。 类图: 源代码: public interface Subject { public void request(); } class RealSubject implements Subject { @Override public void request() { System.out.println("this is request in RealSubject."); } } public class Proxy implements Subject { Subject subject; public Proxy() { subject = new RealSubject(); } @Override public void request() { subject.request(); } } 具体源码及类图详见

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服