-1

Does it only come handy in a situation "display a table of records with a check box and then process only the records that are selected".

Because I have searched and found every website displaying this in its example.

Is wrapper class used other than the above scenario as well?

3
  • 1
    You can use wrapper class to serialize/deserialize from JSON or so. Basically, wrapper class is still a class, so you can use it in any way that you want, which is applicable for classes
    – kurunve
    Commented Aug 7, 2017 at 9:24
  • 1
    Likely duplicate: Purpose of an inner class
    – Adrian Larson
    Commented Aug 7, 2017 at 12:58
  • 2
    @AdrianLarson Agree there is some overlap and I nearly linked to that post because I was going to mention that inner classes are a good way to go for these Wrapper classes. But this question seems to me about a pattern name (that gets overused IMHO) and not about inner classes.
    – Keith C
    Commented Aug 7, 2017 at 13:58

2 Answers 2

7

Looking at what Google has to say, Wrapper is an alternative name for both these patterns:

So the examples that focus on adding a checkbox might be better labelled as Decorator classes not Wrapper classes to be more specific and so clearer.

An example that say takes two different types of SObject and presents them in one table is acting as an Adapter (and is still a legitimate use of the term Wrapper).

IMHO the term should not be used where an object (or objects) are not being "wrapped". So, for example, the classes you might use for JSON serialization/deserialization would be better termed POAO (Plain Old Apex Object) following the term that started in Java with POJO, but unfortunately POAO is not a term that has gathered any momentum.

Another POAO example is where you need a method to return multiple values of differing types: the method returns an instance of the POAO class.

Keep in mind that Apex is a fairly/fully complete programming language that you can use to build any lines of code that help you solve your business problem. Patterns like Wrapper are just off the-shelf examples of pieces of code you can build with names that help developers quickly understand what is going on. There are lots of patterns to to be inspired by.

5

I believe not.

Wrapper class is a custom table in my belief. Though checkbox is a great example of it. But above it, I have been using Wrapper Classes beyond it's scope.

Suppose there is a Custom object you fetched using SOQL query, than you want to bind another field to it which is not statically stored in the DB but is dynamic in the code.

As the checkbox example states, one can bind the checkbox value(true/false) to the record.

A wrapper class is a custom object defined by programmer wherein he defines the wrapper class properties. Consider a custom object in salesforce, what do you have in it? fields right? different fields of different data types. Similarly wrapper class is a custom class which has different data types or properties as per requirement. We can wrap different objects types or any other types in a wrapper class.

2
  • I've used Wrapper Classes on the Event Object too, Polymorphic fields were the bane of my existence once upon a time. Wrapper classes helped me attach an Event with the Contact in the WhoId field in a Visualforce Page.
    – Dan Jones
    Commented Aug 7, 2017 at 9:44
  • +1, I would also like to add that, wrapper class plays a very handy role when you're writing a REST webservice in apex. You create your wrapper class as per JSON format which you'll be consuming. Commented Aug 7, 2017 at 9:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .