Creational模式
来源:互联网 发布:淘宝网包邮服务 编辑:程序博客网 时间:2024/06/05 23:01
Creational模式
设计模式就是解决特定问题使用特定的架构,从而让程序可重用、可扩展、可维护。
今天看到一个个人网站,觉得对这部分讲述的非常好,能讲述清除特定的设计模式应该用在什么地方,所以决定做个小总结。
构造形设计模式,用于解决如何创建对象,并向客户隐藏创建对象和对象依赖的细节。
注意:UML中的接口,指的是符合类的约定的类型(直白的说就是要有约定的公开方法),而不是特指Java语言的interface类型。试想,Python等动态类型的语言,并没有接口类型的。
Simple Factory模式
简单工厂模式,也称静态工厂模式。如果某个对象创建并建立依赖关系的代码比较复杂时,可以使用该模式隐藏创建对象的细节。
public class MessageFactory { public static Message getMessage(Session session) { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(session.from)); msg.setSubject(session.subject); msg.setText(session.text); msg.setHeader(session.headerName, session.headerValue); msg.setDate(session.date); ... return msg; } }
Abstract Factory模式
当需要一组随时可抽换的元件件,并且可以随时批量更换实现,就使用抽象工厂模式。
interface PointCornerFactory { Point getPoint(); Corner getCorner();}interface Point { void line(int width);}interface Corner { void leftUp(); void rightUp(); void leftDown(); void rightDown();}class Rectangle { private int width; private int height; Rectangle(int width, int height) { this.width = width; this.height = height; } void paint(PointCornerFactory factory) { Point point = factory.getPoint(); Corner corner = factory.getCorner(); corner.leftUp(); point.line(width - 2); corner.rightUp(); System.out.println(); for(int i = 0; i < height - 2; i++) { point.line(width); System.out.println(); } corner.leftDown(); point.line(width - 2); corner.rightDown(); System.out.println(); }}//依您所提供的PointCornerFactory、Point與Corner實作之不同,可以繪製出不同外觀的矩形,例 如:public class Main { public static void main(String[] args) { Rectangle rect = new Rectangle(20, 10); PointCornerFactory factory = new PointCornerFactory() { public Point getPoint() { return new Point() { public void line(int width) { for(int i = 0; i < width; i++) { System.out.print("-"); } } }; } public Corner getCorner() { return new Corner() { public void leftUp() { System.out.print('+'); } public void rightUp() { System.out.print('+'); } public void leftDown() { System.out.print('+'); } public void rightDown() { System.out.print('+'); } }; } }; rect.paint(factory); }}
如果希望同时更换Point、Corner类型,只需要更换PointCornerFactory实现即可!
Factory Method模式
抽象工厂模式,在一个抽象的类别中,依赖于一个组件的接口,而不是组件的实现,并且在该类中提供一个创建该组件接口的抽象方法。这样的好处是,在实现组件之前,基于组件的接口,完成其他工作。组件的实现则推迟至实现该抽象方法的子类中完成。
工厂方法和策略模式很像,其实可以在一个具体的类型中依赖策略接口,完成相似的工作。区别是,工厂方法模式隐藏了创建对象的细节,在方法中创建对象。而策略模式则是客户先创建好策略接口的实例,需要客户自己构造对象。
所以理解策略模式,一定要理解当前策略要解决的问题。这样子,疑惑就一扫而清了。
import java.util.*;abstract class Document { private String title; String getTitle() { return title; } void setTitle(String title) { this.title = title; } abstract void open(); abstract void save(); abstract void close();}abstract class Editor { private List<Document> docs = new ArrayList<Document>(); void open(String file) { Document doc = createDocument(); doc.setTitle(file); doc.open(); docs.add(doc); } void save(Document doc) { doc.save(); } void close(Document doc) { doc.close(); docs.remove(doc); } void close() { for(Document doc : docs) { close(doc); } } // ... 其它的方法定義 abstract Document createDocument(); // Factory method}
Document定义是 interface 或 abstract class 无关紧要,重要的是Editor中所有操作流程都依赖于Document中定义的公开方法,实际上如果构造具体的Document对象,则由子类完成:
class TextEditor extends Editor { Document createDocument() { return new Document() { void open() { System.out.println("開啟文字檔案 " + this.getTitle()); } void save() { System.out.println("儲存文字檔案 " + this.getTitle()); } void close() { System.out.println("關閉文字檔案 " + this.getTitle()); } }; }}public class Main { public static void main(String[] args) { Editor editor = new TextEditor(); editor.open("Main.java"); editor.open("Editor.java"); editor.close(); }}
网上有些文章介绍抽象工厂模式,抽象类中只有一个创建对象的方法,而没有其他依赖于创建的接口的操作,显然没有理解抽象方法模式的意图。如果只需要创建对象的方法,何不采用简单工厂模式呢?
剩下的Creational模式稍后更新,以上内容出自:
https://openhome.cc/Gossip/DesignPattern/index.html
- Creational模式
- Creational模式之Singleton模式
- Creational模式之Builder模式
- Creational模式之Prototype模式
- C++实现Creational - Singleton模式
- C++实现Creational - Singleton模式
- C++实现Creational - Builder模式
- C++实现Creational - Prototype模式
- C++实现Creational - Singleton模式
- C++实现Creational - Singleton模式
- 设计模式笔记(1)-Creational 模式
- Creational模式之Factory Method模式
- Creational模式之Abstract Factory模式
- 《设计模式》之Creational模式:简介
- 《设计模式》之Creational模式:Abstract Factory
- 《设计模式》之Creational模式:Builder
- C++实现Creational - Simple Factory模式
- C++实现Creational - Factory Method模式
- 花椒斥资1亿打造“1218直播节”, 携手北京卫视成就首个上星直播盛典
- python pip安装第三方包速度很慢
- Unity3D
- Docker
- 3.Java变量初始化顺序
- Creational模式
- “戏”说spark---spark 内存管理详解
- ubuntu16.04 sudo:无法解析主机 解决方案
- 操作系统--硬件与软件的桥梁
- 电子科技大学第九届ACM趣味程序设计竞赛第二场(正式赛) 题解
- 算法提高 三进制数位和
- LintCode刷题记录入门题汇总
- C++学习笔记--类对象、继承、多态的内存模型
- Big Data Developer(Day01)