Saturday, February 9, 2013

OOPS Design Principles

What are Software Design Principles?

Software design principles represent a set of guidelines that helps us to avoid having a bad design. The design principles are associated to Robert Martin who gathered them in "Agile Software Development: Principles, Patterns, and Practices". According to Robert Martin there are 3 important characteristics of a bad design that should be avoided:
  • Rigidity - It is hard to change because every change affects too many other parts of the system.
  • Fragility - When you make a change, unexpected parts of the system break.
  • Immobility - It is hard to reuse in another application because it cannot be disentangled from the current application.

Open Close Principle

  • Software entities like classes, modules and functions should be open for extension but closed for modifications.
OPC is a generic principle. You can consider it when writing your classes to make sure that when you need to extend their behavior you don�t have to change the class but to extend it. The same principle can be applied for modules, packages, libraries. If you have a library containing a set of classes there are many reasons for which you�ll prefer to extend it without changing the code that was already written (backward compatibility, regression testing, �). This is why we have to make sure our modules follow Open Closed Principle.
When referring to the classes Open Close Principle can be ensured by use of Abstract Classes and concrete classes for implementing their behavior. This will enforce having Concrete Classes extending Abstract Classes instead of changing them. Some particular cases of this are Template Pattern and Strategy Pattern.

Dependency Inversion Principle

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions.
Dependency Inversion Principle states that we should decouple high level modules from low level modules, introducing an abstraction layer between the high level classes and low level classes. Further more it inverts the dependency: instead of writing our abstractions based on details, the we should write the details based on abstractions.
Dependency Inversion or Inversion of Control are better know terms referring to the way in which the dependencies are realized. In the classical way when a software module(class, framework, �) need some other module, it initializes and holds a direct reference to it. This will make the 2 modules tight coupled. In order to decouple them the first module will provide a hook(a property, parameter, �) and an external module controlling the dependencies will inject the reference to the second one.
By applying the Dependency Inversion the modules can be easily changed by other modules just changing the dependency module. Factories and Abstract Factories can be used as dependency frameworks, but there are specialized frameworks for that, known as Inversion of Control Container.

Interface Segregation Principle

  • Clients should not be forced to depend upon interfaces that they don't use.
This principle teaches us to take care how we write our interfaces. When we write our interfaces we should take care to add only methods that should be there. If we add methods that should not be there the classes implementing the interface will have to implement those methods as well. For example if we create an interface called Worker and add a method lunch break, all the workers will have to implement it. What if the worker is a robot?
As a conclusion Interfaces containing methods that are not specific to it are called polluted or fat interfaces. We should avoid them.

Single Responsibility Principle

  • A class should have only one reason to change.
In this context a responsibility is considered to be one reason to change. This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes. Each class will handle only one responsibility and on future if we need to make one change we are going to make it in the class which handle it. When we need to make a change in a class having more responsibilities the change might affect the other functionality of the classes.
Single Responsibility Principle was introduced Tom DeMarco in his book Structured Analysis and Systems Specification, 1979. Robert Martin reinterpreted the concept and defined the responsibility as a reason to change.

Liskov's Substitution Principle

  • Derived types must be completely substitutable for their base types.
This principle is just an extension of the Open Close Principle in terms of behavior meaning that we must make sure that new derived classes are extending the base classes without changing their behavior. The new derived classes should be able to replace the base classes without any change in the code.
Liskov's Substitution Principle was introduced by Barbara Liskov in a 1987 Conference on Object Oriented Programming Systems Languages and Applications, in Data abstraction and hierarchy

Saturday, January 5, 2013

Implicit Enhancement in SAP ABAP ( New Enhancement Framework ) with Tutorial

