jeudi 5 mars 2015

Facade vs. Adapter design pattern using network a stream

Let's assume that I have three SDK classes named Phone, DigitalClock and DigitalCompass which representing implicitly digital devices.


These classes supports:



  • PC-to-Device and Device-to-PC data stream.

  • operations like Connect-to-PC or Disconnect-from-PC.


Unfortunately, these classes CANNOT be changed and they don't inheritance from anything.


Now, I have a UDP service and TCP service that supports the following operations:



  • Gets data and transmit it through the internet. (PC-to-NET)

  • Receive data from the internet and return it. (NET-to-PC)


Problem: I want theese devices will be able to transmit and receive their streams through the internet.


Solutions: I thought about two ways to design the above problem, i tested them and they both work:



  1. Wrap every class with Facade-alike design pattern like so:

    • StreamablePhone, StreamableDigitalClock and StreamableCompass.

    • Every wrapper class will inheritance from IStreamableDevice.




Results: Phone->streams to->StreamablePhone->sends to->UDP Service->transmits to->NET.



  1. Wrap these classes with the Adapter design-pattern like so:

    • Adaptees: Phone, DigitalClock and DigitalCompass classes.

    • Adapters: The UDP service and the TCP service inheritance from interfaces(the following Targets) with matching names (IUdp* with UDP* service, ITcp with TCP service).

    • Targets:

      • UDP targets will be IUdpPhone, IUdpDigitalClock and IUdpDigitalCompass.

      • TCP targets will be ITcpPhone, ITcpDigitalClock nad ITcpDigitalCompass.






Results: Phone->streams to->IUdpPhone->transmit to->NET.


Which way should i choose to implement, consider it supports the scenario when more devices or more network services being add.


Aucun commentaire:

Enregistrer un commentaire