composition

How do I use composition with inheritance?

元气小坏坏 提交于 2019-12-03 08:16:36
I'm going to try to ask my question in the context of a simple example... Let's say I have an abstract base class Car. Car has-a basic Engine object. I have a method StartEngine() in the abstract Car class that delegates the starting of the engine to the Engine object. How do I allow subclasses of Car (like Ferrari) to declare the Engine object as a specific type of engine (e.g., TurboEngine)? Do I need another Car class (TurboCar)? I'm inheriting a plain old Engine object and I cannot re-declare (or override) it as a TurboEngine in my Car subclasses. EDIT: I understand that I can plug any

React.js: Composing components to create tabs

本小妞迷上赌 提交于 2019-12-03 06:47:55
I’m trying to make a tabs component. TabsSwitcher and TabsPanel must be separate components so they could be used anywhere in DOM, e.g. TabsSwitcher doesn’t have to be followed by TabsPanel. To make it work, I need to somehow connect these components. Furthermore, TabsSwitcher must be able to tell TabsPanel when a tab was clicked. /** @jsx React.DOM */ var TabsExample = React.createClass({ render: function() { var tabs = [ {title: 'first', content: 'Content 1'}, {title: 'second', content: 'Content 2'} ]; return <div> <TabsSwitcher items={tabs}/> <TabsContent items={tabs}/> </div>; } }); var

Inheritance, composition and default methods

做~自己de王妃 提交于 2019-12-03 06:34:52
It is usually admitted that extending implementations of an interface through inheritance is not best practice, and that composition (eg. implementing the interface again from scratch) is more maintenable. This works because the interface contract forces the user to implement all the desired functionality. However in java 8, default methods provide some default behavior which can be "manually" overriden. Consider the following example : I want to design a user database, which must have the functionalities of a List. I choose, for efficiency purposes, to back it by an ArrayList. public class

Haskell function composition

风格不统一 提交于 2019-12-03 05:36:42
问题 I am reading this tutorial on Haskell. They define function composition as the following: (.) :: (b->c) -> (a->b) -> (a->c) f . g = \ x -> f (g x) No examples were provided, which I believe would enlighten me as to what is being defined here. Can someone provide a simple example (with explanation) of how function composition is used? 回答1: Function composition is a way to "compose" two functions together into a single function. Here's an example: Say you have these functions: even :: Int ->

python: inheriting or composition

浪子不回头ぞ 提交于 2019-12-03 01:59:38
Let's say that I have class , that uses some functionality of dict . I used to composite a dict object inside and provide some access from the outside, but recently thought about simply inheriting dict and adding some attributes and methods that I might require. Is it a good way to go, or should I stick to composition? Inheritance is very often abused. Unless your class is meant to be used as a generic dictionary with extra functionality, I would say composition is the way to go. Saving forwarding calls is usually not a good enough reason for choosing inheritance. From the Design Pattern book:

association, aggregation and composition

爱⌒轻易说出口 提交于 2019-12-03 00:39:48
I'm dealing with this problem. I'm creating math problems, each one has response. For example. If my question is about the "result of 5x + 15 = 2?" , I'll be waiting just one answer (as integer). If my question is about says "give me the area and permiter of this shape" , I'll be waiting two answers (as doubles). In another one, I'll be waiting as response a string And anothers, I can have several answers or responses with various datatypes. My big question is. How would be the relation between the classes question and response. Also I was dealing if this should be an association, aggregation

Mixins vs composition in scala

一个人想着一个人 提交于 2019-12-03 00:16:28
问题 In java world (more precisely if you have no multiple inheritance/mixins) the rule of thumb is quite simple: "Favor object composition over class inheritance". I'd like to know if/how it is changed if you also consider mixins, especially in scala? Are mixins considered a way of multiple inheritance, or more class composition? Is there also a "Favor object composition over class composition" (or the other way around) guideline? I've seen quite some examples when people use (or abuse) mixins

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

本小妞迷上赌 提交于 2019-12-02 22:20:24
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: return self.contained.__getattr__(item) # redirection Background: I am trying to build a class ( Indicator )

How to manage discovery and composition as 2 separate concerns?

两盒软妹~` 提交于 2019-12-02 14:54:57
问题 I have set up an assembly catalog: private CompositionContainer GetContainer() { // initialize directory info ExtensionDirectory = new DirectoryInfo(settings.ExtensionsPath); // directory catalog var dirCatalog = new DirectoryCatalog(ExtensionDirectory.FullName); return new CompositionContainer(dirCatalog); } The contents of the container will load up all the assemblies in the directory as expected. I do not want to actually compose anything yet because I have constructors that will be

Difference between Composition and Dependency in class diagram?

北城余情 提交于 2019-12-02 11:48:16
I know, somebody had asked the same question about this case, but i still don't really get it, i need a specific answer. Thank you :D Since Gangnus did not correctly explain the meaning of composition, I'll have to do it. As explained by Gangnus, an aggregation is a special form of association with the intended meaning of a part-whole-relationship , but without a precise semantics (the UML spec says: "Precise semantics of shared aggregation varies by application area and modeler"). For instance, we can model an aggregation between the classes Car and Engine and between the classes Course and