design pattern to use for changing object properties from property inspector - oop

I have this problem at hand and wanted some inputs on best way to approach it -
In my application, i have different set of objects and each object has some properties associated with them.
For eg: objects like, say, shapes , which has some common properties like color and specific properties ( for eg: circle has radius and center has its specific property )
The user can change object properties through a property inspector.
Currently, the PI holds a object ID and uses that to talk to DOM for anything related to object it currently refers. like say, if it wants to change the property of object it will call DOM Api
ChangeObjectProperty(objectID , *);
(I hope using object ID to refer to objects inside the DOM is right way. (is there a better way to do this ?))
What should i pass as the second argument ?
Should it be a single opaque object ( say, IObjProps) where each objects provides its own structure ?
For example the circle will have following structure of its own -
class CircleProps extends CommonProps
{
public var radius:Number;
public var center:Point;
}
class CommonProps extends IObjProps
{
public function clone();
public var color:Number;
}
The DOM will pass on this structure to the object and object takes care of applying the change.
So ChangeObjectProperty(objectID , *); function will be implemented as -
function ChangeObjectProperty(objectID , props:IObjProps)
{
var object:Object = GetObjectFromID(objectID);
object.SetProperty(iObjProps)
}
The problem with the above approach is that - its kind of difficult to know what specific property changed since its a single structure -
The alternative is to have property dictionary - the client inserts only the properties which got changed into the dictionary.
I wanted to know what is design pattern followed for such use cases.
Adding more info - this is actionscript -

Related

How to iterate Apache velocity template variable attributes

