首页 \ 问答 \ 参考数据类成员访问者模式(reference data class member visitor pattern)

参考数据类成员访问者模式(reference data class member visitor pattern)

我有一个容器类,它有一个名为DataStore的普通旧数据类型。 此数据存储将传递给访问者。他们共享一个副本,因为访问者可以看到容器执行的任何更新。

class xyz
{
    //Plain Old Datatype
    typedef struct DataStore
    {
        //add any new required data by any test here
        bool    _detectorConnectionStatus;
        DataStore():_detectorConnectionStatus(false){}
    }DataStore;

    DataStore _dataStore;

    typedef struct visitorData
    {
        DataStore& dataStore;
        visitorData(DataStore data):dataStore(data){}
    }visitorData;

    //data to be sent to visitor
    visitorData _visitorData;
};

xyz::xyz():_visitorData(_dataStore)
{
}

class IVisitor
{
private:
struct DataStore* dataStore;
public:
//get the data,no check for validity of data performed
 void visit(struct DataStore& dataStore){};
//process the data
virtual void process() = 0;
};

代码编译但是有更好/更清洁的方法吗?


I have a container class that has a plain old datatype called DataStore. This data store will be passed to visitor.They share a single copy since any updated performed by the container are to be seen by visitor.

class xyz
{
    //Plain Old Datatype
    typedef struct DataStore
    {
        //add any new required data by any test here
        bool    _detectorConnectionStatus;
        DataStore():_detectorConnectionStatus(false){}
    }DataStore;

    DataStore _dataStore;

    typedef struct visitorData
    {
        DataStore& dataStore;
        visitorData(DataStore data):dataStore(data){}
    }visitorData;

    //data to be sent to visitor
    visitorData _visitorData;
};

xyz::xyz():_visitorData(_dataStore)
{
}

class IVisitor
{
private:
struct DataStore* dataStore;
public:
//get the data,no check for validity of data performed
 void visit(struct DataStore& dataStore){};
//process the data
virtual void process() = 0;
};

The code compiles but is there a better/cleaner way to do it?


原文:https://stackoverflow.com/questions/30723758
更新时间:2023-09-01 22:09

最满意答案

由于您的功能正在关闭fluxSEMdepth因此您正在使用del s来修复返回的值,因此您遇到了问题。

flux是一个全局可变列表,每个通过你的函数你清除flux的内容,重新填充它,然后返回一个你之后存储为flux_n flux的引用。 这就是你在同一个底层list对象中的所有 flux_n点。 这同样适用于SEM_n

当您使用matplotlib绘图时,数据会被复制,因此一旦您创建了现有的图形,它们就不会受到影响。

试试:

def data_reader(file_name, first_surface):
    flux = []
    SEM = []
    depth = []

    with open (file_name) as inf:
        lines = inf.readlines()
        for (i, line,) in enumerate(lines):
            if ' surface  %d' %first_surface in line:
                data = (lines[i+1].strip())
                fields = data.split()
                numbers = list(map(float,fields))  
                flux.append(numbers[0])
                SEM.append(numbers[1])
                first_surface += 1
                depth.append(first_surface - 101)                

    return (flux, SEM, first_surface)

You are having issues due to the fact that your function is closing over flux, SEM and depth so you are mucking up the returned values with the dels.

flux is a global mutable list, each pass through your function your clear the contents of flux, re-fill it, and then return a reference to flux which you then store as flux_n. That is all of your flux_n point at the same underlying list object. The same applies for SEM_n.

When you plot using matplotlib that data gets copied so your existing graphs are not affected once you make them.

Try :

def data_reader(file_name, first_surface):
    flux = []
    SEM = []
    depth = []

    with open (file_name) as inf:
        lines = inf.readlines()
        for (i, line,) in enumerate(lines):
            if ' surface  %d' %first_surface in line:
                data = (lines[i+1].strip())
                fields = data.split()
                numbers = list(map(float,fields))  
                flux.append(numbers[0])
                SEM.append(numbers[1])
                first_surface += 1
                depth.append(first_surface - 101)                

    return (flux, SEM, first_surface)

