Factory method
Hey all, I am tying to learn factory method and working on an example from the Oreilly AS3 Design patterns book. I am attempting figure out how all the classes work together in this design pattern and I have a question I am hoping someone might be able clearify for me.
In the document class "Main" I have the following code in the constructor
// instantiate concrete shape creators
var unfilledShapeCreator:ShapeCreator = new UnfilledShapeCreator( );
// draw unfilled shapes NOTE: draw() is a method of the ShapeCreator class
unfilledShapeCreator.draw(UnfilledShapeCreator.CIRCLE, this.stage, 50, 75);
I get what is going on there, so I open up the 'UnfilledShapeCreator" class and it has the following:
package shapecreators
/* Concrete Creator class */
public class UnfilledShapeCreator extends ShapeCreator
public static const CIRCLE :uint = 0;
public static const SQUARE :uint = 1;
//implement the createShape factory method from the ShapeCreator Class.
override protected function createShape(cType:uint):ShapeWidget
if (cType == CIRCLE)
trace("Creating new circle shape");
return new CircleWidget( ); //Instantiates circleWidge Product Class
} else if (cType == SQUARE) {
trace("Creating new square shape");
return new SquareWidget( ); //Instantiates square Widge Product Class
} else {
throw new Error("Invalid kind of shape specified");
return null;
and the ShapeCreator class is as follows
package shapecreators
/* Defines the abstract interface for the creator classes */
import flash.display.DisplayObjectContainer;
import flash.errors.IllegalOperationError;
// ABSTRACT Class (should be subclassed and not instantiated)
public class ShapeCreator
public function draw(cType:uint, target:DisplayObjectContainer, xLoc:int, yLoc:int):void
var shape = this.createShape(cType);
shape.drawWidget( );
shape.setLoc(xLoc, yLoc); // set the x and y location
target.addChild(shape); // add the sprite to the display list
// ABSTRACT Method (must be overridden in a subclass)
protected function createShape(cType:uint):ShapeWidget
throw new IllegalOperationError("Abstract method: must be overridden in a subclass");
return null;
My question is: how does the cType parameter get passed into the createShape method? This method does not seem to get called from anywhere or any other class. I see how FilledShapeCreator.CIRCLE gets passed to the draw method in the ShapeCreator class, but the createShape method is never called like the draw method is.
Is it because createShape is called within the draw function?
I am quite sure it is a simple answer, but it has me completely lost.
Thanks,
Yes, it is called by super class in line:
var shape = this.createShape(cType);
By the way, I don't know where you got code but it is a pretty bad practice not type variables. Good compiler should not even allow you to compile. The line above should be:
var shape:ShapeWidget = this.createShape(cType);
Similar Messages
-
LOGO required in ALV top of page using factory method
Hi,
I am doing an ALV using factory method of class Cl_SALV_TABLE. Can any one help me about putting a LOGO on the top of page.
Thanks in advance.
AmitavaHi,
In START-OF-SELECTION.
put form to display header
like PERFORM build_header
gr_table->display( ).
then...
in FORM
FORM build_header.
lr_grid TYPE REF TO cl_salv_form_layout_grid,
lr_logo TYPE REF TO cl_salv_form_layout_logo,
create object lr_logo.
lr_logo->set_left_content( lr_grid ).
lr_logo->set_right_logo( 'LOGO_NAME' ).
* Set the element top_of_list
gr_table->set_top_of_list( lr_logo ).
ENDFORM.
thanx. -
How to add a new button in an ALV using factory method
im using factory method to creat an ALV
The reason why I'm doing this is because I want the ALV and the selection screen in the same screen like exemplified here http://help-abap.blogspot.com/2008/10/dispaly-alv-report-output-in-same.html
CALL METHOD cl_salv_table=>factory
EXPORTING
list_display = if_salv_c_bool_sap=>false
r_container = lo_cont
container_name = 'DOCK_CONT'
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = me->t_data.
The above code already uses every parameter that method as to offer.
Is it possible to add extra buttons to an ALV using that method?Hi Ann,
The reason you are not able to see any of the new columns as a option to select in your web service block is because when you have published that block, they were not present. Add these two new objects in your block and publish it again. You will be prompted for duplication content. Select the highlighted block for duplicate and now you can see the new added objects in the filter option. Update and this will overwrite your published block. Please note, web services do appear to behave weirdly when used with dashboards so I request you to please try it in a separate test report first.
Hope that helps.
Regards,
Tanisha -
Problem in factory method, how to pass arguments ?
Hello it's me again :)
here's the code :
package print;
import java.util.*;
import static print.Print.*;
interface Fact<T> {
T create(String n);;
T create ();
class PetColl {
public String toString() {
return getClass().getSimpleName();
static List<Fact<? extends Pet>> petSpecies=
new ArrayList<Fact<? extends Pet>>();
static {
// Collections.addAll() gives an "unchecked generic
// array creation ... for varargs parameter" warning.
petSpecies.add(new Cymric.Factory());
petSpecies.add(new EgyptianMau.Factory());
petSpecies.add(new Hamster.Factory());
petSpecies.add(new Manx.Factory());
petSpecies.add(new Mouse.Factory());
petSpecies.add(new Pug.Factory());
petSpecies.add(new Mutt.Factory());
petSpecies.add(new Rat.Factory());
private static Random rand = new Random(47);
public static Pet createRandom() {
int n = rand.nextInt(petSpecies.size());
return petSpecies.get(n).create();
public Pet[] createArray(int size) {
Pet[] result = new Pet[size];
for(int i = 0; i < size; i++)
result[i] = createRandom();
return result;
public ArrayList<Pet> arrayList(int size) {
ArrayList<Pet> result = new ArrayList<Pet>();
Collections.addAll(result, createArray(size));
return result;
class Individual implements Comparable<Individual> {
private static long counter = 0;
private final long id = counter++;
private String name;
public Individual(String name) { this.name = name; }
// ?name? is optional:
public Individual() {}
public String toString() {
return getClass().getSimpleName() +
(name == null ? "" : " " + name);
public long id() { return id; }
public boolean equals(Object o) {
return o instanceof Individual &&
id == ((Individual)o).id;
public int hashCode() {
int result = 17;
if(name != null)
result = 37 * result + name.hashCode();
result = 37 * result + (int)id;
return result;
public int compareTo(Individual arg) {
// Compare by class name first:
String first = getClass().getSimpleName();
String argFirst = arg.getClass().getSimpleName();
int firstCompare = first.compareTo(argFirst);
if(firstCompare != 0)
return firstCompare;
//second compare by name
if(name != null && arg.name != null) {
int secondCompare = name.compareTo(arg.name);
if(secondCompare != 0)
return secondCompare;
}//third compare by id
return (arg.id < id ? -1 : (arg.id == id ? 0 : 1));
class Pets {
public static final PetColl creator =
//new LiteralPetCreator();
new PetColl();
public static Pet randomPet() {
return creator.createRandom();
public static Pet[] createArray(int size) {
return creator.createArray(size);
public static ArrayList<Pet> arrayList(int size) {
return creator.arrayList(size);
class Person extends Individual {
String name;
public static class Factory implements Fact<Person>{
public Person create(String name){
Person.name=name;
return new Person(); }
public Person create(){return new Person();}
class Pet extends Individual {
class Dog extends Pet {
class Mutt extends Dog {
public static class Factory implements Fact<Mutt> {
public Mutt create(String name){return new Mutt(name);}
public Mutt create () {return new Mutt();}
class Pug extends Dog {
public static class Factory implements Fact<Pug> {
public Pug create(String name){return new Pug(name);}
public Pug create () {return new Pug();}
class Cat extends Pet {
class EgyptianMau extends Cat {
public static class Factory implements Fact<EgyptianMau> {
public EgyptianMau create(String name){return new EgyptianMau(name);}
public EgyptianMau create () {return new EgyptianMau();}
class Manx extends Cat {
public static class Factory implements Fact<Manx> {
public Manx create(String name){return new Manx(name);}
public Manx create () {return new Manx();}
class Cymric extends Manx {
public static class Factory implements Fact<Cymric> {
public Cymric create(String name){return new Cymric(name);}
public Cymric create () {return new Cymric();}
class Rodent extends Pet {
class Rat extends Rodent {
public static class Factory implements Fact<Rat> {
public Rat create(String name){return new Rat(name);}
public Rat create () {return new Rat();}
class Mouse extends Rodent {
public static class Factory implements Fact<Mouse> {
public Mouse create(String name){return new Mouse(name);}
public Mouse create () {return new Mouse();}
class Hamster extends Rodent {
public static class Factory implements Fact<Hamster> {
public Hamster create(String name){return new Hamster(name);}
public Hamster create () {return new Hamster();}
public class Test {
public static void main(String[] args) {
for(Pet p:Pets.creator.arrayList(25)){
PetCount.petC.count(p.getClass().getSimpleName());
print(p.getClass().getSimpleName());}
class PetCount {
static class PetCounter extends HashMap<String,Integer> {
public void count(String type) {
Integer quantity = get(type);
if(quantity == null)
put(type, 1);
else
put(type, quantity + 1);
public static PetCounter petC= new PetCounter();
}and here's my problem:
I'm trying to fill up list using factory method but in a fact that I want to have two constructors, I have a problem to set field name of objects of those classes. Is there any possibility to use in that way some factory method to create that list ?
In Person class I've tried to set it in factory method before creating an object, but as you know that option is only alvailable for static fields which i don't want to be static.I for one have no idea what you're asking, and what you seem to be saying doesn't make sense.
I'm trying to fill up list using factory method but in a fact that I want to have two constructors,Two constructors for what? The factory class? The classes that the factory instantiates?
I have a problem
to set field name of objects of those classes. Is there any possibility to use in that way some factory method to
create that list ?What?
In Person class I've tried to set it in factory method before creating an object, but as you know that option is only alvailable for static fields which i don't want to be static.That doesn't make any sense. A Factory can easily set fields in the objects it creates on the fly (not static). -
Meaning of factory method in abap objects
Hi,
Can anybody help me in understanding the meaning of factory method in abap object? what is the importance of this?
Regards
SudhansuHi 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 -
Trying to understand Objective C warning related to factory method
All,
I'm just beginning learning objective C (I am a C++ programmer by day), and I have a question about a warning I am seeing in my code. I found some really simple example code on the web that used the 'new' factory method to create an instance of an object. Here are the three files:
List.h
#import <objc/Object.h>
@interface List : Object // List is a subclass of the superclass Object
int list[100]; // These are instance variables.
int size;
/* Public methods */
- free;
- (int) addEntry: (int) num;
- print;
/* Private methods */
/* Other programs should not use these methods. */
- resetSize;
@end
List.m
#import "List.h"
@implementation List
+ new // factory method
self = [super new];
printf("inside new\n");
[self resetSize];
return self;
- free
return [super free];
- (int) addEntry: (int) num
list[size++] = num;
return size;
- print
int i;
printf("\n");
for (i = 0; i < size; ++i)
printf ("%i ", list);
return self; // Always return self
// if nothing else makes sense.
- resetSize
printf("Inside resetSize\n");
size = 0;
return self;
@end
main.m
#import <objc/Object.h>
#import "List.h" // Note the new commenting style.
main()
id list; // id is a new data type for objects.
list = [List new]; // create an instance of class List.
[list resetSize];
[list addEntry: 5]; // send a message to the object list
[list print];
[list addEntry: 6];
[list addEntry: 3];
[list print];
printf("\n");
[list free]; // get rid of object
Now, I know that the 'best' way to allocate and initialize an object is through [[List alloc] init], but I'm seeing a strange warning when I compile List.m, and I just want to understand why it's showing up. Here's the warning.
List.m: In function ‘+[List new]’:
List.m:9: warning: ‘List’ may not respond to ‘+resetSize’
List.m:9: warning: (Messages without a matching method signature
List.m:9: warning: will be assumed to return ‘id’ and accept
List.m:9: warning: ‘...’ as arguments.)
It looks like something is assuming that resetSize is a factory method rather than an instance method. The strange thing, though, is that the program works exactly like expected. resetSize is actually called from within the new method correctly.
Any ideas why I'm seeing the error, and how I should get rid of it?
Thanks,
EricThanks for clearing that up for me - it makes sense now. And sorry about the formatting problems - I guess forgetting the {code} tags make a pretty big difference
It's taking a little bit for my mind to switch from C++ syntax to Objective C, but I'm able to visualize the problem and the reason the code I found produced the error.
Thanks again,
Eric -
ALV GRID DISPLAY USING FACTORY METHODS
Hi all
I am using factory methods for my alv grid display.
I have a list of functionalities, for which i am not able to find a correct method..
1) Header of alv(with all the values of the selection-screen)
2)How to give text to a subtotal(ed) column, i.e. if i subtotal a qty field against a sorted field, i want to display ==> Nett Wt. = 123.00 (for first header entry) and so on for each header entry.
3)how to remove the zeroes from a quantity field?
4) Displaying the cells as blanks where data is 0( for quantity fields if i have a cell with zero value, it should be blank.)
5) double click on a cell to open a transaction with the cell's value.
Any help on this would be appreciated.
Points will be rewarded for sure...
Thanks & Regards
Ravish GargHello Ravish
Regarding the display of zero values as empty cells have a look at my <i>modified </i>sample report <b>ZUS_SDN_CL_SALV_TABLE_INTERACT</b>.
*& Report ZUS_SDN_CL_SALV_TABLE_INTERACT
REPORT zus_sdn_cl_salv_table_interact.
TYPE-POOLS: abap.
DATA:
gt_knb1 TYPE STANDARD TABLE OF knb1.
DATA:
go_table TYPE REF TO cl_salv_table,
go_events TYPE REF TO cl_salv_events_table.
* CLASS lcl_eventhandler DEFINITION
CLASS lcl_eventhandler DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
handle_double_click FOR EVENT
if_salv_events_actions_table~double_click
OF cl_salv_events_table
IMPORTING
row
column.
ENDCLASS. "lcl_eventhandler DEFINITION
* CLASS lcl_eventhandler IMPLEMENTATION
CLASS lcl_eventhandler IMPLEMENTATION.
METHOD handle_double_click.
* define local data
DATA:
lo_table TYPE REF TO cl_salv_table,
lt_orders TYPE STANDARD TABLE OF bapiorders,
ls_knb1 TYPE knb1.
READ TABLE gt_knb1 INTO ls_knb1 INDEX row.
IF ( syst-subrc = 0 ).
CALL FUNCTION 'BAPI_SALESORDER_GETLIST'
EXPORTING
customer_number = ls_knb1-kunnr
sales_organization = '1000'
* MATERIAL =
* DOCUMENT_DATE =
* DOCUMENT_DATE_TO =
* PURCHASE_ORDER =
* TRANSACTION_GROUP = 0
* PURCHASE_ORDER_NUMBER =
* IMPORTING
* RETURN =
TABLES
sales_orders = lt_orders.
* Create ALV grid instance
TRY.
CALL METHOD cl_salv_table=>factory
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
r_salv_table = lo_table
CHANGING
t_table = lt_orders.
CATCH cx_salv_msg .
ENDTRY.
lo_table->display( ).
** SET PARAMETER ID 'BUK' FIELD ls_knb1-bukrs.
** SET PARAMETER ID 'KUN' FIELD ls_knb1-kunnr.
** CALL TRANSACTION 'XD03' AND SKIP FIRST SCREEN.
ENDIF.
ENDMETHOD. "handle_double_click
ENDCLASS. "lcl_eventhandler IMPLEMENTATION
START-OF-SELECTION.
SELECT * FROM knb1 INTO TABLE gt_knb1
WHERE bukrs = '1000'.
* Create ALV grid instance
TRY.
CALL METHOD cl_salv_table=>factory
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
r_salv_table = go_table
CHANGING
t_table = gt_knb1.
CATCH cx_salv_msg .
ENDTRY.
* Create event instance
go_events = go_table->get_event( ).
* Set event handler
SET HANDLER:
lcl_eventhandler=>handle_double_click FOR go_events.
PERFORM modify_columns.
go_table->display( ).
END-OF-SELECTION.
*& Form MODIFY_COLUMNS
* text
* --> p1 text
* <-- p2 text
FORM modify_columns .
* define local data
DATA:
lt_dfies TYPE ddfields,
ls_dfies TYPE dfies,
lo_typedescr TYPE REF TO cl_abap_typedescr,
lo_strucdescr TYPE REF TO cl_abap_structdescr,
lo_tabledescr TYPE REF TO cl_abap_tabledescr,
lo_columns TYPE REF TO cl_salv_columns_table,
lo_column TYPE REF TO cl_salv_column.
lo_columns = go_table->get_columns( ).
lo_typedescr = cl_abap_typedescr=>describe_by_data( gt_knb1 ).
lo_tabledescr ?= lo_typedescr.
lo_strucdescr ?= lo_tabledescr->get_table_line_type( ).
lt_dfies = lo_strucdescr->get_ddic_field_list( ).
LOOP AT lt_dfies INTO ls_dfies.
lo_column = lo_columns->get_column( ls_dfies-fieldname ).
IF ( ls_dfies-keyflag = abap_true ).
CONTINUE.
ELSEIF ( ls_dfies-fieldname = 'WEBTR' ). " Bill of ex. limit
lo_column->set_zero( if_salv_c_bool_sap=>true ). " display zero
lo_column->set_zero( if_salv_c_bool_sap=>false ). " hide zero
ELSE.
lo_column->set_technical( if_salv_c_bool_sap=>true ). " hide col
ENDIF.
ENDLOOP.
ENDFORM. " MODIFY_COLUMNS
Regards
Uwe -
Factory method usage, creating the correct Image subclass
Hi, I created a factory method
public enum ImgType {
SINGLE, STRIP, SPRITE_SHEET
public static Image createImage(ImgType imgType) {
switch (imgType) {
case SINGLE:
return new MyImage1(0, 0);
case SPRITE_SHEET:
return new MyImage2("", 0, 0);
case STRIP:
return new MyImage3("", 0, 0);
default:
throw new IllegalArgumentException("The image type " + imgType + " is not recognized.");
}that creates different images based on the enum type provided.
and heres is a usage of that function
public BufferedImage loadAwtImage(ImageInputStream in, String imgName, ImageTypeFactory.ImgType imgType) throws IOException {
BufferedImage img = imgLoader.loadImage(in);
BufferedImage imgsubType = ImageTypeFactory.createImage(imgType); // Convert to real Image type
addAwtImg(imgName, imgsubType);
return imgsubType;
}In a test:
imgLib.loadAwtImage(imageStream, "cursor", ImageTypeFactory.ImgType.SPRITE_SHEET);Is it 'good' that in the test I can say(using the imgType enum) what sort of Image should be returned?
doesn't this expose the working of the class to the outside, on the other hand I don't know howto create the correct sub image based on an image loaded.
I use subImages to allow selection within the image, like the 3th 'tile' within an imageStrip returns just that cropped image.
Before I had List<Image> and List<List<Image>> and now I can just return Images in 1 method instead of 2,3 by using lists.
Edited by: stef569 on Dec 12, 2008 11:05 AMcreates specifications... :p
* load the BufferedImage from the ImageInputStream
* add it to the cache keyed by the imgName
* @return a BufferedImage, or a custom subclass based on the imgType
* @throws IOException when the img could not be loaded
public BufferedImage loadAwtImage(ImageInputStream in, String imgName, ImageTypeFactory.ImgType imgType) throws IOException I can test it, but the ImageTypeFactory.ImgType imgType parameter looks wrong to me.
if you see this in a class method would you change it/find a better way. -
Static factory methods, instead of constructor
Hi All,
why we use static factory methods, instead of constructor.
Apart from Singleton class , what is use of static factory methods ?
Thanks in Advance,
RishiOne reason for using factories is that they simplify creating instances of immutable classes that might otherwise have messy constructors with lots of arguments.
-
Factory method generateRandomCircle: "cannot resolve symbol"
I have written 2 classes: Point and Circle, which uses Point objects in order to create a Circle object. I have created a factory method for Point, which creates random Point objects and a for Circle, which does the same. "newRandomInstance" works fine for Point, but "generateRandomCircle" throws a "cannot resolve symbol" error. "main" is inside Point class. What's the problem?
Thanks
================================================
import java.io.*;
import java.lang.*;
import java.util.*;
public class Circle implements Cloneable
public static Circle generateRandomCircle()
int a= (int) (Math.random()* Short.MAX_VALUE);
int b= (int) (Math.random()* Short.MAX_VALUE);
Point p=new Point(a,b);
int r= (int) (Math.random()* Short.MAX_VALUE);
Circle c= new Circle(p,r);
return c;
===============================================
import java.io.*;
import java.lang.*;
import java.util.*;
public class Point implements Cloneable, Comparable
public static Point newRandomInstance()
int x = (int) (Math.random() * Short.MAX_VALUE);
int y = (int) (Math.random() * Short.MAX_VALUE);
return new Point(x,y);
public static void main (String[] args)
Circle cRandom= generateRandomCircle(); //doesn't work
System.out.println(cRandom.getCenter()+" "+ cRandom.getRadius());
Point randomP= newRandomInstance(); //works OK
randomP.printPoint();
}I tried "Circle cRandom=
Circle.generateRandomCircle(); " instead of "Circle
cRandom= generateRandomCircle();" and it worked. Why
did this happen?Because generateRandomCircle() exists in class Circle and not in class Point where your are trying to use it.
>
Function "newRandomInstance()" works either as
"Point.newRandomInstance()" or as
"newRandomInstance()", however. Why does this
controversy exist?No controversy! Your main() is contained within class Point and as such knows about everything that is declared in class Point() but nothing about what is in class Circle unless you tell it to look in class Circle. -
Design Patterns: 'Program to an interface, not an impl.' and Factory Method
Design Patterns: 'Program to an interface, not an implementation' and Factory Method.
Hi All,
I've 4 questions. And 1M thanks for your precious input.
1. OOAD steps:
Requirement-->Use Cases-->Analysis Classes-->Sequence Diagrams-->CRC-->other UML diagrams if needed--> Domain/Business Classes.
If we follow the rule 'Program to an interface, not an implementation',
would that imply NECESSARILY we should have another set of Interface Classes for our Domain Classes? i.e Interface_ClassX for ClassX_Impl.
2. If the point 1 is a MUST because of the rule 'Program to an interface, not an implementation',
ie we should have an Interface classe for every one Domain classe,
would that NECESSARILY imply we should have as many Factory Methods as they are Domain Classes to abstract the creation process?
Interface_ClassX X= Factory.GetClassX() ( return new ClassX_Impl)
Interface_ClassY Y= Factory.GetClassY() ( return new ClassY_Impl)
Interface_ClassZ Z= Factory.GetClassZ() ( return new ClassZ_Impl)
3. On the point 2, the underlying principle used is Factory Methods.
Now on the surface, what are other possible business and/or technical naming for such Factory Methods? I mean should we call it a kind of Business Facade?
4. Is the point 1 and point 2 considered to be the best practices?So the question here is whether we can predict having
more than one possible implementations which required
option c. Is this a dilema? I guess it's hard to
predict the future.Right. Hopefully it's fairly obvious while designing things and
deciding what objects are needed.
Now, if the Presentation Tier, says JSP, needs that
ClassNormal object. Would we still keep that line of
code a.
OR would we introduce an intermediate object to free
JSP from the direct creational aspect using new
keyword like the choice b. that you reject.
The point here is to reduce the direct coupling aspect
between the Presentation Tier and Business Tier. So
what would that intermediate object be?In that case, you have to ask yourself if there is a valid
need for reducing the coupling. If you simply make an intermediate
object, what keeps you from making an intermediate object to your
new intermediate object, ad infinitum.
That intermediate object could be a Facade pattern, or simply
an abstraction. We actually did that here, we began a massive
java project, and we abstracted away from Swing J-classes and created
our own "wrappers" that simply extended all the J-classes and we had
all our programmers develop using our wrappers instead of the Swing
classes. That allowed us to add some custom code, some temporary bug fixes, etc. Some of our classes were nothing more than "EPPasswordField extends JPasswordField" with nothing overridden. It does allow us a place to hook in and adjust or fix things if needed though. -
Back button issue in ALV Grid(Factory method)
Hi All,
I have displayed a report using factory method(CL_SALV_TABLE).In that i have added buttons in application bar too.So when clicking on refresh button ALV should display again with latest number of records,that is working fine but when clicking on back button it should go to selection screen instead of that it is going a step back and displaying previous list of grid and then back and then selection screen.
Can any one help me how to directly go to input selection screen instead of going step back...
Regards,
RamHI,
Use this syntax (CALL SELECTION-SCREEN 1000)...Try this one
regards,
balaji -
What is a Factory method and when to use this concept?
Could any one please describe what a factory method is and explain how I can use it by giving a simple example
A Factory Method (sometimes called a "virtual constructor") is a way to avoid hard coding what class is instantiated. Consider:
DataSource myDataSource = new DataSource();Now, if you want to use some other DataSource in your app, say, an XMLDataSource, then you get to change this code and all subsequent lines that use this, which can be a lot. If, however, you specified and interface for your DataSources, say, IDataSource, and you gave the DataSource class a static "create" method that would take some indication of what sort of DataSource to actually use, then you could write code like:
IDataSource myDataSource = DataSource.create(dataSourceString);And be able to pass in a dataSourceString describing what DataSource you wanted to use - and not have to recompile. Check out the Java Design Patterns site ( http://www.patterndepot.com/put/8/JavaPatterns.htm )
Make sense?
Lee -
Coloring an single field of an ALV using factory method
how can we color just a single field in ALV using factory method.
Hello Regi you may want to check this sample code, basically what you need is to use the lvc_s_colo structure and the set_color method of the class cl_salv_column_table.
METHOD set_colors.
*.....Color for COLUMN.....
DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table,
lo_col_tab TYPE REF TO cl_salv_column_table.
DATA: ls_color TYPE lvc_s_colo. " Colors strucutre
* get Columns object
lo_cols_tab = co_alv->get_columns( ).
INCLUDE <color>.
* Get ERDAT column & set the yellow Color fot it
TRY.
lo_col_tab ?= lo_cols_tab->get_column( 'ERDAT' ).
ls_color-col = col_total.
lo_col_tab->set_color( ls_color ).
CATCH cx_salv_not_found.
ENDTRY.
*.......Color for Specific Cell & Rows.................
* Applying color on the 3rd Row and Column AUART
* Applying color on the Entire 5th Row
DATA: lt_s_color TYPE lvc_t_scol,
ls_s_color TYPE lvc_s_scol,
la_vbak LIKE LINE OF ct_vbak,
l_count TYPE i.
LOOP AT ct_vbak INTO la_vbak.
l_count = l_count + 1.
CASE l_count.
* Apply RED color to the AUART Cell of the 3rd Column
WHEN 3.
ls_s_color-fname = 'AUART'.
ls_s_color-color-col = col_negative.
ls_s_color-color-int = 0.
ls_s_color-color-inv = 0.
APPEND ls_s_color TO lt_s_color.
CLEAR ls_s_color.
* Apply GREEN color to the entire row # 5
* For entire row, we don't pass the Fieldname
WHEN 5.
ls_s_color-color-col = col_positive.
ls_s_color-color-int = 0.
ls_s_color-color-inv = 0.
APPEND ls_s_color TO lt_s_color.
CLEAR ls_s_color.
ENDCASE.
* Modify that data back to the output table
la_vbak-t_color = lt_s_color.
MODIFY ct_vbak FROM la_vbak.
CLEAR la_vbak.
CLEAR lt_s_color.
ENDLOOP.
* We will set this COLOR table field name of the internal table to
* COLUMNS tab reference for the specific colors
TRY.
lo_cols_tab->set_color_column( 'T_COLOR' ).
CATCH cx_salv_data_error. "#EC NO_HANDLER
ENDTRY.
ENDMETHOD. "set_colors -
Coloring an individual cell in factory method
hi
i generate a report using factory method by passing one internal table ti it
now i want to color particular cells based on some condition.
color is red. any pointers...pls provide code if you have.
thnks in advance.hi,
check this..
*& Report Y_TEST
REPORT y_test.
TABLES: mara.
DATA: str1 TYPE string,
str2 TYPE string,
str3 TYPE string,
str4 TYPE lvc_title,
num TYPE i,
c1(5) TYPE c.
DATA: value1 TYPE salv_s_layout,
key TYPE salv_s_layout_key VALUE sy-repid,
tool TYPE lvc_tip,
col TYPE lvc_s_colo,
cell TYPE salv_t_cell.
DATA: gr_table TYPE REF TO cl_salv_table,
gr_functions TYPE REF TO cl_salv_functions,
gr_layout TYPE REF TO cl_salv_layout,
gr_display TYPE REF TO cl_salv_display_settings,
gr_column TYPE REF TO cl_salv_column_table,
gr_columns TYPE REF TO cl_salv_columns_table,
gr_aggregations TYPE REF TO cl_salv_aggregations,
gr_aggregation TYPE REF TO cl_salv_aggregation,
gr_selections TYPE REF TO cl_salv_selections,
gr_sorts TYPE REF TO cl_salv_sorts.
DATA: it_mseg TYPE STANDARD TABLE OF mseg WITH HEADER LINE.
SELECT * FROM mseg INTO CORRESPONDING FIELDS OF TABLE it_mseg WHERE werks = 'KWWK' AND matnr NE ' '.
*--ALV display-------------------------------------------------*
cl_salv_table=>factory(
IMPORTING r_salv_table = gr_table
CHANGING t_table = it_mseg[] ). "gr_table Instance created
*---Setting the ALV toolbar-------------------------------------*
gr_functions = gr_table->get_functions( ). "gr_functions instance created
gr_functions->set_all( abap_true ).
*---Setting the Layout------------------------------------------*
gr_layout = gr_table->get_layout( ).
*gr_layout->set_key( key ).
gr_layout->set_save_restriction( ).
*---Setting the header------------------------------------------*
gr_display = gr_table->get_display_settings( ).
str1 = 'Display of MSEG Table'.
str2 = '-'.
DESCRIBE TABLE it_mseg LINES num.
c1 = num.
str3 = ' Rows are Selected'.
CONCATENATE str1 str2 c1 str3 INTO str4.
gr_display->set_list_header( str4 ).
*---Zebra-------------------------------------------------------*
gr_display->set_striped_pattern( 'X' ).
*---Deactivating horizontal line--------------------------------*
gr_display->set_horizontal_lines( ' ' ).
*---Deactivating vertical line----------------------------------*
*gr_display->set_vertical_lines( ' ' ).
*---Set color to Column-----------------------------------------*
*col-col = 3.
*col-int = 1.
*col-inv = 1.
*gr_columns = gr_table->get_columns( ).
*gr_column ?= gr_columns->get_column( 'LAEDA' ).
*gr_column->set_color( col ).
*col-col = 5.
*col-int = 1.
*col-inv = 1.
*gr_columns = gr_table->get_columns( ).
*gr_column ?= gr_columns->get_column( 'ERNAM' ).
*gr_column->set_color( col ).
col-col = 4.
col-int = 0.
col-inv = 1.
gr_columns = gr_table->get_columns( ).
gr_column ?= gr_columns->get_column( 'BWART' ).
gr_column->set_color( col ).
gr_column->set_alignment( '3' ).
gr_column->set_tooltip( 'helllooooo' ).
*----Set key column----------------------------------------------*
gr_columns->set_key_fixation( 'X' ).
*----Setting total-----------------------------------------------*
gr_aggregations = gr_table->get_aggregations( ).
gr_aggregation = gr_aggregations->add_aggregation( 'MENGE' ).
gr_aggregation->set( if_salv_c_aggregation=>total ).
gr_aggregations = gr_table->get_aggregations( ).
gr_aggregation = gr_aggregations->add_aggregation( 'DMBTR' ).
gr_aggregation->set( if_salv_c_aggregation=>total ).
*----Selection---------------------------------------------------*
gr_selections = gr_table->get_selections( ).
gr_selections->set_selection_mode( cl_salv_selections=>if_salv_c_selection_mode~cell ).
*----Setting of sort & subtotal column---------------------------------------*
gr_sorts = gr_table->get_sorts( ).
gr_sorts->add_sort( columnname = 'MATNR' subtotal = 'X').
*gr_sorts->get_subtotals( ).
gr_table->display( ).
Arunima
Maybe you are looking for
-
ITunes 10 - Crashes when importing CD or from RIP'd file/folder
Windows Vista. There is no error, iTunes stops working and Microsoft Windows Message pops-up ITunes has stopped working A problems caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.
-
Speed capped at 0.5 mbps?
Hi guys. I live in a rural area around 4 miles from the nearest exchange. I have been with BT Broadband for several years and have always had a speed of around 0.5 megs and my intenet never looses sync (it may be worth noting i am on bt option 2, up
-
Itunes 10.5.1 doesn't open with windows 7 64 bit
i have just purchased a new HP laptop with windows 7 64bit and have installed itunes. all the files have installed correctly but the program doesnt open. any ideas ?
-
Export BLOB to file system folder in Oracle 8i
Hi Folks, I want export the doc and zip files from oracle column BLOB to folder on my desktop / network . Can anyone please suggest the easiest way . Thanks
-
Workflow Errors in HCM Processes and Forms
Team, we are facing one issue related to HCM Process and Forms. Recently we have upgraded our HR Package after which workflows used in HCM Forms stopped working though user who is initiating the process has SAP_ALL. We have noticed that workflow is t