As title, is there any way to iterate or display Apache velocity template attributes?
for example, I have following code :
<code>
${ctx.messages.headerMessage}
</code>
And I want to know how many other attributes the variable ${ctx} has
It's only possible to discover and to loop on an object properties (that is, the ones with getters and/or setters) if you can add a new tool to your Velocity context. If you can't, you're rather stuck.
There are several ways to do this, I illustrate below how to do this with commons-beanutils.
First, add Apache commons-beanutils in your class path, and add it to your Velocity context from Java:
import org.apache.commons.beanutils.PropertyUtils;
...
context.put("beans", new PropertyUtils());
...
One remark: if you do not have access to the Java part, but if by chance commons-beanutils is already in the classpath, there is one hakish way of having access to it: #set($beans = $foo.class.forName('org.apache.commons.beanutils.PropertyUtils').newInstance()).
Then, let's say that I have the following object:
class Foo
{
public boolean isSomething() { return true; }
public String getName() { return "Nestor"; }
}
which is present in my context under $foo. Using your newly $beans properties introspector, you can do:
#set ($properties = $beans.getPropertyDescriptors($foo.class))
#foreach ($property in $properties)
$property.name ($property.propertyType) = $property.readMethod.invoke($foo)
#end
This will produce:
bar (boolean) = true
class (class java.lang.Class) = class Foo
name (class java.lang.String) = Robert
(you'll need to filter out the class property, of course)
One last remark, though. Templates are for coding the View layer of an MVC application, and doing such a generic introspection of objects in them is rather inadequate in the view layer. You're far better of moving all this introspection code on the Java side.

How should a 'Virtual ListCtrl' access the 'Model Data' to populate it's list?

I have a class called MVC which provides separation and acts as a broker between wxFrame/UI and the Model Data.
class diagram
To keep things simple, MVC was made a member variable of wxFrame.
class MAIN_FRAME: public wxFrame
{
public:
MAIN_FRAME();
public:
MVC MODEL;
};
wxFrame implements a 'virtual wxListCtrl' to request the list data from MVC.
However, I'm not clear on the best way for the 'virtual ListCtrl' to access the MVC, specifically in the overloaded function which requests the list data. The problem being that MyVirtualListCtrl is separate from wxFrame so doesn't have scope to access wxFrame member variable MVC:
wxString MyVirtualListCtrl::OnGetItemText( long item_, long col_ ) const
{
// If no data then populate list cells with "Empty".
if( this->MVC.empty() )
{
return _( "Empty" ) ;
}
// Use item and column to return the correct data for that particular cell.
// mock solution
>>wxString s = MVC.get_data( item_, col_ );<<
>>return s;<<
}
When constructing your MyVirtualListCtrl class, you can give it a reference to the MAIN_FRAME or, arguably better, directly to its MODEL and then just use it. As you can safely assume that MAIN_FRAME (and hence its model) will outlive MyVirtualListCtrl, because the frame children are destroyed when the frame itself is, this is safe from the object life-time point of view.

Swift 2: use extensions to turn an Apple class into a singleton?

I have a lot of different classes that need to access the same GKMatch object, and I'm getting wary of how often I have to pass that GKMatch reference around. I want to find a way that any class can reliably get the same GKMatch instance.
I am proposing doing it indirectly via GKMatchDelegate, like this:
extension GKMatchDelegate {
static func sharedGKMatch ()->GKMatch? { return nil }
}
Then, in my GKMatchDelegate subclass, I would create a currentMatch: GKMatch? variable and override the function to return it.
When I tried to test the concept in a playground, I did this:
class SingletonTest {}
extension SingletonTest {
static func shared ()->SingletonTest? { return nil }
}
class SubclassOfSingletonTest: SingletonTest {
static var sharedTest: SingletonTest?
override static func shared()->SingletonTest? {
return sharedTest
}
}
And got the error message "Declarations from extensions cannot be overriden yet". From the "yet" I'm guessing they're working on it. In the meantime, is there another way to achieve the same result?
I'm assuming you only want one shared GKMatch, not a GKMatch for every subclass of GKMatchDelegate. In the former case, I think you are overthinking this. Just have a public var that is the singleton not in any class.
public var sharedGKMatch: GKMatch = GKMatch()
If you want to make it read only
public let sharedGKMatch - GKMatch()
It's a class so the content of GKMatch is still mutable.
Most applications have some sort of data model -- an object or group of objects that stores all the information that the app needs to do whatever the app does. For a word processor, for example, the data model might be a collection of open documents together with any global configuration information (printer information, fonts, preferences, etc.). For a game, the model contains the current state of the game -- board state, current locations and velocities of bad guys, number of gold coins collected, whatever.
Your app should have a data model too, and it sounds like the current GKMatch instance should be included in that model. That way, any class that has access to the model automatically has access to the GKMatch, which means that you've only got one thing that you need to pass around. That's almost certainly a better approach than adding a shared object interface to someone else's class.

OO Design Issue related to subclassing

I have a parent bean having one instance variable like below :
public Class ParentBean {
protected boolean show; // this variable will be used to show some UI component
public void action() {
// update show variable here
}
public boolean isShow() {
return show;
}
}
Is it a good design if I want to reuse the "show" variable in a child bean (to show other UI component specific to child bean ) as shown below :
public Class ChildBean extends ParentBean {
// override the action method from parent bean
public void action() {
// update show variable here
show = true /false;
}
}
In effect , show variable is being updated by "childBean" by overriding action() method.
Is this a good design practice ? Otherwise same thing has to be repeated in ChildBean to get this work done.
If you use the show variable for the same purpose in the subclass, as you seem to be doing in this example, then obviously you should reuse it, because otherwise you just end up writing the same code twice, which is contrary to the point of OOP.
In general, in OOP, it is common to override superclass methods in subclasses, as well as modifying superclass instance variables, as long as you know what the variable you are modifying is being used for (you don't want to be randomly changing instance variables in classes that you don't completely understand, or don't have access to the source of, because you don't want any unfortunate side effects), so when it's your own code, this is absolutely fine.
As a general guideline, if your options are either to copy and paste a massive hunk of code and use it unchanged, or subclass and use the superclass' instance variables or functions, it's better to subclass. Otherwise, you're missing out on the point of OOP.
Changing the value in subclass will not affect superclass variable
This is fine with respect to the design. When a subclass object is instantiated, it will have a different copy of variable. and If superclass object is instantiated it will have different copy.
It is. Having a protected variable means you are allowed to modify it into parent or children classes (remember every single instance of each class has its own property values). So, if you have some generic functionality which is valuable for all the children:
Parent class:
public void action(){
//update show variable here
show = true;
}
Appart from that, if you want to add some extra functionality in a specifical child:
Child class:
#Override
public void action(){
super.action();
//Extra functionality goes here
//You can also access parent's 'protected' fields
if (show){
System.out.println("Shown");
}
}
An example of the use:
Parent p = new Parent();
p.action();//Prints nothing
Child c = new Child();
p.action();//Prints 'shown'

Building one object given another

Say I am calling a third-party API which returns a Post, and I want to take that and transfer properties from it into my own Post class. I have in the past had a method like public static my.Post build(their.Post post) which maps the properties how I want.
However, is it better/valid to have a constructor that accepts their.Post and does the property mapping in there? Or should there always be a separate class that does the converting, and leaves my.Post in a more POJO state?
Thanks for your thoughts!
These answers always starts with "it depends."
People generally argue against using public static methods, based on the fact that it is hard to mock them (I don't buy into that bandwagon).
This comes down to design, do you want their post to be part of your class? If you add it as a "copy" constructor then it will now be part of your class and you are dependent on changes to post. If they change their post, your code has to adapt.
The better solution is to decouple it. You would need to find some extenal method to map the two. One way is to use a static builder method (like you mentioned) or if you want to take it a step further, a more complicated solution would be to extract the information you want from their post into some type of generic collection class. Then create a constructor that will accept that constructor class. This way if they change their design your class stays in tact and all you have to do is update the mappings from their post to your generic representation of it.
public class MyPost{
public MyPost(ICollectionOfProperties props){
//copy all properties.
}
}
public static class TheirPostExtensions{
public static ICollectionOfProperties ExtractProperties(this TheirPost thePost){
return new CollectionOfProperties(){
A = thePost.PropA,
B = thePost.PropB
};
}
}
public class Example{
public Example(){
TheirPost tp = new TheirPost();
ICollectionOfProperties props = tp.ExtractProperties();
MyPost mp = new MyPost(props);
}
}