Mixins vs composition in scala

A lot of the problems that people have with mix-ins can be averted in Scala if you only mix-in abstract traits into your class definitions, and then mix in the corresponding concrete traits at object instantiation time. For instance trait Locking{ // abstract locking trait, many possible definitions protected def lock(body: =>A):A } class MyService{ … Read more

Java – Method name collision in interface implementation

No, there is no way to implement the same method in two different ways in one class in Java. That can lead to many confusing situations, which is why Java has disallowed it. interface ISomething { void doSomething(); } interface ISomething2 { void doSomething(); } class Impl implements ISomething, ISomething2 { void doSomething() {} // … Read more

Haskell composition (.) vs F#’s pipe forward operator (|>)

In F# (|>) is important because of the left-to-right typechecking. For example: List.map (fun x -> x.Value) xs generally won’t typecheck, because even if the type of xs is known, the type of the argument x to the lambda isn’t known at the time the typechecker sees it, so it doesn’t know how to resolve … Read more

Implementation difference between Aggregation and Composition in Java

Composition final class Car { private final Engine engine; Car(EngineSpecs specs) { engine = new Engine(specs); } void move() { engine.work(); } } Aggregation final class Car { private Engine engine; void setEngine(Engine engine) { this.engine = engine; } void move() { if (engine != null) engine.work(); } } In the case of composition, the … Read more

Difference between Inheritance and Composition

They are absolutely different. Inheritance is an “is-a” relationship. Composition is a “has-a”. You do composition by having an instance of another class C as a field of your class, instead of extending C. A good example where composition would’ve been a lot better than inheritance is java.util.Stack, which currently extends java.util.Vector. This is now … Read more

Orchestration vs. Choreography

Basic technologies (such as XML, SOAP, WSDL) provide means to describe, locate, and invoke services as an entity in its own right. However, these technologies do not give a rich behavioral detail about the role of the service in more complex collaboration. This collaboration includes a sequence of activities and relationships between activities, which build … Read more

Prefer composition over inheritance?

Prefer composition over inheritance as it is more malleable / easy to modify later, but do not use a compose-always approach. With composition, it’s easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type. So … Read more