已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
设计模式笔记1、 单例模式定义:确保一个类只有一个实例,并提供一个全局访问点。类图:源代码:public class Singleton private static Singleton instance = null;private Singleton()public static Singleton getInstance() if(instance = null)synchronized (Singleton.class) instance = new Singleton();return instance;2、 工厂模式定义:工厂模式定义了一个创建对象的接口,由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。(1). 简单工厂模式类图:源代码:public abstract class Product public void method1() public void method2() public class Product_A extends Productpublic Product_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 Productpublic Product_B() public void method1() System.out.println(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_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 implements Product Overridepublic void method1() System.out.println(this is method1 in Product_A);Overridepublic void method2() System.out.println(this is method2 in Product_A);public class Product_B implements Product Overridepublic void method1() System.out.println(this is method1 in Product_B);Overridepublic void method2() System.out.println(this is method2 in Product_B);public interface AbstractFactory public Product getProduct();public class Product_A_Factoty implements AbstractFactory Overridepublic Product getProduct() return new Product_A();public class Product_B_Factory implements AbstractFactory Overridepublic Product getProduct() return new Product_B();3、 策略模式定义:策略模式定义了算法族,分别将其封装起来,让它们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。类图:源代码:public abstract class Processor public Object process(Object obj)return obj; /* * ConcreteProcessor * */public class Upcase extends Processorpublic String process(Object obj)return (String)obj).toUpperCase(); /* * ConcreteProcessor * */public class Downcase extends Processorpublic String process(Object obj)return (String)obj).toLowerCase(); public class Strategy public void process(Processor p,Object obj) System.out.println(cess(obj);4、 观察者模式定义:观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。类图:源代码:public interface Subject public void registerObserver(Observer o);public void removeObserver(Observer o);public void notifyObserver(); /当主题状态改变时,该方法会被调用,以通知所有的观察者public class ConcreteSubject implements Subject private List observers;private String parameter;public ConcreteSubject() this.observers = new ArrayList();Overridepublic void registerObserver(Observer o) this.observers.add(o);Overridepublic void removeObserver(Observer o) this.observers.remove(o);Overridepublic void notifyObserver() int i,n;n = this.observers.size();for(i = 0;i n;i+)Observer observer = this.observers.get(i);observer.update(parameter);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 subject;public ConcreteObserver(Subject subject) this.subject = subject;this.subject.registerObserver(this);Overridepublic void update(String parameter) this.parameter = parameter;display();public void display() System.out.println(Current state : + parameter);5、 装饰者模式定义:动态地将责任附加到对象上。若要扩展功能,装饰着提供了比继承更有弹性的替代方法。类图:源代码:public abstract class Component protected String description = Component;public String getDescription() return description;public class ConcreteComponent_A extends Componentpublic ConcreteComponent_A() description = ConcreteComponent_A;public String getDescription() return description;public class ConcreteComponent_B extends Componentpublic ConcreteComponent_B() description = ConcreteComponent_B;public String getDescription() return description;public abstract class Decorator extends Componentpublic abstract String getDescription();public class ConcreteDecorator_A extends DecoratorComponent component;public ConcreteDecorator_A(Component component) ponent = component;Overridepublic String getDescription() return component.getDescription() + , ConcreteDecorator_A;public class ConcreteDecorator_B extends DecoratorComponent component;public ConcreteDecorator_B(Component component) ponent = component;Overridepublic String getDescription() return component.getDescription() + , ConcreteDecorator_B;6、 命令模式定义:将“请求”封装成对象,以便使用不同的请求、队列或日志来参数化其对象。命令模式也支持可撤销的操作。类图:源代码:public interface Command public void execute();/* 起到ConcreteCommand的作用 * */public class LightOnCommand implements Command private Light light;public LightOnCommand(Light light) this.light = light;Overridepublic void execute() light.on();/* 起到ConcreteCommand的作用 * */public class LightOffCommand implements Command private Light light;public LightOffCommand(Light light) this.light = light;Overridepublic void execute() light.off();/* 起到Receiver的作用 * */public class Light private boolean light;private String name;public Light(String name) = name;public void on() light = true;public void off() light = false;Overridepublic String toString() return Light name = + name + , is = + light + ;public class Invoker private Command onCommands;private Command offCommands;public Invoker() onCommands = new Command2;offCommands = new Command2;for(int i = 0;i 2;i+)onCommandsi = null;offCommandsi = null;public void setCommand(int index,Command on,Command off) onCommandsindex = on;offCommandsindex = off;public void onButtonWasPushed(int index) onCommandsindex.execute();public void offButtonWasPushed(int index) offCommandsindex.execute();Overridepublic String toString() String str = ;for(int i = 0;i onCommands.length;i+)str += index +i+ : +onCommandsi.getClass().getName()+ +offCommandsi.getClass().getName()+n;return str;7、 适配器模式定义:将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。类图:源代码:public interface Target public void request();public class RealTarget implements Target Overridepublic void request() System.out.println(this is real do.);public class Adaptee public void doing() System.out.println(this is Adaptee_do.);public class Adapter implements Target private Adaptee adaptee;public Adapter(Adaptee adaptee) this.adaptee = adaptee;Overridepublic void request() adaptee.doing();8、 外观模式定义:提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。类图:9、 模板方法模式定义:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。类图:源代码:public abstract class AbstractClass final void templateMethod()primitiveOperation1();primitiveOperation2();abstract void primitiveOperation1();abstract void primitiveOperation2();public class ConcreteClass_A extends AbstractClassOverridevoid primitiveOperation1() System.out.println(this is primitiveOperation1 in ConcreteClass_A);Overridevoid primitiveOperation2() System.out.println(this is primitiveOperation2 in ConcreteClass_A);public class ConcreteClass_B extends AbstractClassOverridevoid primitiveOperation1() System.out.println(this is primitiveOperation1 in ConcreteClass_B);Overridevoid primitiveOperation2() System.out.println(this is primitiveOperation2 in ConcreteClass_B);10、 迭代器模式定义:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。作用:迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露其内部的表示。将游走的任务放在迭代器上,而不是聚合上,这样简化了聚合的接口和实现,也让责任各得其所。类图:源代码:public interface 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(second);addItem(third);public void addItem(String parameter) list.add(parameter);Overridepublic Iterator createIterator() return list.iterator();11、 组合模式定义:允许将对象组合成树形结构来表现“整体/部分”层次结构。作用:组合能让客户以一致的方式处理个别对象以及对象组合。组合模式让我们能用树形方式创建对象的结构,树中包含了组合以及个别的对象。使用组合结构,我们能把相同的操作应用在组合和个别对象上。换句话说,在大多数情况下,我们可以忽略对象组合和个别对象之间的差异。类图:源代码:public abstract class Component public void add(Component component) throw new UnsupportedOperationException();public void remove(Component component) throw new UnsupportedOperationException();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;public void operation() System.out.println(this is Leaf + description + operation.);public class Composite extends ComponentList components;String description;public Composite(String description) components = new ArrayList();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() System.out.println(this is Composite + description + operation.);12、 状态模式定义:允许对象在内部状态改变时
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026事业单位工勤技能-天津-天津保育员五级(初级工)历年参考题库含答案详解
- 2026事业单位工勤技能-四川-四川图书资料员四级(中级工)历年参考题库含答案详解
- 2026事业单位工勤技能-吉林-吉林家禽饲养员四级(中级工)历年参考题库含答案详解
- 2026事业单位工勤技能-内蒙古-内蒙古食品检验工二级(技师)历年参考题库含答案详解
- 2026事业单位工勤技能-内蒙古-内蒙古保健按摩师五级(初级工)历年参考题库含答案详解
- 2026事业单位工勤技能-云南-云南下水道养护工三级(高级工)历年参考题库含答案详解
- 2026事业单位工勤技能-上海-上海公路养护工二级(技师)历年参考题库含答案详解
- -七年级上学期期末考试试题英语-无答案
- 2026年濉溪县网格员招聘笔试备考题库及答案解析
- 2026年曲周县中小学幼儿园教师招聘笔试参考题库及答案解析
- 2026 年秋季开学 传承红色基因少年勇担使命
- 1.1 从原始社会到奴隶社会 课件 2026-2027学年统编版九年级历史上册
- 销售目标任务责任书
- 护理实习中的心理调适
- 高校新校区建设项目水资源论证报告书
- 2026年二级建造师《市政实务》考试真题及答案解析
- “化危为安”线上讲堂第153期-用好重大隐患判定准则 准确排查整治风险隐患-程长进
- 回复供应商询价的回复函3篇范文
- 福建省2025年中国奥林匹克竞赛预赛化学试题 (无答案)
- 气切病人脱机训练
- 高空作业平台倾覆现场处置方案
评论
0/150
提交评论