composition

java inheritance versus composition (implementing a stack)

℡╲_俬逩灬. 提交于 2019-12-05 21:56:52
I am trying to implement a Stack in java (using the list interface: Interface List ). I want to implement it two different ways: using composition and inheritance. For inheritance, so far I have: import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class StackInheritance implements List { //implement list methods } For composition, I have: import java.util.List; public abstract class StackComposition implements List { // implement some standard methods } public class StackViaList extends StackComposition { // implement methods

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

一曲冷凌霜 提交于 2019-12-05 21:17:28
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 of first and second structs without nesting. Here is an example: https://play.golang.org/p/jbJykip7pw

interface vs composition

淺唱寂寞╮ 提交于 2019-12-05 17:30:11
I think I understand the difference between interface and abstract. Abstract sets default behavior and in cases of pure abstract, behavior needs to be set by derived class. Interface is a take what you need without the overhead from a base class. So what is the advantage of interface over composition? The only advantage I can think is use of protected fields in the base class. What am I missing? An interface defines how you will be used. You inherit in order to be reused. This means you want to fit into some framework. If you don't need to fit into a framework, even one of your own making, don

Where and why is identity function useful?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 16:44:33
问题 I understand why function composition is important. It allows building large and complex functions from small and simple ones. val f: A => B = ... val g: B => C = ... val h = f andThen g; // compose f and g This composition conforms to identity and associativity laws. Associativity is useful because it allows grouping f1 andThen f2 andThen f3 andThen f4 ... in any order. Now I wonder why identity is useful. def f[T](t:T) = t // identity function val g: A => B = ... // just any function g

Is it possible to parameterize a MEF import?

我的梦境 提交于 2019-12-05 13:52:08
I am relatively new to MEF so I don't fully understand the capabilities. I'm trying to achieve something similar to Unity's InjectionMember. Let's say I have a class that imports MEF parts. For the sake of simplicity, let's take the following class as an example of the exported part. [Export] [PartCreationPolicy(CreationPolicy.NonShared)] public class Logger { public string Category { get; set; } public void Write(string text) { } } public class MyViewModel { [Import] public Logger Log { get; set; } } Now what I'm trying to figure out is if it's possible to specify a value for the Category

is there a way to access an object within an object?

旧城冷巷雨未停 提交于 2019-12-05 13:51:42
I'm practicing java by building a simple directory. I have 4 Classes. These are: Person Address Contact testClass I have already finished creating this system and it works the way I want it. I did this by making 3 different arrays for the Person, Address and Contact. To link the Person, Address and Contact together I place them on there respective array with the same index number. (Not literally linking them together, just a way to know which address or contact to access when editing a person). But now, I want to optimize it. I want to create a single HashMap to hold a person with the address

OO Design Patterns with Perl

ⅰ亾dé卋堺 提交于 2019-12-05 12:44:00
I am currently planning the design for a new system I will need to code that interacts with a back-end API. I was contemplating object composition and inheritance and decided that the most correct procedure in my situation would be to go with composition over inheritance as my objects have a "has a" relationship to one another and not an "is a". I find now though that because some objects are reliant on other, there may be cases were "object A" has an attribute which is "object B" and an attribute "object C" - however "object B" also has an attribute "object C". In hope that this analogy would

Inheritance vs Composition [duplicate]

百般思念 提交于 2019-12-05 05:04:20
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Prefer composition over inheritance? I wonder, why (or in which cases) should one consider inheritance instead of composition when there are so much cons of it: if we implement/override a method in a subclass which calls the method of the superclass, there's no guarantee that another version of our superclass (maybe some library) won't break our code if there will appear a new method in the superclass with the

How to write without Do notation

只愿长相守 提交于 2019-12-05 02:06:35
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,""):[]) -> return (Just x) _ -> return Nothing I wrote this function with the intent of creating composable

Tacit function composition in Haskell

柔情痞子 提交于 2019-12-04 23:53:21
Say I have a mean function defined like so: mean xs = sum xs / (fromIntegral $ length xs) but I want it in some tacit form, like this: mean = sum / (fromIntegral . length) Is there a built-in Haskell way to do something along these lines without having to build up my own tacit function (something like this): tacit :: (a -> b -> c) -> (d -> a) -> (d -> b) -> d -> c tacit a b c i = a (b i) (c i) In this form, the function looks like this: mean = tacit (/) sum (fromIntegral . length) but it feels like there might be a way to avoid having to use an explicit function such as this. I'm just