简体   繁体   English

每个类引用相同数据时如何保持类之间的松散耦合

[英]How to maintain loose coupling between classes when each class is referencing the same data

I have two spinners in an activity, A and B. Each spinner uses an arrayAdapter linked to one or more string-arrays from an XML file. 我在活动A和B中有两个微调器。每个微调器都使用一个arrayAdapter链接到XML文件中的一个或多个字符串数组。 I need multiple because B's values are populated depending on what is selected in A. 我需要多个,因为根据A中选择的内容填充了B的值。

A reference to some arrays that B uses: 对B使用的某些数组的引用:

<!-- Used in spinner mCcColor -->
<string-array name="component0_color">
    <item>Chocolate</item>
    <item>Yellow</item>
    <item>Strawberry</item>
</string-array>

<!-- Used in spinner mCcColor -->
<string-array name="component1_color">
    <item>Blueberry</item>
    <item>Chocolate</item>
    <item>Dark Chocolate</item>
</string-array>

Next, I created a custom view class that is basically a canvas that I draw bitmaps (png) to. 接下来,我创建了一个自定义视图类,该类基本上是一个画布,可以在其中绘制位图(png)。 Spinner A is the bitmap, and spinner B is color. 微调器A是位图,微调器B是颜色。 So, in order to determine what to draw in this view, I need to know what the user selected in spinner A and B. 因此,为了确定在此视图中绘制的内容,我需要知道用户在微调器A和B中选择的内容。

It's fairly easy to pass the position that was selected in both spinner A and spinner B. Here is an example. 通过微调器A和微调器B中选择的位置相当容易。这是一个示例。

ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.cc_components, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mCcComponent.setAdapter(adapter1);
mCcComponent.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
     //Call the method of the custom view that draws to the canvas
     mSelectedComponent = position;
     myCustomView.foo(mSelectedComponent, mSelectedCompColor);
     myCustomView.postInvalidate();
});

The problem I have is tying the index numbers of the spinners I pass into the custom view. 我遇到的问题是将传递给自定义视图的微调器的索引号绑定在一起。 I could do a bunch of switch case crap based on the position values I pass in, but this seems unnecessary because each case is just a few static values that correlate to the spinner. 我可以根据传入的位置值进行一堆开关案例废话,但这似乎是不必要的,因为每种情况只是与微调器相关的一些静态值。 Plus then I would be tightly coupling my view class to the caller activity. 另外,我将把视图类与调用者活动紧密结合在一起。

It seems like it would be much easier and cleaner to set-up a more complex array in XML and have it contain strings and 3 integer values for each string. 用XML设置一个更复杂的数组,并使其包含字符串和每个字符串3个整数值,似乎会更容易,更清洁。

Is there a way I can use a complex array without causing headaches for my spinner, the image, or the colors inside the custom view? 有没有一种方法可以使用复杂的数组而不会引起我的微调器,自定义视图中的图像或颜色麻烦呢? Is there a better way to approach this? 有没有更好的方法来解决这个问题? Can anyone point me to some topics or links or post a few lines of code? 谁能指出一些主题或链接,或者发布几行代码?

Thanks everyone! 感谢大家!

Here is a visual representation of what I'm trying to explain, which seems like a cleaner way to solve this: 这是我要解释的可视化表示,这似乎是解决此问题的一种更简洁的方法:

<!-- Used in spinner mCcColor -->
<array name="component1_color">
    <item>Blueberry</item>
     <red>81</red>
     <green>27</green>
     <blue>21</blue>
    <item>Chocolate</item>
     <red>45</red>
     <green>21</green>
     <blue>6</blue>
</array>

Why not create a class for each of these, so you don't deal with the array directly, but go through functions? 为什么不为每个对象创建一个类,这样就不必直接处理数组,而要通过函数呢?

Then you can have listeners if you want, so that the two spinners will be informed when there is a change and can adapt. 然后,如果需要,您可以拥有听众,以便在发生变化时可以通知两个微调器并且可以适应。

If you are worried about tight coupling, encapsulating the shared array will remove this difficulty. 如果您担心紧密耦合,则封装共享阵列将消除此困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM