且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么时候需要使用后期静态绑定?

更新时间:2022-12-08 11:13:29

在以下情况下,我需要使用LSB:

I needed LSB this for the following scenario:

  • 想象一下您正在构建一个邮件处理器"守护程序,该守护程序从电子邮件服务器下载消息,对其进行分类,解析,保存,然后根据消息的类型执行某些操作.
  • 类层次结构:您有一个基本的Message类,带有子级"BouncedMessage"和"AcceptedMessage".
  • 每种消息类型都有自己的方式将自身持久保存在磁盘上.例如,所有BouncedMessage类型的消息都尝试将其自身另存为BouncedMessage-id.xml.另一方面,AcceptedMessage需要以不同的方式保存自身-作为AcceptedMessage-timestamp.xml.这里重要的是,用于确定文件名模式的逻辑对于不同的子类而言是不同的,但是对于子类中的所有项都是 shared (共享).这就是为什么采用静态方法才有意义.
  • 基本消息类具有抽象的静态方法(是的,抽象的 AND 静态)保存". BouncedMessage使用具体的静态方法实现此方法.然后,在实际检索消息的类中,您可以调用":: save()"
  • Imagine you're building a "mail processor" daemon that downloads the message from an email server, classifies it, parses it, saves it, and then does something, depending on the type of the message.
  • Class hierarchy: you have a base Message class, with children "BouncedMessage" and "AcceptedMessage".
  • Each of the message types has its own way to persist itself on disk. For example, all messages of type BouncedMessage try to save itself as BouncedMessage-id.xml. AcceptedMessage, on the other hand, needs to save itself differently - as AcceptedMessage-timestamp.xml. The important thing here is that the logic for determining the filename pattern is different for different subclasses, but shared for all items within the subclass. That's why it makes sense for it to be in a static method.
  • Base Message class has an abstract static method (yes, abstract AND static) "save". BouncedMessage implements this method with a concrete static method. Then, inside the class that actually retrieves the message, you can call "::save()"

如果您想了解更多有关该主题的信息:

If you want to learn more about the subject:

  • http://www.qcodo.com/forums/topic.php/2356
  • http://community.livejournal.com/php/585907.html
  • http://bugs.php.net/bug.php?id=42681