[Design Patterns] 4. Creation Pattern

设计模式是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结,使用设计模式的目的是提高代码的可重用性,让代码更容易被他人理解,并保证代码可靠性。它是代码编制真正实现工程化。

四个关键元素:(1) Pattern Name, (2) Problem, (3) Solution, (4) Consequences.


01. Factory Method Pattern

/* The product should be created by his own factory. */

LogFactory factory = new FileLogFactory();
Log log = factory.createLog();

log.writeLog();

任务的分配:

架构师1:定义factory, product接口;

程序员1:完成FileLogFactory类,FileProduct类;

问题:只定义产品,又如何?

答:factory类中可以添加如系统版本,环境等不适合由Product处理的信息。


02. Abstract Factory Pattern

一个工厂一般会生产一系列具有相关性的产品,即产品族

在不同的工厂(大环境下)可能生产同一系列的产品族,具有相似的产品等级结构

DBFactory factory= new OracleFactory();  // 什么样的工厂 生产 哪一个产品族的产品们;所以,以下代码是可复用的。

Connection connection = factory.createConnection():
Statement statement = factory.createStatement();

connection.connect();
statement.executeStatement();

任务的分配:

架构师1:定义factory, product接口;

程序员1:完成OracleFactory类,connection类,statement类。


03. Builder Pattern

如果抽象工厂模式是一个汽车配件生产厂,那么构建者模式是一个汽车组装厂,通过对配件的组装返回一台完整的汽车。

构建者模式将复杂对象的构建对象的表现分离开来,这样使得同样的构建过程可以创建出不同的的表现对象。

ActorController ac = new ActorController();
ActorBuilder ab = new AngelBuilder();
Actor angel = ac.construct(ab);  // 什么样的builder/调料包 构建出 什么样的一个角色/方便面

String type = angel.getType();
System.out.println(type + "的外观:");
System.out.println("性别:" + angel.getSex());
System.out.println("面容:" + angel.getFace());
System.out.println("服装:" + angel.getCostume());
System.out.println("发型:" + angel.getHairstyle());


[Design Patterns] 4. Creation Pattern

时间: 2024-10-14 00:31:12

[Design Patterns] 4. Creation Pattern的相关文章

Learning JavaScript Design Patterns The Module Pattern

The Module Pattern Modules Modules are an integral piece of any robust application's architecture and typically help in keeping the units of code for a project both cleanly separated and organized. In JavaScript, there are several options for impleme

[Design Patterns] 3. Software Pattern Overview

When you're on the way which is unknown and dangerous, just follow your mind and steer the boat. 软件模式: 设计模式.体系结构模式.分析模式.过程模式等. 体系结构模式 ANSIIEEEStd1471一200对体系结构的定义:一个系统的基本组织,表现为系统的组件.组件之间的相互关系.组件和环境之间的相互关系以及设计和进化的原则. 黑板模式 黑板模式是一种常用的架构模式,应用中的多种不同数据处理逻辑相

Learning JavaScript Design Patterns The Observer Pattern

The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains a list of objects depending on it (observers), automatically notifying them of any changes to state. When a subject needs to notify observers about s

Learning JavaScript Design Patterns The Singleton Pattern

The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a class to a single object. Classically, the Singleton pattern can be implemented by creating a class with a method that creates a new instance of the cla

Learning JavaScript Design Patterns The Constructor Pattern

In classical object-oriented programming languages, a constructor is a special method used to initialize a newly created object once memory has been allocated for it. In JavaScript, as almost everything is an object, we're most often interested in ob

Learning JavaScript Design Patterns -- A book by Addy Osmani

Learning JavaScript Design Patterns A book by Addy Osmani Volume 1.6.2 Tweet Copyright © Addy Osmani 2015. Learning JavaScript Design Patterns is released under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 unported license. It

Design Patterns 乌蒙山连着山外山---单件模式singleton pattern

1 //包含单件实例的类Singleton 2 public class Singleton 3 { 4 //声明用于存储单件实例的变量instance 5 private static Singleton instance; 6 //定义用于标识同步线程的对象locker 7 private static Object locker = new Object(); 8 //私有的构造函数Singleton 9 private Singleton() { } 10 //公共访问的返回单件实例的函

Design Patterns Uncovered: The Chain Of Responsibility Pattern

Chain of Responsibility in the Real World The idea of the Chain Of Responsibility is that it avoids coupling the sender of the request to the receiver, giving more than one object the opportunity to handle the request.  This process of delegation app

Design Patterns Example Code (in C++)

Overview Design patterns are ways to reuse design solutions that other software developers have created for common and recurring problems. The design patterns on this page are from the book Design Patterns, Elements of Reusable Object-Oriented Softwa