example of factory pattern in java jdk - factory-pattern

At below link:
Examples of GoF Design Patterns in Java's core libraries
that java.lang.Object#toString() is example of factory pattern.
I am confused about this.
What i have understood till now is that factory pattern is used to create objects .
Can someone explain it more clearly?

In essence, the factory pattern is an abstract class or interface that specifies a method to produce something. Then you have an implementation and from that implementation, you can build that something.
Here we have:
Abstract class or interface: Object
Build method: toString()
Implementation: Any java object
Product: A string
So yeah, it is a bit of a strange example and there are better ones out there, but it does fit the model for a factory.

Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class. Typically, getInstance() method is present which returns different type of objects based on inputs provided.
To understand it better, You can refer this example, in Java API- calender class returns different calender object based on inputs :
static Calendar getInstance()
Gets a calendar using the default time zone and locale.
static Calendar getInstance(Locale aLocale)
Gets a calendar using the default time zone and specified locale.
static Calendar getInstance(TimeZone zone)
Gets a calendar using the specified time zone and default locale.
static Calendar getInstance(TimeZone zone, Locale aLocale)
Gets a calendar with the specified time zone and locale.
Examples of Factory pattern used in JDK:
java.util.Calendar, ResourceBundle and NumberFormat getInstance() methods

Related

What is the meaning of the expression A('pk') in Django Tables 2?

I'm trying to setup a LinkColumn and I've seen in the examples that a the args parameter has usually the form args=[A('pk')]. I'm wondering what is the meaning of the A().
From the documentation of django-tables, A is the Accessor Class.
A string describing a path from one object to another via attribute/index accesses. For convenience, the class has an alias A to allow for more concise code.
Relations are separated by a . character.
So basically you are using the primary key in this example to access the objects.
From django-tables2 source code
class Accessor(str):
'''
A string describing a path from one object to another via attribute/index
accesses. For convenience, the class has an alias `.A` to allow for more concise code.
Relations are separated by a ``.`` character.
'''

Why are converter methods of Power BI visuals public instead of private?

Working on making custom IVisual implementations; The recommended pattern includes a converter method, which converts the dataview into the visual's own view model. I am curious why the converter is declared as public instead of private.
In the Hello World example it is coded here, and explained here.
public static converter(dataView: DataView): HelloViewModel {
...
}
In code, converter seems to only be accessed within the class itself, so it is naturally a private method. Moreover, making it public necessitates also exporting its type, HelloViewModel, which also seems to only be used internally.
Possible answer: There are a handful of built-in visuals that ship with their own test classes, like treemapTests.ts for treemap.ts. These classes also test the functionality of converter methods, and this is the only place where I have seen converter called from outside of its class.
Is this the entire reason converter methods have been made public, or is there a plan to make them a formal part of the IVisual interface in the future, or is there something else going on?
Great question :) No reason. Originally there was talk to change the update options to contain the visual's vm instead of the dataview. And power BI would use the public converter method to pass in the right vm. This way other sites hosting power bi visuals wouldn't need any dependency on dateview. I don't think we'll be going that route though.

How to get the full name of a Sitecore DMS rule?

I'm using Sitecore. I want to get the full name/description of a DMS rule in programcode by Sitecore ID, for example: "Where the DayOfWeek has a value that is equal to Tuesday".
Who knows how to do this?
Thanks a lot.
Jordy
I don't know of a simple way, but the class responsible for rendering the rule text is Sitecore.Shell.Applications.Rules.RulesRenderer in Sitecore.Client.dll.
Its constructor accepts the XML from a rules field and you call the Render method, passing in a prepared HtmlTexteWriter. It also has a bunch of fairly self-explanatory private methods like RenderRule, RenderCondition etc.
I'm sure if you decompile that class you can pick out the bits you need.

when should I use BOM to XOM mapping ExtenderName in ILOG Jrules

In the rule studio BOM editor , there is BOM to XOM mapping window and it asks for execution name and extender name. I can write java code in a separate project and import it as BOM. So what is the purpose of this extender mechanism ? As always IBM doc says how to do it. But doesn't tell why !
As far as I remember the first displayed is execution:
It is used when you create a "Virtual Member" meaning in Ilog terminology: a method or attribute or class which doesn't rely on a XOM.
Remember that you can create an empty BOM or you can add a method or attribute in a BOM class based on a XOM
The easiest example is "age" NO database will ever store such field but you could had a piece a logic in a "virtual attribute or method" in order to do the comparison between Date of birth and today.
If you create a class from scratch (not an attribute or method) a kind of "Virtual Object" you still need to tell JRules how to consider this Object at runtime.
So you use this field to tell JRules, here is a virtual class based on no XOM but at execution time use it as an java.lang.Object
I never used this field with any other Class than java.lang.Object
Does it make sense?
Second one is really like "extends" in pure java. Never used it... No need.
Hope it helps
To complete Damien answer :
The "execution name" field is also used when your bom class don't have the same name as the xom class.
From the Jrules 7.0.2 doc :
For example, in your BOM, there is a business class named ShoppingCart. You need to map this business class to an execution class called Cart in the XOM. To do the mapping, select the class ShoppingCart, and in the BOM Editor specify Cart as the Execution name.

Scala template accessing model object properties in Play 2.0

I have a simple question regarding accessing member variables of a model object.
I have the following model objects:
#Entity
public class Person extends Model{
#Id
public Long id;
public String name;
}
#Entity
public class Account extends Model{
#Id
public String email;
public String password;
#OneToOne
public Person person;
}
So far so good, Any given person can have a single account. The Account object is copied from the zentask example. After authentication I redirect to the index page which displays the user realname as stated in the Person.name member variable. The Account object is inserted in the page just as with the zentasks example like so:
Account.find.byId(Controller.request().username());
Now the following strange things happen in the template which i do not understand:
#account.person.name
results in a Null value inserted in the template while calling:
#account.person.getName() or #account.person.getName
results as expected with the correct name inserted from the person object.
#account.person
shows the .toString() of the person object, also correctly showing the name.
So to summarize: What is wrong with the code above? Why can I call the account.person value without any problems, but when I call account.person.name this does not work anymore
Thank you in advance!
Richard
This is because JPA uses Aspects to intercept getter usage and fill-in the missing data from objects that are lazy-loaded. I don't know what conventional thinking is, but I would not use public members ever with JPA for this reason, it will break the framework consistently.
If you really want to use public members, you'll have to mark relationships as eager fetching:
#OneToMany(fetch=FetchType.EAGER)
or explicitly fetch all of the object tree you'll need in your template (ugh).
In your case, the relationship, a OneToOne is defined on the other side of the relationship, if you define it on the Account side, it should fetch eager by default. I forget if you can define OneToOne on both entities, I think you can, but you might have to fiddle with it a bit.
Overall, don't use public members with JPA, it will break. Better yet, ditch JPA and use Anorm instead, it maps to the problem domain much more successfully than JPA. Issues like this consistently cause JPA implementations to take up twice as much implementation time as anyone seems able to predict.
I just stumbled upon an answer posted by Guillaume Bort, which explains things.
Read here:
https://groups.google.com/d/topic/play-framework/CNjH3w_yF6E/discussion
Hope this helps!
Because of lazy loading the values in the field only get loaded when you access them from the class itself.(something that, in normal circumstances would use a setter/getter
In order to load the values you ether have to write getters and setters.
Or you can create a methode that checks every value.
you can add the following methode to your Account Entity:
public void checker(){
if(email==null){}
if(password==null){}
if(person==null){}
}
this will load every value, but won't reduce performance