Introduction: Implicit enhancement is an enhancement option which is provided in new enhancement framework .
Requirement is to remove Reconstruct tab from WPDTC Transaction.:
There is something called transaction variant and screen variant .to solve this problem .But it has it’s own disadvantages like You can’t submit that Report using SUBMIT statement as it will not consider the changes done on screen using the variant.
Step 1:
Go to system status of transaction WPDTC and check the program name it is PDTC_REPORT.
Here you can see many Explicit enhancement (Source code plug-ins options).
To hide a screen element code can be done in AT SELECTION-SCREEN OUTPUT event.
There is a form named  pdtc_screen_output.
Step 2:
To see the Explicit enhancement option click on the spiral symbol on the top.
As we don’t have any explicit enhancement options the only possibility left is implicit enhancement.
To see the implicit Enhancement Go to menu path:
EDIT->Enhancement operations->Show implicit Enhancement Points.
As you know implicit enhancement options are provided at beginning and end of every method, function module and in subroutine.
Select the Enhancement mode as CODE.
The next pop up will show u the available Enhancement done in this Report, You can merge your enhancement in one of the available Enhancement or you can create your own. To create a new one click on the create icon.
Step 3:
Provide the Enhancement Implementation name and short text to it.
(Note: No need for composite enhancement implementation).
Click on the continue. It will again take you to the Select Enhancement popup. Select your created enhancement and continue.
Step 4:
Now you can see some space in standard code to put your own code.
As per the requirement we need to de-activate the last tab.
Go to layout of the screen 1000, and find the name of the third tab. To make your work easy the third tab name is : TABSTR_4.
Here is the simple code which can loop at the screen and deactivate the third tab.
loop at screen.
if screen-name = ‘TABSTR_4′.
screen-active = 0.
Put the above code inside the Enhancement and activate it.
Step 5:
Go to Transaction WPDTC and check that the third tab is invisible or not. ( if not don’t look puzzled just check whether you have followed the steps properly or not ).

SAP’s New Enhancement and Switch FrameWork

Get bored of using Old Customer Exits, User Exits ?????
Here comes The New Enhancement Framework to make it Easy for you to do customization in Standard SAP Applications.
This New Enhancement frame work provides all new enhancement technologies such as new business add-ins ( BAdIs ) and source code plug-ins.

Kernel BADIs
New BADIs are also Known as Kernel BADIs , These BADIs are object-oriented enhancement options (or plug-ins). These BAdIs are based on interfaces that may be implemented by classes that may then be transported.
You can see Kernel BADIs from the transaction code Se18:
(Note :Technically badis are link with the Enhancment Spot) check the Below screen shot for more details.
Source code plug-ins
In Source code plug-ins we have many options like Explicit Enhancement and Implicit Enhancement Developers (both SAP and non-SAP) may provide special hooks or portions in their code where enhancements may be applied. These are known as explicit enhancement options, and are mostly created by SAP at specific points of standard programs.
Explicit Enhancement:
In simple word explicit enhancement is provided by SAP, It is predefined location or plugs where you can put your code and enhance the standard. To see the explicit enhacement options in the object it requires to click on the spiral button which is given in the top tool bar.
Supported Enhancement Technologies
The Enhancement Framework supports a number of different enhancement technologies, including:
• Class Enhancements. Class enhancement lets you add new methods to a class. Moreover, you may also
add optional parameters to existing methods.
• Function Group Enhancements. You may add new parameters to a function module via function group
• Source Code Enhancements. These may be enhancement points in the source code where source
code plug-ins may be attached. These plug-ins contain the code that enhances the given program, and
are treated as an addition to the original code of the program in question. On the other hand, the code
with the enhancement section may be substituted with the source code plug-in code used.
This plug-ins in the source code is called as Enhancement SPOTS.
SPOTS can be categorised in :
  1. Enhancement POINTS
  2. Enhancement SECTION
In simple words When you want to add your extra logic over the existing one use POINTS while when you want to replace the existing one use SECTION. When a SPOT is static it is used for data declaration while Dynamic SPOTS are used for coding.
Enhancement POINTS and SECTION are looks like as mentioned Below:
ENHANCEMENT-POINT <name> SPOTS <spot1> [<spot2>] [STATIC] …
ENHANCEMENT-SECTION <name> SPOTS <spot1> [<spot2>] [STATIC]
Implicit Enhancement:
In last if nothing works , like you don’t have any Explicit enhancement or you don’t have any BADIs, Do implicit enhancement .
This is easiest enhancement you can ever find.
Implicit enhancement options are provided internally in the beginning and end of every subroutine , Function module and methods. Where you can put your own code. Check the article

Tutorial on Implicit Enhancement.
Switch Frame Work:
Each Enhancement package contains a set of business functions. Each “business function” are optionally implemented and activated. Switch frame work is something using which you can deactivate certain Business functionality depending on the client requirement. You can also assign switch to your implemented enhancements so if required you can de activate all of them in single go.

Webdynpro ABAP application to locate Employee’s location into google map

