引用链接:http://watirmelon.com/2011/01/24/composition-or-inheritance-for-delegating-page-methods/

Composition or inheritance for delegating page methods?

Posted on January 24, 2011

The thing I like to do when creating a page object pattern for automated web testing is delegating any methods that don’t belong to the Page object itself.

For example, a very simple page object model like this GoogleHomePage doesn’t delegate any methods to the Browser object.

require "rubygems"
require "watir-webdriver"class GoogleHomePagedef initialize(browser)@browser = browserenddef visit@browser.goto "www.google.com"end
endb = Watir::Browser.new :firefox
p = GoogleHomePage.new b
p.visit
puts p.title
p.close

So, the p.title and p.close statements both fail with an error: undefined method `goto' for # (NoMethodError).

One approach would be simply to write appropriate methods for what you would do on the Browser object. For example:

require "rubygems"
require "watir-webdriver"class GoogleHomePagedef initialize(browser)@browser = browserenddef visit@browser.goto "www.google.com"enddef title@browser.titleenddef close@browser.closeend
endb = Watir::Browser.new :firefox
p = GoogleHomePage.new b
p.visit
puts p.title
p.close

But this isn’t DRY. It means every method of Browser you access to needs to be rewritten. But I often see this happen.

What we should be doing is simply delegating any methods that don’t exist on the Page object to the Browser object which is passed in at initialization. There are two ways I know of to do this: inheritance delegation and composition.

Inheritance Delegation

Inheritance delegation means changing our class so it delegates appropriately using a DelegateClass. This means anything of class Browser is delegated.

For example:

require "rubygems"
require "watir-webdriver"class GoogleHomePage < DelegateClass(Watir::Browser) def initialize(browser)super(browser)enddef visitself.goto "www.google.com"end
endb = Watir::Browser.new :firefox
p = GoogleHomePage.new b
p.visit
puts p.title
p.close

From this point forward you don’t need to refer to @browser, instead you just refer to self in your class.

Composition

Composition is about composing the class of different elements, some of which are passed to the browser. This essentially involves creating a method_missing method and passing these methods to the instance variable @browser.

require "rubygems"
require "watir-webdriver"class GoogleHomePagedef initialize(browser)@browser = browserenddef method_missing(sym, *args, &block)@browser.send sym, *args, &blockenddef visit@browser.goto "www.google.com"end
endb = Watir::Browser.new :firefox
p = GoogleHomePage.new b
p.visit
puts p.title
p.close

This means that any reference to the Browser object still needs to refer to @browser throughout the class.

Inheritance or Composition?

You can see from the examples above, both approaches are very similar, but from researching these, it seems that most people prefer composition to inheritance in ruby, mainly due to maintainability of class chains. In our example, the inheritance chain is small and very simple, so I don’t think this poses a great maintainability issue.

转载于:https://www.cnblogs.com/dami520/p/3240738.html

Composition or inheritance for delegating page methods?相关推荐

  1. React中文文档之Composition vs Inheritance

    Composition vs Inheritance - 组合对比继承 React有一个强大的组合模型,并且我们推荐使用组合来代替继承,在组件间重复使用代码. 在这一章节,我们将考虑React新手开发 ...

  2. -Head First Design Pattern- 大师与门徒(3) -Favor Composition over Inheritance

    大师:蚱蜢啊,前上一谈,谅达雅鉴,迄今有些时日了吧.你对"OO中的继承"有什么彻悟吗? 门徒:是的,大师."OO继承"之强大,非吾之言能尽.及至后来,亲见知识, ...

  3. C++面向对象(四)Inheritance, Composition, Delegation

    C++面向对象(四)Composition, Delegation, Inheritance 一.Composition(复合) - 关系表示为:has-a - 其构造和析构的关系 二.Delegat ...

  4. 捍卫者usb管理控制系统_捍卫效用优先CSS

    捍卫者usb管理控制系统 by Sarah Dayan 通过莎拉·达扬 捍卫效用优先CSS (In Defense of Utility-First CSS) "Favor composit ...

  5. COMP 3023代写、代写COMP 3023、代做 C++ - Assignment、 代编码C++ - Assignment

    COMP 3023代写.代写COMP 3023.代做 C++ - Assignment. 代编码C++ - Assignment Revision 1 COMP 3023 Software Devel ...

  6. COMP 3023 国外作业代写、C++ - Assignment代写、代做留学生Software Development程序作业、代做C/C++作业...

    COMP 3023 国外作业代写.C++ - Assignment代写.代做留学生Software Development程序作业.代做C/C++作业 Revision 1 COMP 3023 Sof ...

  7. 程序员练级攻略(2018)-陈皓-笔记整理

    程序员练级攻略(2018)     开篇词     入门篇         零基础启蒙         正式入门     修养篇         程序员修养     专业基础篇         编程语 ...

  8. Effective Java 2.0_中英文对照_Item 8

    文章作者:Tyan 博客:noahsnail.com  |  CSDN  |  简书 CHAPTER3 Methods Common to All Objects ALTHOUGH Object is ...

  9. 不懂SOLID,GRASP这些软件开发原则!写出来的代码都是垃圾!

    从一些老生常谈的事情开始说起来吧,优秀的代码应符合以下特质: 1,可维护性 2,可扩展性 3,模块化 如果代码在其生命周期内保持易于维护.扩展和模块化,那么就上面列出的特性而言,这意味着代码高于平均水 ...

最新文章

  1. android 带边框的圆角按钮
  2. 2019 CES展上最受外媒体关注的中国机器人产品及技术
  3. php实现tcp连接esp8266,ESP8266之TCP透传
  4. Python 技术篇-含中文编码的代码运行方法,(unicode error) ‘utf-8‘ codec can‘t decode问题原因及解决方法
  5. Listener--------监听器
  6. 异星工厂 自动机器人_插件机机器人能够实现工厂自动化
  7. 计组之中央处理器:1、CPU的功能和基本结构
  8. subline修改字体大小和空格问题
  9. 商城系统PageBean分页 Cookie存储浏览记录
  10. 由一道题目引发的为稳定与不稳定的排序思路
  11. vue.js的生命周期
  12. Use AVAudioPlayer in OperationQueue
  13. jad的用法(反编译某目录下所有class)
  14. VS社区版许可证过期更新
  15. 悟空互动:如何让百度更快的收录网站,试试快速收录提交入口!
  16. cocos2dx[2.x](13)--基本绘图DrawPrimitives
  17. 学历真的是衡量一个人的首要条件吗?
  18. c++小游戏:飞机游戏
  19. 谱分析——连续傅里叶变换
  20. 前端常用的文档及组件库

热门文章

  1. java jama_java矩阵包jama的简单操作
  2. 梦醒了,一切都结束了
  3. 因果推断笔记——因果图建模之微软开源的EconML(五)
  4. 前端知识天天学(2)
  5. 经典SQL语句大全(转)
  6. base64与图片互换
  7. 阿里云linux主机更新hostname
  8. Sql Server 2005如何导入DBF文件?
  9. pad点餐系统 内存管理的一点总结
  10. Try Microsoft AutoCollage 2008