相关问答

更多
  • TL; DR; 你不应该在OCaml堆中存储任何秘密信息。 因此,您绝不能将您的秘密复制到任何OCaml堆分配值中,因此,即使是暂时的,也不能使用字节,字符串或阵列。 OCaml内存模型介绍 OCaml值统一表示为标记的机器字。 一个字的最低有效位用作标签,区分指针(标签= 0)和即时值(标签= 1)。 因此,值总是固定的大小,并且是指针或直接值。 即时值将数据存储在单词的最重要部分,即32位系统中的31位和64位系统中的63位。 指针将数据存储在块中,位于所谓的OCaml堆中 。 OCaml堆是由垃圾收集 ...
  • 通过获取现有值并在PHP中加入字符串来解决问题 $id = $_GET["id"]; $username = $_SESSION['username']; $sql = "SELECT `purchased` FROM `users` WHERE `username` = '".$_SESSION['username']."'"; $data = mysql_query($sql); while($row = mysql_fetch_row($data)) { $ep = $row[0]; ...
  • 释放内存后,通常会重新分配内存。 当该内存存储在堆栈中时尤其如此。 所以,是的。 After the memory is freed it will often be reassigned. This is especially true when that memory is stored on the stack. So, yes.
  • 这很正常,所以你能做的就是制作这样的字典 let data = ["Fornavn": Fornavn.text!,"Efternavn": Efternavn.text!] 然后呢 self.ref.child("users").child(Personnr.text!).setValue(data) 这样做是因为它在Personnr.text父节点下存储了Fornavn和Efternavn数据 That is pretty normal so what you can do is make a di ...
  • 如果您尝试添加重复的ZipEntry,您将获得异常。 如果要替换当前条目,则需要将其删除并重新插入。 我怀疑你获得的异常与这个异常相同。 If you try to add a duplicate ZipEntry you will get an exception. If you want to replace the current entry you need to delete it and re-insert it. I suspect the exception you get is much ...
  • 由于您的功能正在关闭flux , SEM和depth因此您正在使用del s来修复返回的值,因此您遇到了问题。 flux是一个全局可变列表,每个通过你的函数你清除flux的内容,重新填充它,然后返回一个你之后存储为flux_n flux的引用。 这就是你在同一个底层list对象中的所有 flux_n点。 这同样适用于SEM_n 。 当您使用matplotlib绘图时,数据会被复制,因此一旦您创建了现有的图形,它们就不会受到影响。 试试: def data_reader(file_name, first_su ...
  • 这是你的问题: public class Frame{ private static List frame = new ArrayList(); private static int size; private static int serialNum; Frame的成员不应该是静态的,因为静态成员是在类的所有实例之间共享的。 删除静态关键字。 相应地改变构造函数: public class Frame { private List ...
  • 在didSelectItemAtIndexPath您要将Dictionary添加到Array ,您可以使用此Dictionary来区分它来自main collectionView,而不是将isFromMain实例值设置为1 。 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { var dictSub = arr[indexPath.row] ...
  • 您可以在Firebase数据库控制台中的任何位置导入JSON。 所以不仅在根,而且在其下的特定路径,例如/users , /users/charlinagnes等。 在某个位置导入JSON时,Firebase会在该位置执行setValue()操作 。 因此,它会使用您提供的新JSON覆盖该位置的现有数据。 没有用于执行任何类型的合并操作的UI。 但幸运的是Firebase拥有广泛的API(毕竟它是开发人员产品),允许您编写自己的合并逻辑。 使用update()方法可能有助于此类合并。 You can Imp ...
  • opneing文件时的模式是错误的,应该是a+ 。 详见文档: http : //ruby-doc.org/core-2.2.4/IO.html#method-c-new 此外,您可能只想打开该文件一次而不是每行。 Your mode when opneing the file is wrong and should be a+. See details in the docs: http://ruby-doc.org/core-2.2.4/IO.html#method-c-new Also, you mi ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)