Intent

  • Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • A hierarchy that encapsulates: many possible “platforms”, and the construction of a suite of “products”.
  • The new operator considered harmful.

Problem

  • If an application is to be portable, it needs to encapsulate platform dependencies. These “platforms” might include: windowing system, operating system, database, etc. Too often, this encapsulatation is not engineered in advance, and lots of #ifdef case statements with options for all currently supported platforms begin to procreate like rabbits throughout the code.

Discussion

  • Provide a level of indirection that abstracts the creation of families of related or dependent objects without directly specifying their concrete classes. The “factory” object has the responsibility for providing creation services for the entire platform family. Clients never create platform objects directly, they ask the factory to do that for them.
  • This mechanism makes exchanging product families easy because the specific class of the factory object appears only once in the application - where it is instantiated. The application can wholesale replace the entire family of products simply by instantiating a different concrete instance of the abstract factory.
  • Because the service provided by the factory object is so pervasive, it is routinely implemented as a Singleton.

Structure

  • The Abstract Factory defines a Factory Method per product. Each Factory Method encapsulates the new operator and the concrete, platform-specific, product classes. Each “platform” is then modeled with a Factory derived class.

Example

  • The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes. This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles. The stamping equipment is an Abstract Factory which creates auto body parts. The same machinery is used to stamp right hand doors, left hand doors, right front fenders, left front fenders, hoods, etc. for different models of cars. Through the use of rollers to change the stamping dies, the concrete classes produced by the machinery can be changed within three minutes.

Check list

  1. Decide if “platform independence” and creation services are the current source of pain.
  2. Map out a matrix of “platforms” versus “products”.
  3. Define a factory interface that consists of a factory method per product.
  4. Define a factory derived class for each platform that encapsulates all references to the new operator.
  5. The client should retire all references to new, and use the factory methods to create the product objects.

Rules of thumb

  • Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects, Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
  • Abstract Factory, Builder, and Prototype define a factory object that’s responsible for knowing and creating the class of product objects, and make it a parameter of the system. Abstract Factory has the factory object producing objects of several classes. Builder has the factory object building a complex product incrementally using a correspondingly complex protocol. Prototype has the factory object (aka prototype) building a product by copying a prototype object.
  • Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype.
  • Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
  • Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

Abstract Factory in Delphi

source url:http://delphipatterns.blog.com/

UML

source:

unit AbstractFactory;interfacetype// the brands definitionIBrand = interface['{3D8E5363-08E7-4B8F-ABF1-8953E13C7A9A}']function Price: integer;function Material: string;end;TGucci = class(TInterfacedObject, IBrand)publicfunction Price: integer;function Material: string;end;TPoochy = class(TInterfacedObject, IBrand)publicfunction Price: integer;function Material: string;end;// the products definitionIBag = interface['{7EACC8B1-7963-4677-A8D7-9FA62065B880}']function Material: string;end;IShoes = interface['{1560A332-D2D1-43E1-99BB-3D2882C1501A}']function Price: integer;end;TBag = class(TInterfacedObject, IBag)privateFMyBrand: IBrand;publicconstructor Create(brand: IBrand);function Material: string;end;TShoes = class(TInterfacedObject, IShoes)privateFMyBrand: IBrand;publicconstructor Create(brand: IBrand);function Price: integer;end;// factories definitionIFactory = interface['{A9948727-DA7C-4843-83CF-92B81DD158F9}']function CreateBag: IBag;function CreateShoes: IShoes;end;TGucciFactory = class(TInterfacedObject, IFactory)publicfunction CreateBag: IBag;function CreateShoes: IShoes;end;TPoochyFactory = class(TInterfacedObject, IFactory)publicfunction CreateBag: IBag;function CreateShoes: IShoes;end;implementation{ TGucci }function TGucci.Material: string;
beginResult := 'Crocodile skin';
end;function TGucci.Price: integer;
beginResult := 1000;
end;{ TPoochi }function TPoochy.Material: string;
beginResult := 'Plastic';
end;function TPoochy.Price: integer;
vargucci: TGucci;
begingucci := TGucci.Create;tryResult := Round(gucci.Price / 3);finallygucci.Free;end;
end;{ TBag }constructor TBag.Create(brand: IBrand);
begininherited Create;FMyBrand := brand;
end;function TBag.Material: string;
beginResult := FMyBrand.Material;
end;{ TShoes }constructor TShoes.Create(brand: IBrand);
begininherited Create;FMyBrand := brand;
end;function TShoes.Price: integer;
beginResult := FMyBrand.Price;
end;{ TGucciFactory }function TGucciFactory.CreateBag: IBag;
beginResult := IBag(TBag.Create(TGucci.Create));
end;function TGucciFactory.CreateShoes: IShoes;
beginResult := IShoes(TShoes.Create(TGucci.Create));
end;{ TPoochyFactory }function TPoochyFactory.CreateBag: IBag;
beginResult := IBag(TBag.Create(TPoochy.Create));
end;function TPoochyFactory.CreateShoes: IShoes;
beginResult := IShoes(TShoes.Create(TPoochy.Create));
end;end.

