2020 Sep 17 8:50 AM
2020 Sep 17 8:50 AM
Welcome and Thank you for visiting SAP Community to get answers to your questions. Please add more details to your question, e.g. you can also add a screenshot. With that, you can reach a broader range of experts to get your question answered. I also recommend to do this tutorial https://developers.sap.com/tutorials/community-qa.html
The more details you provide, the more likely it is that members will be able to assist you.
Regards, Svea
SAP Community moderator
2020 Sep 17 8:53 AM
Hello yashaswini33
Whenever you want to create an instance without really specifying the exact class (factory method contains the logic for choosing the correct class).
You can read more about it on the Internet, example article from Wikipedia: https://en.wikipedia.org/wiki/Factory_method_pattern
Kind regards,2020 Sep 19 7:40 AM
2020 Sep 17 9:39 AM
Hello!
Well the thing about OO ABAP and "normal" OO is that they are the same! ABAP may not have some features that other OO languages have, like method overloading, but overall, all OO principles and patterns are applied the same way, or at least for the same reasons.So whenever you want to encapsulate/abstract/hide implementation details of how an object is created, you can use one of the factory patterns (factory, factory method, abstract factory).
Consider a Class that needs another class in its constructor,
new ItemOrder( new noDiscountStrategy( ) )
You can just have a factory method on ItemOrder
data(lo_item) = ItemOrder=>newNoDiscountItem( ).
Imagine a situation where you need to create the same class over and over, all over the place. Now consider you need to change how this class is created. Factories usually helps in this situation too.
You can also use factories to determine what class needs to be created and together with good OO, they become a good place to isolate changes:
Consider a code like the one below (but with a 1000 lines plus). The idea is that any conditional logic can be encapsulated, and by sharing a common interface, be decided within a factory.
some code...
case.
when time > 06:00 and < 10: 00.
append value #( name = 'toast' price = 3.00 ) to menu.
append value #( name = 'orange juice' price = 2.00 ) to menu.
append value #( name = 'eggs special' price = 3.50 ) to menu.
when time >= 10:00 and < 14:00.
append value #( name = 'pizza' price = 7.00 ) to menu.
append value #( name = 'burguer' price = 7.50 ) to menu.
append value #( name = 'tikka massala' price = 6.50 ) to menu.
when time > 14:00 and < 17:00.
...
when time >= 17:00 and < 18:00.
...
when time >= 19:00 and < 23: 00.
...
end case.
Well...all that could become:
data(currentTimeMenu) = MenuFactory=>build_for( sy-uzeit ).
data(menu) = currentTimeMenu->get_items( ).
By having the factory
interface menu
method get_items
returning value(rt_items) type menu_items.
endinterface.
class menuFactory.
...
method buid_for.
case.
when time > 06:00 and < 10: 00.
ro_menu = new MorningMenu( ).
when time >= 10:00 and < 14:00.
ro_menu = new LunchMenu( ).
when time > 14:00 and < 17:00.
ro_menu = new AfternoonMenu( ).
when time >= 17:00 and < 18:00.
ro_menu = new HappyHourMenu( ).
when time >= 18:00 and < 23: 00.
ro_menu = new NightMenu( ).
end case.
endmethod.
endclass.
Look how much nicer the client class became, and how it is more clear what each time represents too.
Regards,
Felipe
2020 Sep 19 7:41 AM
2020 Sep 19 6:03 AM
Hi!
You can use factory pattern in following sistuations.
1. A class cannot anticipate the type of objects it needs to create beforehand.
2. A class requires its subclasses to specify the objects it creates.
3. You want to localize the logic to instantiate a complex object.
bellow are the links : https://blogs.sap.com/2012/02/20/factory-pattern-in-abap-oo/#:~:text=The%20factory%2Dpattern%20is%20....
https://www.sitepoint.com/understanding-the-factory-method-design-pattern/
you can go through these links for better understanding
Regards,
Varun
2020 Sep 19 7:41 AM