3

I have a behaviour that I'd like to use in different classes. Those classes are unrelated to each other (no inheritance). I'm using AS3, where multiple inheritance is not possible.

I could use an interface but then I'd have to rewrite the same implementation every time, which is basically what I'm doing now (but without the interface).

As en example I have many situations where I have an icon and a text label. In buttons, signs, etc. I'd like to centralise the alignment behaviour between the icon and the label.

What is the best OOP pattern for that?

  • If those classes are completely unrelated to each other, it is quite impossible to share implementation in type-safe manner. I believe text label and icon at least derive from some common class? – tia Jul 16 '13 at 3:10
  • Yes, the label and the icon are the same classes. But I want the behaviour in the classes that use composition with the icon and the label. For example the button class. – Pier Jul 16 '13 at 3:12
  • What I meant was, that all icons dervie from the same Image class, and all labels derive from the same Label class. Just in case it was not clear. – Pier Jul 16 '13 at 3:20
  • I think it's still not clear enough. Provide some of your current code snippet would help a lot. – tia Jul 16 '13 at 3:38
  • The question is just simply: how to use the same behaviour/implementation in different classes without using an interface. If that is still not clear enough I can write some code to show the problem. – Pier Jul 16 '13 at 3:43
3

The Strategy pattern is what I would use for this (encapsulating an algorithm). How the alignment is done is something that can be configured at runtime. There are also lots of different ways of aligning elements so you encapsulate them behind an interface. The example below is one way you could use strategy pattern with your Aligner class.

class Panel
{
    UIElement[] Children;
    IAligner Aligner;

    public SetAlignment(IAligner aligner)
    {
        this.Aligner = aligner;
    }

    public void Render()
    {
        Aligner.Align(Children)
        Display(Children)
    }
}

interface IAligner
{
    void Align(UIElement[] elements)
}

class LeftAligner : IAligner
class RightAligner: IAligner
class CenterAligner: IAligner
  • Thanks, that was what I found in my own answer. Since you posted the code and all I will give you the valid answer to you. Cheers. – Pier Jul 16 '13 at 4:02
2

Well it seems the best solution is simply using composition.

I found the answer here https://stackoverflow.com/questions/10714984/same-implementation-in-different-subclasses

In my example that would be having an Aligner class, and compositing an instance in my classes.

0

When the same behavior is needed in multiple classes, factor out that behavior into a new class. In this case, create a new LabeledIcon class containing an icon and a label, properly aligned. Create new instances of LabeledIcon as needed.

  • The label icon thing was just an example. What if I want to align 2 (or more) visual elements? 2 icons, 2 labels, 1 icon and 1 label, etc. – Pier Jul 16 '13 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.