-------------------------------------------------------------------------------------------------------------------------

program AbstractFactory.Pattern;{$APPTYPE CONSOLE}usesSysUtils,AbstractFactory in 'AbstractFactory.pas';varbag: IBag;shoes: IShoes;factory: IFactory;begin
//  ReportMemoryLeaksOnShutDown := DebugHook  0;tryfactory := TGucciFactory.Create;bag := factory.CreateBag;shoes := factory.CreateShoes;WriteLn('I bought a Bag which is made from ' + bag.Material);WriteLn('I bought some shoes which cost ' + IntToStr(shoes.Price));factory := TPoochyFactory.Create;bag := factory.CreateBag;shoes := factory.CreateShoes;WriteLn('I bought a Bag which is made from ' + bag.Material);WriteLn('I bought some shoes which cost ' + IntToStr(shoes.Price));ReadLn;excepton E:Exception doWriteln(E.Classname, ': ', E.Message);end;
end.
 
----------------------------------
Result:
 

spaghetti [spə'ɡeti]

  • · n. 意大利式细面条

concrete [kən'kri:t, 'kɔnkri:t]

  • · adj. 混凝土的;实在的,具体的;有形的
  • · vi. 凝结
  • · vt. 使凝固;用混凝土修筑
  • · n. 具体物;凝结物

suite [swi:t]

  • · n. 组曲;套房;(一批)随员,随从;(一套)家具

portable ['pɔ:təbl, 'pəu-]

  • · adj. 手提的,便携式的;轻便的
  • · n. 手提式打字机

engineer [,endʒi'niə]

  • · n. 工程师;工兵;火车司机
  • · vt. 策划;设计;精明地处理
  • · vi. 设计;建造

platform ['plætfɔ:m]

  • · n. 平台;月台,站台;坛;讲台

procreate ['prəukrieit]

  • · vi. 生育(子女);产生
  • · vt. 生殖

throughout [θru:'aut]

  • · adv. 自始至终,到处;全部
  • · prep. 贯穿,遍及

indirection [,indi'rekʃən, -dai-]

  • · n. 间接;迂回;不坦率

brand [brænd]

  • vt. 铭刻于,铭记;打烙印于;印…商标于
  • n. 商标,牌子;烙印

pervasive [pə:'veisiv, pə-]

adj. 普遍的;到处渗透的

wholesale ['həulseil]

  • adj. 批发的;大规模的
  • n. 批发
  • adv. 大规模地;以批发方式
  • vt. 批发
  • vi. 批发;经营批发业

routinely [ru:'ti:nli]

adv. 例行公事地;老一套地

转载于:https://www.cnblogs.com/xiuyusoft/archive/2011/06/16/2082919.html

Design Pattern----01.Creational.AbstractFactory.Pattern (Delphi Sample)相关推荐

  1. Design Pattern----06.Creational.Singleton.Pattern (Delphi Sample)

    Intent Ensure a class has only one instance, and provide a global point of access to it. Encapsulate ...

  2. AI:2020年6月22日北京智源大会演讲分享之09:50-10:40 Anil教授《Pattern Recognition: Statistics to Pattern Recognition》

    AI:2020年6月22日北京智源大会演讲分享之09:50-10:40 Anil教授<Pattern Recognition: Statistics to Pattern Recognition ...

  3. java pattern详解_Java Pattern pattern()用法及代码示例

    Java中Pattern类的pattern()方法用于获取正则表达式,将其编译以创建此模式.我们使用正则表达式创建模式,并且使用此方法来获取相同的源表达式. 用法: public String pat ...

  4. Design Pattern----21.Behavioral.Memento.Pattern (Delphi Sample)

    Intent Without violating encapsulation, capture and externalize an object's internal state so that t ...

  5. Java设计模式 Design Pattern:包装模式 Decorator Pattern

    意图 Attach additional responsibilities to an object dynamically. 为一个对象动态的添加职责. Decorators provide a f ...

  6. TeeChart 8.01 With Source在Delphi 7.0中的安装(转)

    转载自:http://www.360doc.com/content/10/1012/22/3572432_60499559.shtml TeeChart.v8.01安装日志. 为了安装teechart ...

  7. java pattern 怎么用,Java Pattern的用法?

    java中pattern是什么意思? java 中pattern为正则表达式的编译表示形式.指定为字符串的正则表达式必须首先被编译为此类的实例.然后,可将得到的模式用于创建 Matcher 对象,依照 ...

  8. Design Pattern: Observer Pattern

    1. Brief 一直对Observer Pattern和Pub/Sub Pattern有所混淆,下面打算通过这两篇Blog来梳理这两种模式.若有纰漏请大家指正. 2. Use Case 首先我们来面 ...

  9. Head First Design Pattern 读书笔记(4) 工厂模式

    2019独角兽企业重金招聘Python工程师标准>>> Head First Design Pattern 读书笔记(4) Factory Pattern 工厂模式 ##Factor ...

最新文章

  1. R语言编写自定义函数基于ggsumarystats函数计算每个分组的统计值、自定义可视化分组分面条形图,并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr)
  2. kettle 日志存到mysql_kettle作业(job)调用转换,设置变量,写日志到数据库中【转】...
  3. Windows下运行rabbitmqctl 相关命令(如rabbitmqctl stop)报错:Error: unable to perform an operation on node解决方案
  4. 微信小程序自定义组件,提示组件
  5. Buildroot stress-ng Linux系统压力测试
  6. 使用JUnit 5测试异常
  7. java中修改密码_java怎样修改用户名密码?
  8. php memcached 队列,redis获取所有队列_memcached
  9. 网盘工具比较,以及自己开发的网盘工具[转]
  10. shell 将两行内容合并到同一行
  11. 8位并行左移串行转换电路_74ls194串行数据到并行数据的转换
  12. 阿里云下mysql远程访问被拒绝_记一次MySQL数据库拒绝访问的解决过程
  13. java derby xsai2,java-j内的引用罐
  14. systemctl start named失败的解决方法_有关平安银行的提额方法和提额失败的解决方法...
  15. 拓端tecdat|R语言进行数据结构化转换:Box-Cox变换、“凸规则”变换方法
  16. CSS布局:让页脚始终保持底部的方法
  17. 【托业】【金山词霸】单词1-20
  18. 条件概率公式、全概率公式以及贝叶斯公式
  19. 如何解决企业税务压力?这些企业税收优惠政策您要了解
  20. Word插入希腊字母及特殊符号 分类整

热门文章

  1. oracle操作错误还原,Oracle delete误操作数据恢复(BBED)
  2. 手把手教你python作词云,不断更新内容,有问题可以互相交流哦
  3. AES-128-ECB/CBC 查表法 C#实现
  4. 斐波那契数列----有一段楼梯有n级台阶,规定每一步只能跨一级或两级,要登上第n级台阶有几种不同的走法?...
  5. Flutter 3.X二维码扫描功能
  6. 隆重推出:Android KTX 预览版让 Kotlin 代码更精简
  7. Java编程思想-并发
  8. 蓝桥杯万字攻略:算法模板大放送!-c++
  9. FTP服务(文件的上传和下载)
  10. 手机qq邮箱pop3服务器是什么意思,qq邮箱pop3是什么意思怎么弄(邮箱客户端设置中IMAP和POP3有什么区别)...