composition

How to write solid Pure Aggregation (composition) Game Objects in Java?

左心房为你撑大大i 提交于 2019-12-10 02:52:22
问题 So I am just at the beginning of writing a game in Java and I am writing my game objects. Now I have read here in Evolve Your Hierarchy that you should build your games as compositions and not as a big class hierarchy. As this image from the previous link shows: However, when actually getting down to the implementation I have one small question about where to apply the interfaces. Lets say you have a class called Player and the interfaces Moveable and Renderable. Do you implement this using

Extending a JFrame

时光总嘲笑我的痴心妄想 提交于 2019-12-10 02:23:38
问题 What are the pros and cons of extending a JFrame rather than create a new JFrame ? For example: public class Test extends JFrame { setVisible(true); } or public class Test { JFrame test = new JFrame(): test.setVisible(true); } 回答1: You should not extend a class, unless you want to actually extend its functionality, in the example you've shown, you should use option 2, as option 1 is an abuse of the extend ing feature. In other words - as long as the answer to the question is Test a JFrame ?

How to write without Do notation

有些话、适合烂在心里 提交于 2019-12-10 02:19:13
问题 I was playing around with composable failures and managed to write a function with the signature getPerson :: IO (Maybe Person) where a Person is: data Person = Person String Int deriving Show It works and I've written it in the do-notation as follows: import Control.Applicative getPerson = do name <- getLine -- step 1 age <- getInt -- step 2 return $ Just Person <*> Just name <*> age where getInt :: IO (Maybe Int) getInt = do n <- fmap reads getLine :: IO [(Int,String)] case n of ((x,""):[])

How to redirect all methods of a contained class in Python?

不打扰是莪最后的温柔 提交于 2019-12-09 04:18:11
问题 How to implement the composition pattern? I have a class Container which has an attribute object Contained . I would like to redirect/allow access to all methods of Contained class from Container by simply calling my_container.some_contained_method() . Am I doing the right thing in the right way? I use something like: class Container: def __init__(self): self.contained = Contained() def __getattr__(self, item): if item in self.__dict__: # some overridden return self.__dict__[item] else:

How to implement only certain methods of an abstract class?

自作多情 提交于 2019-12-08 08:45:33
In my concrete class I need to implement (set public) only certain methods of an abstract class. So I cannot extend it beacause all the abstract methods are public. I could use composition, and forward just methods I need, but I obviously need to implement the abstract class. So, I'm wondering, is it a good approach to implement an abstract class in a private class and then forward in the parent class only the needed methods of the inner class? For example: public class ConcreteClass{ private InnerClass inner = new InnerClass(); public String fwMethod() { return inner.method1(); } private

How to use AVMutableComposition and CALayers on iOS

我与影子孤独终老i 提交于 2019-12-07 17:18:32
问题 I'm planning to render content in a view on iOS using AV mutable composition. I want to combine the video coming from one of the iPhone cameras with content created in a layer - mutable composition seems to fit the bill here as it can composite layers into the video content. It's not critical that the compositing be done as video is being recorded - I'm also happy to mix the required data into a composition that is then rendered (via AVExportSession) to a file after initial video recording

How to embed a map into a struct so that it has a flat json representation

守給你的承諾、 提交于 2019-12-07 16:36:16
问题 In order to create a table-like structure, I serialized my row data in following format in my previous application: { "key1": "...", "key2": "...", "15/04": 1.3, "15/05": 1.2, .... "17/08": 0.8 } Now I am trying to rewrite it in Go in order to learn the language with hands-on experience. In Go, one can compose two structs together by embedding them into another struct. The marshalled json out of that struct will have a flat structure, i.e. the resulting json object will have union of fields

What is the best way to use mixins in js?

你离开我真会死。 提交于 2019-12-07 15:24:44
问题 Recently, I came across two articles on mixins. That got me confused between which one is better than other. First one from mdn var calculatorMixin = Base => class extends Base { calc() { } }; var randomizerMixin = Base => class extends Base { randomize() { } }; class Foo { } class Bar extends calculatorMixin(randomizerMixin(Foo)) { } Second one from https://javascript.info/mixins let sayMixin = { say(phrase) { alert(phrase); } }; let sayHiMixin = { __proto__: sayMixin, // (or we could use

MEF: GetExportedValue from Type?

别来无恙 提交于 2019-12-07 14:56:11
问题 Using MEF I can create and load a type like this: var view = Container.GetExportedValue<MyView>(); Now what I want to do is this: Type t = typeof(MyView); var view = Container.GetExportedValue<t>(); (of course the type might contain something different than MyView). This is not possible using the generics GetExportedValue<> - is there some other way to achieve this? 回答1: You can use reflection. Here is an example: using System; using System.ComponentModel.Composition; using System

wpf - best practice of registering a DelegateCommand to a CompositeCommand

岁酱吖の 提交于 2019-12-07 14:44:08
问题 iv'e got a CompositeCommand exposed globally in my startup project public static class Commands { public static readonly CompositeCommand DiceRolledCommand = new CompositeCommand(); } in a ControlLibrary referenced by my startup project iv'e got a Control which has a DelegateCommand , each instance of this Control has to register it's Command with the globally exposed DiceRolledCommand. what wold be the best practice of doing so : here are 3 idea's of which the first 2 i don't like because