装饰设计模式适合我吗? [英] Is decorator design pattern right for me?

查看:63
本文介绍了装饰设计模式适合我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Mono / C#套接字服务器,它接受几种不同类型的客户端。



当客户端连接并发送数据时,客户端工厂用于创建新客户端正确的类型。基本类型由数据客户端发送确定。这是一个简单的部分 - 我有IClient接口,每个BaseClient类继承并在基类型客户端的HandleData函数中处理数据。



问题是,一些这些基本客户端有几个子类型应该以不同于基本客户端类的方式处理传入数据。此外,一些客户端可以启用额外的功能(或多个!)来更改预期的数据以及应该如何处理。



所以我的问题是:我应该改变整个事情来使用单基本客户端类与装饰器模式处理所有不同类型的数据处理?还是其他一些模式更适合我的问题?



我尝试过的事情:



目前,具有多个子类型的基本客户端类具有名为ClientDataHandler的内部变量,该变量是进行实际数据处理的类。 'ClientDataHandler'是抽象'ClientDataHandlerBase'类型,它由所有不同的客户端子类继承,在基本客户端类中进行真正的数据处理。



也许这会让事情变得更糟糕更清楚:

1.客户端连接到套接字服务器并发送数据

2. ClientFactory根据数据创建右基本客户端

3.套接字服务器调用客户端'HandleData'

4.客户端(HandleData)确定子类型并相应地实例化'ClientDataHandler'

5.客户端(HandleData)调用'ClientDataHandler.HandleData'



这一切都是混乱,主要是因为几种基本客户端类型可以具有类似的功能(例如解析GPRMC)。类似地,许多客户端子类型可以激活相同的扩展,这导致了不同的'ClientDataHandler'实现之间的大量重复代码:(

I have Mono/C# socket server that accepts several different types of clients.

When client connects and sends data, a client factory is used to create new client of right type. The basic type is determined from the data client sends. This is the easy part - I have IClient interface that each BaseClient class inherits and data is processed in the base type clients 'HandleData' function.

Problem is, some of these base clients has several subtypes that should handle incoming data differently than the base client class. In addition, some of the client can have extra functionality (or several!) enabled that changes what data is expected and how it should be handled.

So my question is: Should I change this whole thing to use single base client class with decorator pattern handling all the different kind of data handling? Or is some other pattern better fit for my problem?

What I have tried:

Currently those base client classes that have several subtypes have internal variable called 'ClientDataHandler' that is a class doing the real data processing. 'ClientDataHandler' is of type abstract 'ClientDataHandlerBase', which is inherited by all the different client subclasses doing the real data processing inside base client class.

Maybe this makes things clearer:
1. Client connects to socket server and sends data
2. ClientFactory creates right base client according to the data
3. Socket server calls clients 'HandleData'
4. Client (HandleData) determines the subtype and instantiates 'ClientDataHandler' accordingly
5. Client (HandleData) calls 'ClientDataHandler.HandleData'

This whole thing is a mess mainly because several of the base client types can have similar functionalities (e.g parse GPRMC). Similarly many of the client subtypes can have same extension activated, which has resulted a lot of duplicate code between different 'ClientDataHandler' implementations :(

推荐答案

当我们说,客户端发送上面1中的数据,数据的格式是什么?如果它是序列化的字节流,服务器将如何确定它应该为相应的数据流初始化哪个具体类型?这实际上使事情变得更复杂。 br />


如果可能,让我们在数据本身有一个共同的标题部分,它描述了客户端类型。然后,在HandleData()方法中,我们应该有那个客户端类型传递,它将初始化客户端的具体类型,并传递剩余数据以初始化对象。在这里,您可以使用对象池模式(< a href =https://en.wikipedia.org/wiki/Object_pool_pattern )a [ ^ ]以提高效率。甚至原型模式(< a href =https://en.wikipedia.org/wiki/Prototype_pattern)a [ ^ ]可能有效。
When we say, client sends data in 1 above, what is the format of the data ? If it is serialized stream of bytes, how will the server determine on which exact concrete type it should initialized for the respective data stream ? This actually makes the things more complicated.

If possible, let's have a common header part in the data itself, which describes the client type. Then, inside the method HandleData(), we should have that client type passed, which will initialize the concrete type of the client, and pass the remaining data to initialize the object. Here, you can use Object pool pattern ( <a href="https://en.wikipedia.org/wiki/Object_pool_pattern")a[^] for better efficiency. Or even prototype pattern ( <a href="https://en.wikipedia.org/wiki/Prototype_pattern")a[^] may work.


这篇关于装饰设计模式适合我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