Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

meaning of factory method in abap objects

Former Member
0 Kudos

Hi,

Can anybody help me in understanding the meaning of factory method in abap object? what is the importance of this?

Regards

Sudhansu

4 REPLIES 4

uwe_schieferstein
Active Contributor
0 Kudos

Hello Sudhansu

Factory methods (or factory classes) are used if you (as developer of the factory method/class) want to keep control about how instances of classes are created.

In addition, factory methods are used if groups of classes have to be instantiated at the same time and you do not want to burden this complexity on to the developer using this classes.

Examples for factory methods and classes are:

- CL_PT_EMPLOYEE=>GET_EMPLOYEE (employee instance)

- CF_RECA_MESSAGE_LIST=>CREATE (message handler)

- CL_WB2_DOC_FACTORY=>CREATE_DOC (create different types of documents -> sales order, purchase order, etc)

Please note that factory methods are a <b>design pattern</b> and, by no means, specific to ABAP-OO.

I can highly recommend the book

<a href="http://www.oreilly.com/catalog/hfdesignpat/">Head First Design Patterns</a>

in order to learn the basics of design patterns.

Regards

Uwe

0 Kudos

Hi Uwe ,

Thanks a lot for this useful information.

Regards

Sudhansu

0 Kudos

Hi Uwe,

The head first Design Patters book that was suggested by you, I think the examples and code snippets are given with perspective of Java Programming. Me being a SAP ABAP consultant learning OO-ABAP and working on it would it be helpful ?

Regards,

Krishna.

0 Kudos

Hi Krish and Sudhansu,

Design patterns are solutions which are already verified and known by many developers. That is why it is worth to use them. There is no need to reinvent the wheel in many cases.

I would recommend book which is placed in the ABAP world:

http://www.sap-press.com/products/Design-Patterns-in-Object%252dOriented-ABAP-(2nd-Edition).html

Although Java language has intuitive syntax, there are some special things in ABAP development so it is better to check solutions adjusted for ABAP editor.

The most common usage of factory pattern is to simplify object creation.

- By one method call you provide required parameters and do all initializations, including dependent objects.

- Class can have many factory methods, if you want to provide more ways of initialization.

- Factory method is usually static in the class and they return initialized instance of object for this class.

- There is naming convention to start factory method name with "create" - easy to recognize pattern.

- If you set property of class to "private instantiation" then you force to use factory method for object creation. In this way it is really simple to find all places where object are created with given set of input parameters - find references of factory method.

Factory pattern becomes even more powerful if we add inheritance. Factory method returns basic object (like ZCL_VEHICLE) but its implementation can return different subclass instance, depending on input parameter (ZCL_CAR, ZCL_TRAIN etc). Each instance can implement differently behavior (methods implementation), but these are object oriented techniques.

Regards,

Adam