Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将对象C从类A发送到类B_Python_Design Patterns_Class Design - Fatal编程技术网

Python 将对象C从类A发送到类B

Python 将对象C从类A发送到类B,python,design-patterns,class-design,Python,Design Patterns,Class Design,我不知道如何在我的系统中设计类 在classA中,我创建了对象selenium(它模拟网站上的用户操作) 在这个类中,我创建了另一个对象,如SearchScreen、Payment\u Screen和Summary\u Screen # -*- coding: utf-8 -*- from selenium import selenium import unittest, time, re class OurSiteTestCases(unittest.TestCase): def s

我不知道如何在我的系统中设计类

在classA中,我创建了对象selenium(它模拟网站上的用户操作)

在这个类中,我创建了另一个对象,如SearchScreen、Payment\u Screen和Summary\u Screen

# -*- coding: utf-8 -*-
from selenium import selenium
import unittest, time, re

class OurSiteTestCases(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []

        self.selenium = selenium("localhost", 5555, "*chrome", "http://www.someaddress.com/")
        time.sleep(5)
        self.selenium.start()        

    def test_buy_coffee(self):

        sel = self.selenium

        sel.open('/')
        sel.window_maximize()

        search_screen=SearchScreen(self.selenium)
        search_screen.choose('lavazza')

        payment_screen=PaymentScreen(self.selenium)
        payment_screen.fill_test_data()

        summary_screen=SummaryScreen(selenium)
        summary_screen.accept()


    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
这是SearchScreen模块的示例:

class SearchScreen:
    def __init__(self,selenium):
        self.selenium=selenium

    def search(self):
        self.selenium.click('css=button.search')

我想知道这些类的设计是否合适?

如果SearchScreen/PaymentScreen/SummaryScreen只执行测试逻辑,那么在我看来,您最好将该逻辑放在我们SiteTestCases的实用方法中

test_buy_coffee方法的可能设计(取决于您在SearchScreen等中的实际操作):

编辑:
如果您需要在(选择)搜索(search),(fill)支付(payment)数据(data)和(accept)摘要(summary)中考虑测试逻辑,以便在测试之间共享,您可以在一个。或者,您可以编写一个测试基类,其中包含selenium对象(self.selenium)并具有“受保护”的实用程序方法\u choose\u search、\u fill\u payment\u data和\u accept\u summary。这完全取决于你的实际情况:)

你的方法很好。您有一组工具类,每个工具类都需要知道其目标。然后您就有了一个toolkit类,该类在特定目标上协调这些工具

class AgreePrice:
    def __init__(self, connection): ...

class PlaceOrder:
    def __init__(self, connection): ...

class ConfirmAvailability:
    def __init__(self, connection): ...

class BookingService:
    def __init__(self, connection): ...

    def book(self): 
        for Command in (ConfirmAvailability, AgreePrice, PlaceOrder):
            command = Command(self.connection)
            command.run()
            assert command.success()
这些类型的类结构没有任何问题,事实上它们总是出现,当单个“工具”类不能方便地放在一个函数中时,它们是一个相当好的设计

如果您发现自己的类中有几十个方法,其中许多方法可以根据特定任务进行分组,那么这是一个很好的重构

一般来说,您希望确保您的“工具”类(
SearchScreen
等)在概念上低于您的控制器(测试用例)。这是给你的

最简单的说,这些工具类是设计模式的一种形式。虽然在您的例子中,您对每个对象调用的方法不止一个,所以它们稍微复杂一些



或者,简而言之。您的设计很好,非常普通。

也许您可以使用责任链模式:

定义: 通过给多个对象一个处理请求的机会,避免将请求的发送方与其接收方耦合。链接接收对象并沿链传递请求,直到对象处理它

例如c#中的:


下面是完整的文档:

为什么需要SearchScreen等的类?通过将它们划分成不同的类,您得到了什么?我只给出了测试用例的伪代码示例。我有很多测试用例,所以我创建SearchScreen等来避免重复code@user278618:请在我的答案末尾查看我的更新。
class AgreePrice:
    def __init__(self, connection): ...

class PlaceOrder:
    def __init__(self, connection): ...

class ConfirmAvailability:
    def __init__(self, connection): ...

class BookingService:
    def __init__(self, connection): ...

    def book(self): 
        for Command in (ConfirmAvailability, AgreePrice, PlaceOrder):
            command = Command(self.connection)
            command.run()
            assert command.success()
  Handler h1 = new ConcreteHandler1();

  Handler h2 = new ConcreteHandler2();

  Handler h3 = new ConcreteHandler3();

  h1.SetSuccessor(h2);

  h2.SetSuccessor(h3);



  // Generate and process request

  int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };


  foreach (int request in requests)
  {
    h1.HandleRequest(request);
  }