问题描述
我有:
几种颜色类别,RGB,CMYK等。
每个都有相同的'ToGrey'方法(去饱和度,分解等)
最初每个类中只有一个ToGrey方法,它使用一个参数来决定使用哪种实际方法。我现在正在实现策略模式,以避免在此早期方法中使用switch语句。
因此我需要:
每个ToGrey实现的类。
关联的接口。
一个竞赛类和每个颜色类中该类的实例。
但是,每个ToGrey实现都需要使用几个不同颜色类中的任何一个,因此方法签名需要一个接口而不是一个颜色类,它需要是允许在颜色之间进行类型转换的接口类型(因为一些ToGrey方法是RedChannel,BlueChannel等)因此要求RedChannel灰色版本的CMYK或HSL颜色变得可能,因为我知道我可以将传递的对象转换为RGB类型。或者每个ToGrey实现都需要为每种颜色类型重载。
这开始变得混乱,设计一定是错的(我想)。 />
有什么想法吗?
附录
==========
以上思考的下一个问题是,一旦我得到一个颜色的实例(但我必须将其视为接口)并将其转换为例如RGB为了得到RedChannel然后它很容易,当我需要将它转换回原来的类类型时,因为我不知道那是什么!
谢谢,
M
I have:
Several color classes, RGB, CMYK etc.
Each has the same 'ToGrey' methods (Desaturate, Decompose etc.)
Initially there was just one ToGrey method in each class which took a parameter to decide which actual method to use. I'm now implementing the strategy pattern in order to avoid having the switch statements in this earlier method.
Therefore I need to have:
A class for each ToGrey implementation.
An associated interface.
A contect class and an instance of that class in each color class.
However, each ToGrey implementation needs to take any one of several different color classes, so the method signature needs an interface rather than a color class and it needs to be the interface that permits type casting between the color types (since some ToGrey methods are RedChannel, BlueChannel etc.) hence asking for the RedChannel grey version of a CMYK or HSL color becomes possible, since I know I can convert the passed object to an RGB type. Either that or every ToGrey implementation needs to be overloaded with every color type.
That's starting to feel messy and hance the design must be wrong (I think).
Any ideas?
Addendum
==========
The next problem with the above thinking is that once I get an instance of a color (but which I must treat as the interface) and convert it to e.g. RGB in order to get the RedChannel then it's easy apart from when I need to convert it back to its original class type, since I have no idea what that was!
Thanks,
M
推荐答案
这篇关于我在寻找什么样的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!