Webdynpro ABAP application to locate Employee’s location into google map.Follow the below 10 easy steps to create this application.
in last you can able to locate your employee on google map.
Step 1:
Go to Se80 create a Webdynpro comp name ZTEST_LOCATE_EMP.
Step 2:
Create a node into your main windo with name EMP. Dictionary strcuture as PA0006.Click on the button “ADD attriubute from structure” and select the perner from it.
Step 3:
Go to layout tab into MAIN view and create an input field with name EMP_ID.
Step 4:
Double click on the EMP_ID.
You can see the value property for it.Bind this to the context attribute you have created.
Step  5:
Create a Button with name LOCATE.
Step 6:
Double click on it, you can see On action propery. Just click on the create button right side of it.
Create a ACTION name LOCATE .no need to give any outbound plug name.
Step 7 :
When you create an action it automatically a Method name ONACTION<your acion name>.
Step 8.
Put the below code inside your method :
data : v_pernr type pa0006-pernr.
data : v_street type pa0006-STRAS.
data : v_city type pa0006-ORT01.
data : lr_node type ref to if_wd_context_node.
data:  lo_window_manager type ref to if_wd_window_manager.
data:  lo_api_component  type ref to if_wd_component.
data:  lo_window         type ref to if_wd_window.
data:  ld_url type string.
lr_node = wd_context->get_child_node( ‘EMP’ ).
lr_node->GET_ATTRIBUTE( exporting NAME = ‘PERNR’
select SINGLE STRAS ORT01 from pa0006 into (v_street,v_city) where
PERNR eq v_pernr.
lo_api_component  = wd_comp_controller->wd_get_api( ).
lo_window_manager = lo_api_component->get_window_manager( ).
*FINAL_URL = ‘’.
ld_url = FINAL_URL.
EXPORTING     URL                = ld_url
RECEIVING    WINDOW         = lo_window.
lo_window->open( ).

Step 9:
Create a Webdynpro application
Step 10:
Execute the application and give the perner which have the address data updated in PA0006. Take pernr 00000003 if you dont get any.

Call SAP Tcode using Webdynpro for ABAP

If you have a requirement to Call SAP standard Transaction code from Webbrowser in your webdynpro Application than check it out this article to know how can achive this.

Problem Description
It will help to call SAP Tcode from the Webbrowser using webdynpro for ABAP.
Solution Description
SAP Internet Transaction Server (ITS) – The ITS is the interface between the Internet and R/3  and is the runtime engine that links Web server to the R/3 Application server. So to display a standard tcode in your webdynpro application you can use ITS.
Setting needs to be Done.
Go to SE80 : In the top menu choose “Utilities(M)” –> “settings”. Right-click on the tab-arrow until you come to tab “Internet Transaction Server”.
Make sure that “Publish” tabe and check mark “Integrated ITS” is selected.
Start transaction “SE80″. Choose “Internet Service” from the first drop down box  Enter “SYSTEM” in the input field.  You will see the ITS service SYSTEM and its components in a tree structure. Right click on “SYSTEM” in the tree and select “Publish” –> “Complete Service” to publish the service.  once the publishing is completed the system will issue a message “Object published successfully; site INTERNAL”.
Repeat the previous step with “SHUFFLER” and “WEBGUI” instead of “SYSTEM” to publish both, too.
How to test it::
Enter Tcode SICF.
Select the path as : /sap/bc/gui/sap/its/webgui
Right click on webgui and click on “test” to execute the will open the sap screen in web browser.
Now it’s time for some coding. create LINK to URL or LINK TO ACTION according to requirement.  the code below is develop having scinario ::
Users are displyed creating link to userid coloumn. Whenever end user click on the any of the user id.  on_click method will be called wih open the user details in SU01D  Befor that get the path of the application server. you can find it in webdynpro application. doble clcik on webdynpro application in that you can find the URL  in Administrative data.
data: lr_node type ref to if_wd_context_node,
lt_event_properties type table of IF_MAIN=>element_event_properties,
ls_event_properties type IF_MAIN=>element_event_properties.

data:  lo_window_manager type ref to if_wd_window_manager.
data:  lo_api_component  type ref to if_wd_component.
data:  lo_window         type ref to if_wd_window.
data:  ld_url type string.

field-symbols: <l_value> type any.
*fill internal table
ls_event_properties-name = ‘COLUMN_ID’.
ls_event_properties-value = r_param->column.
append ls_event_properties to lt_event_properties.

ls_event_properties-name = ‘INDEX’.
ls_event_properties-value = r_param->index.
append ls_event_properties to lt_event_properties.

ls_event_properties-name = ‘ATTRIBUTE’.
ls_event_properties-value = r_param->attribute.
append ls_event_properties to lt_event_properties.

assign r_param->value->* to <l_value>.
ls_event_properties-name = ‘VALUE’.
ls_event_properties-value = <l_value>.
append ls_event_properties to lt_event_properties.

* navigate to context node EVENT_PROPERTIES
lr_node = wd_context->get_child_node( ‘EVENT_PROPERTIES’ ).

* bind internal table to context node
lr_node->bind_table( lt_event_properties ).

FINAL_URL = ‘http://<url>:portno/sap/bc/gui/sap/its/webgui?~TRANSACTION=SU01D%20USR02-BNAME=’.
CONCATENATE  FINAL_URL ls_event_properties-value INTO FINAL_URL.

lo_api_component  = wd_comp_controller->wd_get_api( ).
lo_window_manager = lo_api_component->get_window_manager( ).

ld_url = FINAL_URL.
EXPORTING     URL                = ld_url
RECEIVING    WINDOW         = lo_window.
lo_window->open( ).

Working with A BADI which does not contain proper import data

How can you use a BADI which does not contain  desire import or export data ?
Many time such condition occurs  when you find a perfect BADI for your requirement but As the BADI does not contains proper import data you cant use it.
Lets take an example for BADI Delivery_publish.
This BADI gets trigger when you do the POST Goods issue.
Now this BADI does not contain any input parameter.So ideally it is of no use if you want to perform a particular task on that delivery,
So what can be done!!!!!!!!!!!!
SAP has given a unique solution for this kind of problem.
See the below code:::

I can able to find the material Document No. Here..
Here we have use field symbols for reading a global variable EMKPF. Which is there in main program SAPMV50A.
(Note : It is not mandatory that you can access this program’s global variable only.You can access all the program’s global variable which are initialised in the current call sequence.
Note : if you don’t know the main program name you can find it from system table SYST and field CPROG.Now go to this program and check the top include where u will find the EMKPF table .So here is the guess that this variable might have the data initialize by Standard SAP before calling this BADI.u can see this variable in debugging mode by  (SAPMV50A)emkpf-MBLNR.
Note : Don’t ever change value As it may effect the standrad SAP codes.

MVC Architecture for Webdynpro for ABAP

To understand the Webdynpro for ABAP let us use the same methodology which we used during our school days, splitting the word “Webdynpro for ABAP”.
Web + dynpro + ABAP.
“ A Web application which can be accessed via internet which has a capability of dynamic UI  and build using ABAP is we called Webdynpro for ABAP Application.”
Now you will look puzzled if you already know about ITS.
Why we need it if we already have ITS?
ITS-> internet transaction server.
What ITS does is, it converts all the sap gui screen into HTML pages. This can be viewed from the browser. So it was SAP’s baby steps towards the web application. But if you Google it you will find many disadvantages.
You can refer the —————————-Article to know about how it works and can be used.
After ITS it was time for BSP (Business Server Pages)
If you are a JAVA Guy you will be surprised by name as we already have something called JSP (Java server pages).
BSP is also used to develop the web application it has its own tags which makes the UI element on the browser. You can code in Object oriented programming at technology end to implement the business logic.
If you compare BSP and Webdynpro for abap.
Of course winner will be Webdynpro for abap.
Webdynpro for ABAP is based on the MVC architecture. To explain it in simple way let’s start with
Traditional application
Browser directly accesses the required page.
Let’s say if user types
And the server will return the page to the user as response.

MVC application
Browser directly accesses Controller
So now Mr. Controller comes in between which help us to segregate the business logic and View part.
What is MVC
  • MVC is an architectural Design pattern
  • MVC separates the web application into three different modules
To better understand MVC architecture let take a simple example
Let’s say tomorrow is Your brother or sister’s birthday. So you decided to buy a cake and visit a Bakery Shop.
You request the baker to make a wonderful cake and the Baker collects all the ingredients to make the cake.
After collecting all the ingredients the baker decides on how the cake should look like and selects the
appropriate pan(it may be square, circle etc.).When the cake is ready the Baker responds i.e.
gives you back the cake to celebrate your sisters or brothers birthday. I hope this example makes you clear view on how MVC architecture works.
MVC is a collection of Model/View/Controller
Benefits of MVC
  • It keeps your business logic separate from your (HTML-based) views
  • Keeps your code clean and neat in one place
  • Represents the information and the data from the database
  • Validation of the data
  • Data presentation and user data
  • Rendering  models into one or more formats, such as HTML,XHTML, XML, or even Javascript
  • Dispatches requests and control flows
  • connects the model with the view
  • Process the data that comes from the model.
I think you would have got a clear idea about MVC which is the base of the WebDynpro For ABAP.