3

I just listened to Software engineering Radio podcast on components. Their description of components were very vague, they made them sound like lower level versions of functions. I am trying to figure out what components mean in the context of the composite pattern.

  1. What is the difference in purpose between a function and a component?

  2. What does a component have to do with the composite pattern?

  3. Can you describe what a component is by using a funny metaphor? For example, explain how creating a component like baking an apple pie.

  • 1
    Component is an abstraction layer, that consist of bunch classes. The point of a component is to solve particular kind of "global" task. Image you have powerful Image uploader via HTTP - that component will include sub classes that deal with image resizing, fixing problems with UTF-8 filenames, and file validation rules. All of this is a part of a component. The function is just an abstraction of one task (usually) – Yang Sep 7 '14 at 5:41
2

There is no clear notion of the term »component«. You could use it in many different ways. But if you go to the root of the word (in terms of linguistics) you have »componere«, which is itself a »compositum« of »cum« (with) and »ponere« (put). So if you put something together you have a »composition« of individual parts.

There are two main aspects for components:

  • Composition »put together«
  • Compartmentalization »independent things«

So a software component is a unit or part of software, which in itself could be used independent from the actual composition - but on the other hand is used to build a larger system. Ideally you have a software in which each component is orthogonal, which means, no component is dependent on another component and they do each their job, where no functionality is duplicated (a form of DRY).

In most cases you could substitute »component« with »part«.

I am trying to figure out what components mean in the context of the composite pattern.

The point of the composite pattern is to put independent parts under a unique interface. You have the base component as a contract for each part to comply with. You have a leaf which is the part and you have the composite, which contains a collection of those parts.

1) What is the difference in purpose between a function and a component?

A component is the subject / carrier of a function ( in OO-terms). On the other hand, if you take functional languages, I think it would be okay, to speak of functions as components in a functional composition. In a broader sense, you could speak of a component in a whole, which represents one function: e.g. »a printing component« which stands for the function to be able to print.

2) What does a component have to do with the composite pattern?

See above: Composite is about putting together components.

3) Can you describe what a component is by using a funny metaphor? For example, explain how creating a component like baking an apple pie.

  • One way to put it: A component is like an ingredient for a meal.
  • Another way to put it: A component is a player in a soccer game. Each of them is different and has to play another role in the game, but together, they are a team and play soccer.
1

I really don't think that the podcast's discussion of "components" is directly in the context of the composite pattern. They are talking about component-based software engineering. I suppose this is one of the pitfalls of the ambiguity of software development terminology.

But, putting that aside to discuss the composite pattern, there's a nice explanation here, in case you haven't read it.

Basically, in the composite pattern, a Component class is simply a class that provides some abstract functionality. In most real-world use cases, it's a class with an interface to output data, like a post or a message. In ruby, I'll implement a Message class:

class Message

    def initialize
        @message = ''
    end

    def display
        #abstract
    end

end

In the composite pattern, a Leaf is a single entity, that generally displays just one unit of information. In our example, a Single message will simply output the string that it is instantiated with:

class Single < Message

    def initialize(message)
        @message = message if message.is_a?(String)
    end

    def display
        puts @message
    end

end

In the composite pattern, a Composite is an object that can contain other components, whether those components are themselves Composite or simply single Leaves. For our example, I'll create a Multiple message class:

class Multiple < Message

    def initialize
        @message = []
    end

     def display

        @message.each do |message|

            message.display

        end

    end

    def add (message)
        message = Single.new(message) if message.is_a?(String)
        @message << message if message.is_a?(Message)
    end

end

The end result is that Multiple message objects can contain Single messages as well as nested Multiple messages

m1 = Multiple.new
m1.add 'Hello!'
m1.add 'Hola!'
m1.add 'Bienvenue!'

m2 = Multiple.new
m2.add 'Salut!'
m2.add 'Jumbo!'

m1.add m2

m1.display

This code will output:

Hello!
Hola!
Bienvenue!
Salut!
Jumbo!

In metaphor, I suppose a Component could be like an executive in a business. Any given executive may work alone, or be the head of a division or department. But, when you go around and ask how much money each executive is going to need for the upcoming fiscal year, you don't care which one they are. If they work alone, they will simply tell you what their salary is/should be. If they are the head of a division, they will add on the money needed by all of the employees working under them. Those employees may or may not also be the heads of sub-devisions, in which case the same process would happen recursively.

Hope that helps!

0

I think, in a nutshell:


A component has state information.

like:

  • controls
  • connection classes

A function doesn't have state information.

like:

  • matrix calculations
  • string parsers

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.