简体   繁体   English

面向对象编程中的对象交互

[英]Interaction of objects in object-oriented programming

I have been struggling trying to find a nice pattern in objected oriented programming for the following application. 我一直在努力为以下应用程序在面向对象的编程中找到一个不错的模式。

Suppose you have an object that needs to be modified - maybe, the object is a house and its property price will be modified/added. 假设您有一个需要修改的对象-也许该对象是一栋房屋,其房价将被修改/添加。 This modification is based on properties dimension_x and dimension_y . 此修改是基于属性dimension_xdimension_y But in order to set a price, we need to compare instances from other class (in this case, Houses) that have dimension_x and dimension_y . 但是为了设置价格,我们需要比较来自其他类(在本例中为Houses)的具有dimension_xdimension_y实例。

class Houses(object):
  def __init__(self, dimension_x=None, dimension_y=None):
    self.dimension_x = dimension_x
    self.dimension_y = dimension_y

class City(object):
  def __init__(self, houses_without_price=None):
    self.houses_without_price = houses_without_price
    self.houses = []
  def set_dimensions(self, house1_without_price, house2_with_price):
      # calculate dimension_x, dimension_y and set these properties 
      # in an instance of Houses
  def set_price(self, house1):
     # based on dimension_x and dimension_y from our instance of Houses, 
     # add a price to the instances of self.houses_without_price

So basically our class Houses is just a storage for dimension_x and dimension_y. 因此,基本上,我们的房屋之类只是Dimension_x和Dimension_y的存储。 Obviously, I could use a dictionary instead. 显然,我可以改用字典。 However, the interaction between the houses_without_price and a dictionary tends to be overcomplicated. 但是,houses_without_price和字典之间的交互往往过于复杂。 For example: 例如:

d = [{'object': house_without_price_1, 'dimension_x': 20, 'dimension_y': 34}, 
{'object': house_without_price_2, 'dimension_x': 43, 'dimension_y': 55}...]

and then calculate the price property. 然后计算价格属性。

What is the best way to deal with this kind of interaction between objects? 处理对象之间这种相互作用的最佳方法是什么?

Create a manager Class. 创建一个经理类。

Something that will contain all the House Classes, in let's say a list. 在清单中将包含所有房屋类别的物品。

Then you can include a function in the manager class like: 然后,您可以在manager类中包含一个函数,例如:

def CompareDimensions(self):
    dims = [list(instance.GetDimensions()) for instance in self.house_list]
    dims = numpy.array(dims)
    # .... futher processing on matrix

Assuming your house class has a function called GetDimensions, which returns a tuple of (dim_x,dim_y), you can easily build a matrix to compare these values. 假设您的房屋类有一个名为GetDimensions的函数,该函数返回(dim_x,dim_y)的元组,则可以轻松地构建一个矩阵来比较这些值。

Using a manager will also give you the ability to never having to directly deal with each individual house class, but you can abstract the processes from the manager. 使用管理器还可以使您不必直接处理每个房屋类,但是您可以从管理器中提取流程。 Such as creating a manager function called PushNewHouse(self,dim_x,dim_y) 例如创建一个名为PushNewHouse(self,dim_x,dim_y)的管理器功能

A more detailed explaination: 更详细的解释:

import numpy # Assuming you have numpy available

class Houses(object):
    def __init__(self, dimension_x=None, dimension_y=None):
        self.dimension_x = dimension_x
        self.dimension_y = dimension_y
    def GetDimensions(self):
        return (self.dimension_x,self.dimension_y)

class HouseManager(object):
    def __init__(self):
        self.house_list = []
    def PushNewHouse(self,dim_x,dim_y):
        self.house_list.append(House(dim_x,dim_y))
    def CompareDimensions(self):
        dims = [list(instance.GetDimensions()) for instance in self.house_list]
        dims = numpy.array(dims)
        # .... futher processing on matrix

I don't know python, but in C# it would look something like this: 我不懂python,但是在C#中看起来像这样:

public class House(){
    public double X { get; set; }
    public double Y { get; set; }
    public double Area(){
       return X*Y;
    }
    public double Value { get; set; }
}

Not sure how you define a dictionary in python, but basically in C# like pseudocode, you want 不知道如何在python中定义字典,但是基本上想在C#中使用伪代码

Dictionary<double, List<House>> HousesWithValue = new Dictionary<int, List<House>>(); 

My original understanding of the process was that you would have a whole collection of houses with known values that get added to the dictionary. 我对该过程的最初理解是,您将拥有一整套具有已知值的房屋,并将其添加到字典中。 You have a new house that has no value. 您有没有价值的新房子。 So when you add it to the city, first, check the dictionary based on the area, and from that list, you can get the average price from the house values. 因此,当您将其添加到城市时,首先,请根据区域检查字典,然后从该列表中获取房屋价格的平均价格。

But if none of the houses have a price, what you are basically just looking for is a way to SORT the houses that have properties that you do have values for. 但是,如果没有一间房子有价格,那么您基本上只想寻找一种对具有您确实有价值的房屋的房屋进行排序的方法。

// my apologies if this isn't correct python, I'm not that familiar with it. //如果这不是正确的python,我深表歉意,对此我不太熟悉。

house_tuples = [
    ('house1', 20, 30, 600),
    ('house2', 30, 40, 1200),
    ('house3', 30, 30, 900),
]
>>> sorted(house_tuples, key=lambda house: house[3])   # sort by area or something

Then you can sort the list based on area or something. 然后,您可以根据面积或其他内容对列表进行排序。 This could give you a picture of the "relative" price even if you have no knowledge of absolutes. 即使您不了解绝对价格,这也可以为您提供“相对”价格的图片。 So if after sorting, it turns out that house3 is second, then it will have a value in the middle or whatever. 因此,如果排序后发现house3是第二个,那么它将在中间有一个值。

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

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