我有一个 Django 应用程序,它有许多不同的模型,所有模型都有一堆通用数据。最终,我认为这个问题归结为继承和组合之间的决定。我当前的实现是这样的:
\n\nclass Thing(models.Model):\n foo = models.FooField()\n bar = models.BarField()\n type = models.CharField()\n\nclass A(CommonStuff):\n aFoo = models.FooField()\n\nclass B(CommonStuff):\n bFoo = models.FooField()\nRun Code Online (Sandbox Code Playgroud)\n\n通过这个模型,我可以查询Thing使用事物模型的管理器。使用 上的 type 字段Thing,我可以通过查看type包含 'a' 或 'b' 的字段,然后询问 (ie) 来获取子对象数据thing.a.aFoo。这是我喜欢的一个功能,因为获取所有Thing对象的列表是我的应用程序中相当常见的操作。
我在这里看到几个问题。首先,这个type领域似乎没有必要。有没有办法无需先查找类型即可获取子数据?似乎我可以使用一个实例方法来解决这个问题,该方法根据给定的类型值返回正确的对象,或者如果我真的想摆脱类型字段,我可以迭代 上的每个反向关系字段,寻找Thing一个这不会引发DoesNotExist异常。但这对我来说感觉很脆弱。如果我添加一个新的 'subthing' C,我必须更新Thing以查找新类型。我可以通过制作Thing和抽象模型来解决这个问题。这样,A就B可以获取 的所有字段Thing,并且避免使用该type字段。但问题是我失去了对所有Thing对象执行查询的能力。
我正在考虑的另一种模型通过将数据转换为和Thing上的字段来翻转这个模型。AB
class Thing(models.Model):\n …Run Code Online (Sandbox Code Playgroud) 我有点困惑Association, Aggregation and Composition。尽管大量的网站和论坛讨论了这个主题,但在阅读其中一些内容后我变得更加困惑。
我想知道以下是否正确:
1.) 聚合——如果整体被破坏,聚合就会存在。例如, anEngine可以有或没有 a 存在Car。
2.) 组合 - 如果对象被破坏,组合将不存在。例如,Room没有 a 就不可能存在House。
3.) 关联 - 我不确定在什么情况下我们应该使用它。有人可以对此发表评论吗?
当涉及到为聚合、组合和关联编写 Java 代码时
4.)聚合
Class Car {
private Engine engine;
public void setEngine(Engine engine){
this.engine=engine;
}
public Engine getEngine(){
return engine;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为如果是聚合关联,那么就应该accessors and mutators定义。它是否正确 ?
根据我对聚合的定义,我提到如果Car对象被销毁,则Engine可以访问。但是,我在上面的代码中没有看到这种情况发生。如果 Car 对象被销毁,将无法像car.getEngine(). 那么这是怎么发生的呢?
作品
Public House {
private Room room;
Public House (){ …Run Code Online (Sandbox Code Playgroud) 我想做这样的事情:
@QueryHints(@QueryHint(name = "org.hibernate.fetchSize", value = FetchSize.value()))
@Target({ })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FetchSize {
String value();
}
Run Code Online (Sandbox Code Playgroud)
不起作用的部分是value = FetchSize.value()。我想将 @FetchSize 中的值属性传递给目标注释@QueryHint,以便我可以像这样注释:
@FetchSize("1000")
Run Code Online (Sandbox Code Playgroud)
类似的事情是否可能以某种方式实现,我将如何使其发挥作用?
我有一堂课MyClass:
class MyClass(object):
def __init__(self):
pass
def my_function(self, x):
# MyClass.my_function.__doc__ is not writable!
# Otherwise, I could just set it here.
Origin.func(self, x)
Run Code Online (Sandbox Code Playgroud)
该类借用Origin:
class Origin(object):
def func(obj, x):
"""This is a function
"""
# do stuff
pass
Run Code Online (Sandbox Code Playgroud)
如何自动将文档字符串从 Origin.func 复制到 MyClass.my_function 以便 Sphinx Autodoc 识别它?我怎样才能将原始文档字符串扩展几个单词?
编辑:
Afaik,我不能__doc__在函数定义之后进行更改,因为那时 Sphinx 找不到它。或者如果是这样,“docfix”会去哪里?
我是 Swift 的新手,我想知道该语言是否有一些等效于 Python 的装饰器模式。
例如:
import functools
def announce(func):
"""Print a function's arguments and return value as it's called."""
@functools.wraps(func)
def announced_func(*args, **kwargs):
rv = func(*args, **kwargs)
print('In: {0}, {1}'.format(args, kwargs))
print('Out: {}'.format(rv))
return rv
return announced_func
@announce # add = announce(add)
def add(a, b):
return a + b
add(2, 5)
# In: (2, 5), {}
# Out: 7
# 7
Run Code Online (Sandbox Code Playgroud)
也许我只是还没有找到它,但 Swift 似乎没有办法将任意参数转发给函数或保留包装函数的信息(如 functools.wraps 那样)。
是否有等价物,或者该模式不打算在 Swift 中使用?
我的 Android 项目中有复杂的多层架构。
目前我想使用以下 DI 组件和模块的结构:
[Data Layer]
@DataScope //scope is used for caching (Singleton) some Data Layer entities for whole application
- DataComponent //exposes just interfaces which should be used on the BL Layer
//Modules exposes entities for internal (Data Layer) injections and entities which exposed by DataComponent for BL Layer
* DataModule1
* DataModule2
* DataModule3
[Business Logic Layer] (also has component dependency on DataComponent)
@BlScope //scope is used for caching (Singleton) some BL Layer entities for whole …Run Code Online (Sandbox Code Playgroud) 我现在很清楚 mixin 和继承通常被认为是不好的,组合是要走的路,这来自:
https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750
https://facebook.github.io/react/blog/2016/07/13/mixins-thinked-harmful.html
现在,当您发现两个专用于不同事物的组件并且您想要一个是两种行为混合的结果的组件时,该怎么办?例如,我想要一个 textarea,当文本超出初始行时,它会自动增长,并允许在里面提及(又名。react-mentions与混合react-textarea-autosize)
我很难在 React 中用继承代替组合。我将尝试基于 React 风格指南中不鼓励的继承来解释所提出的问题和我当前的解决方案。
首先,我在超级组件中定义公共状态和方法:
export default class SuperComponent extends Component {
constructor(props) {
super(props);
this.state = commonState;
}
foo() {
this.setState({a: 3});
}
render() {
return (
<div>
{this.getContent()}
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
然后我制作具有更多状态和方法的子组件。这些也应该可以访问超级组件的状态:
export default class SubComponent1 extends SuperComponent {
constructor(props) {
super(props);
this.state = Object.assign(this.state, subComponent1State);
}
bar() {
this.setState({b: 7});
}
getContent() {
return (
<div>
I'm subcomponent 1
</div>
)
}
}
export default class SubComponent2 extends SuperComponent {
constructor(props) {
super(props);
this.state = …Run Code Online (Sandbox Code Playgroud) 我一直想知道一个完整的、包罗万象的背景instance Functor (f :.: g)是什么样的。我脑海中立即浮现的想法是:
newtype (f :.: g) a = Comp (f (g a))
instance (Functor f, Functor g) => Functor (f :.: g) where
fmap f (Comp x) = Comp (fmap (fmap f) x)
Run Code Online (Sandbox Code Playgroud)
但是,两个逆变函子也会组合成协变的,如下所示:
instance (Contravariant f, Contravariant g) => Functor (f :.: g) where
fmap f (Comp x) = Comp (contramap (contramap f) x)
Run Code Online (Sandbox Code Playgroud)
这已经不是一个有希望的开始。然而,我也注意到,从技术上讲,f甚至g不必有 kind * -> *—— 唯一的要求f :.: g :: * -> * …
我听说每个拉链都是一个共元,而我认为每个与其自身组成的拉链仍然是一个拉链,因此它是一个共元。所以我决定创建一个。
\n我有以下拉链列表:
\n{-# LANGUAGE DeriveFunctor #-}\n{-# LANGUAGE InstanceSigs #-}\nmodule ListZipper where\nimport Control.Comonad\nimport Data.List (unfoldr)\nimport Test.QuickCheck (Arbitrary (arbitrary))\n\ndata ListZipper a = ZList {previous :: [a], currentL :: a, next :: [a]} deriving (Functor, Eq)\n\ngoLeft, goRight :: ListZipper a -> Maybe (ListZipper a)\ngoLeft (ZList (p:ps) cur ns) = Just $ ZList ps p (cur : ns)\ngoLeft _ = Nothing\ngoRight (ZList ps cur (n:ns)) = Just $ ZList (cur : ps) n ns\ngoRight _ = Nothing\n\nfork :: b -> (b, b)\nfork …Run Code Online (Sandbox Code Playgroud) composition ×10
haskell ×2
inheritance ×2
java ×2
reactjs ×2
aggregation ×1
annotations ×1
associations ×1
comonad ×1
dagger-2 ×1
decorator ×1
django ×1
docstring ×1
functor ×1
instance ×1
javascript ×1
mixins ×1
python ×1
reusability ×1
swift ×1
uml ×1