- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
给定一个接口(interface)和两个(或更多)实现,我很难在扩展功能时轻松切换实现。
例如,假设有一个支持 Inc 和 String 的接口(interface) INumber 以及两个实现 NumberInt32 和 NumberInt64 及其明显的实现。假设我想在 INumber 之上实现一个 EvenCounter。 EvenCounter 只有一个 IncTwice 并且应该调用 Inc 两次。如果不在 EvenCounter 中的 INumber 周围使用额外的结构,我很难获得正确的类型。
type INumber interface {
Inc()
String() string
}
type NumberInt32 struct {
number int32
}
func NewNumberInt32() INumber {
ret := new(NumberInt32)
ret.number = 0
return ret
}
func (this *NumberInt32) Inc() {
this.number += 1
}
func (this *NumberInt32) String() string {
return fmt.Sprintf("%d", this.number)
}
// type NumberInt64.... // obvious
这是我奋斗的地方
type EvenCounter1 INumber // nope, additional methods not possible
type EvenCounter2 NumberInt32 // nope
func (this *EvenCounter2) IncTwice() {
for i:=0; i < 2; i+=1 {
// this.Inc() // Inc not found
// INumber(*this).Inc() // cannot convert
// in, ok := *this.(INumber) // cannot convert
// v, ok := this.(INumber) // cannot convert
// a concrete conversion a) does not work and b) won't help
// here it should be generic
// v, ok := this.(NumberInt32)
// How do I call Inc here on this?
}
}
只需嵌入到一个结构中就可以了...
type EvenCounter3 struct {
n INumber
}
func (this *EvenCounter3) IncTwice() {
n := this.n // that is a step I want to avoid
n.Inc() // using this.n.Inc() twice makes it slower
n.Inc()
}
func (this *EvenCounter3) String() string {
return this.n.String()
}
我可以忍受为每个方法手动实现委托(delegate)的需要,但是显然我想依赖 INumber 而不是具体的实现(即将意味着改变很多地方来尝试另一种实现,但是,我想避免额外的间接访问和(很可能?)额外的空间。有没有避免结构并直接说 EvenCounter 是具有其他方法的(特定)INumber 的方法?
顺便说一下,真实的例子是一组整数和一个整数到整数的映射,其中有数百万个实例相互交织(不,仅仅 map[int]bool 是不够的——太慢了,bitset 很有趣,具体取决于用例等)并通过更改代码中的 2-3 行轻松测试集合和映射的不同实现(理想情况下只是类型,也许实例的通用创建。复制)
感谢任何帮助,我希望这还没有被问到......
最佳答案
您使用嵌入的变体实际上并未嵌入。 Embedded fields are anonymous and Go then delegates automatically .
这将您的示例简化为:
type EvenCounter3 struct {
INumber
}
func (this *EvenCounter3) IncTwice() {
this.Inc() // using this.n.Inc() twice makes it slower
this.Inc()
}
注意 String() 是自动委托(delegate)的(在 Go 语言中是“提升的”)。
至于调用 Inc() 两次使其变慢,好吧,这是使用接口(interface)的限制。接口(interface)的要点是不公开实现,因此您无法访问其内部数字变量。
关于types - 无开销的开关接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14869440/
我想知道,通过数据 channel 发送数据时 WebRTC 会产生多少开销。 我知道 Websockets 每帧有 2 - 14 字节的开销。 WebRTC 是否使用更多开销?我在网上找不到一些有用
我想知道与创建新类而不是该类的新对象相关的开销是小还是大。我正在使用 dojo,但我将提供纯 JS 的示例。我将在启动时创建 10 到 100 个对象,我认为这不会是一个严重的问题,但我想涵盖所有基础
我有一个如下所示的表设置。 Table comment_flags user_id comment_id 我允许用户标记评论,然后给他们取消标记的选项,因为他们可能犯了一个错误。 问题
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: In MySQL what does “Overhead” mean, what is bad about it,
我正在制作一个非常简单的游戏,只是为了好玩/练习,但无论它现在有多简单,我仍然想很好地编写它,以防我想回到它并只是为了学习 因此,在这种情况下,我的问题是: 对象分配涉及多少开销?解释器对此的优化程度
我有一些资源敏感的东西要写。我想知道与仅将这些变量一起传递(例如作为函数参数)相比,在结构中将变量组合在一起是否真的会导致内存开销。 如果是这样,那么在不产生开销的情况下创建对惰性值进行操作的东西的好
我一直在开发一个实时应用程序,并注意到一些 OOP 设计模式在 Python 中引入了难以置信的开销(使用 2.7.5 进行了测试)。 直截了当,当字典被另一个对象封装时,为什么简单的字典值访问器方法
我正在从 ifstream 中读取随机 ascii 文本文件。我需要能够将整个消息放入字符串类型以进行字符解析。我当前的解决方案有效,但我认为我通过使用等效于此的方式来谋杀更冗长文件的处理时间: st
纯粹从软件工程的角度来看,getActivity() 有多少开销? 我在整个应用程序中经常多次使用此方法,并考虑使用一个引用 getActivity() 的全局变量。 如果为 Activity 设置一
我一直在研究 Riccardo Terrell 的 Akka.NET 分形演示 (https://github.com/rikace/akkafractal) 以尝试理解它。 (这很棒,顺便说一句)
我正在尝试使用高分辨率计时器查找我的代码运行时间,我注意到计时器的结果不一致,我想知道为什么会这样。 我找到了这篇文章 How do you test running time of VBA code
我正在学习WPF。我现在开始装订了。使用 INotifyPropertyChanged 时绑定(bind)是否依赖反射?是这样,价格是多少?我正在考虑使用 WPF 来显示通过 UDP 流式传输的数据,
我有某种模板化基类 template class Base { }; 并希望将其派生实例存储在列表中。为此,我使用 using derived_handle = std::unique_ptr v
使用GHC.TypeLits中的Sing有任何开销吗? ?以程序为例: {-# LANGUAGE DataKinds #-} module Test (test) where import GHC.T
我有某种模板化基类 template class Base { }; 并希望将其派生实例存储在列表中。为此,我使用 using derived_handle = std::unique_ptr v
我有一个 ORM sqlalchemy 模型,我需要构建一个查询(使用 ORM 类更容易构建),但这需要大量时间。当我直接像 SQL 一样向数据库执行相同的查询时,速度相当快。 使用 SQLAlche
我在 PHP 平台上有一家商店(开发不善),那里有很多不好的查询(没有索引的长查询、rand() 排序、动态计数,..) 我现在无法更改查询,但我必须调整服务器才能保持事件状态。 我尝试了我所知道的一
我有一个使用 JQuery mobile 构建的移动应用程序,响应时间对我来说非常重要,因为我希望为我的用户提供流畅的体验。 我刚刚将网站的安装移至本地服务器,以提高应用程序的性能,因为它连接到本地
关于数据库设计的问题。如果我有 28 个 bool 值并且能够将它们添加为每行 28 个 bool 值或一个整数,哪一个会更快?哪种方法将使磁盘上的表大小保持最低? 这是在假设我需要的可以通过查询中的
我有一个看起来像 Boost.Array 的简单类。有两个模板参数 T 和 N。Boost.Array 的一个缺点是,每个使用这种数组的方法都必须是带有参数 N 的模板(T 可以)。结果是整个程序往往
我是一名优秀的程序员,十分优秀!