How can decorators implement new behaviour? - decorator

I am trying to teach myself design patterns. I was reading about the Decorator pattern, and there's one thing that I can't quite get. Here is an example found on wikipedia:
Let's say I want to implement a window that can scroll both horizontally and vertically, I would do this:
Window win=new Window();
win=new HorizontalScrollBarDecorator(win);
win=new VerticalScrollBarDecorator(win);
Since win is a reference of type Window, I can call neither drawHorizontalScrollBar() nor drawVerticalScrollBar() (apart from the code in the concrete decorator itself), and of course it would not make sense to change the reference type.
So, how can I "add new behaviour" (I.E. implement new methods) with decoration?

Nevermind, I think I got it:
Through decoration you can't "add new behaviour" as I had interpreted it (drawHorizontalScrollBar() is not visible from outside the HorizontalScrollBarDecorator class, which means it's probably called by its draw() method), BUT you can extend methods of the base class.
It's like extending the base class, overriding the draw() method and then calling super.draw();, with the difference that you can apply multiple decorators, but you can't inherit from multiple classes.

Related

Minecraft Modding: Make entity teleport up when hit by a certain item

I am learning to create minecraft mods.
I would like to know how to make an entity teleport upwards when hit by a certain item. I have already made the item.
Override either Item#onLeftClickEntity(ItemStack, EntityPlayer, Entity) or Item#hitEntity(ItemStack, EntityLivingBase, EntityLivingBase). In that method set the target's Y position to whatever you want.
I think you might be able to use onLeftClickEntity. in the item class for your item, make a
onLeftClickEntity method. inside that method, it should be as simple as using motionY. untested though, and for 1.12.2.

How to add global barButtonItems to custom UINavigationBar?

I am currently trying to create a custom UINavigationBar Subclass.
What i want to achieve is that all custom navigationbars inside my app should have a couple of barbuttonitems in common. But it should still be possible to define left or .rightbarItems on the navigationItem in code.
What i tried so far is that subclassed the UINavigationBar and played around with all methods and delegate callbacks i could potentially use to change the navigationItem. There are methods like
setItems:animated:
setItems:
navigationBar:didPushitem etc.
.. which could be used to modify the navigationItem somehow.
But there is no callback which can be used to change the item initially.
What i basically want to know is how it is possible to change navigationItem initially before it is being pushed ?
Solved it by myself using
willShowViewController of the UINavigationControllerDelegateProtocol
to modify the navigationItem

ASP.NET Multiple Inheritance

I am stuck in a doubt...
I have an aspx page that has to inherit 2 classes:
1) Class expand : Page, IPostBackEventHandler
2) mydll.classX
But I am unable to accomplish the same.
Any workaround that can be suggested would be really helpful...
Thanks
You can't inherit from more than one class in .NET. You should look into one of these methods to solve your problem:
Make your classX inherit Page. Now your new class can inherit classX, and it'll be a child class of both clasX and Page;
Use interfaces instead of classes. You're kinda doing that already, with IPostBackEventHandler. You can't inherit from multiple classes but you can implement as many interfaces as you want;
Use composition - that is, your class has a member that is of type classX. Read about object oriented design patterns, that may help you accomplish what you want in elegant ways that make things easier. See this question: What is composition as it relates to object oriented design?
Since you can't do multiple inheritance in what looks to be C# I think you will have to:
a. Replicate the public interface of myDll.classX and forward calls to an instance of myDll.classX
or
b. Make myDll.classX inherit Page, but that may not really be an option.
Without more information on what you are trying to accomplish, I can't really see any other options.

does actionscript addChild require a display object first

Solution:
if you have the same problem, addElement() instead of addChild() is what did it
I'm trying to move away from mxml to actionsctipt. I have a <s:Rect> that I've created and set its properties, but having trouble adding it.
var aRect:Rect = new Rect();
//set properties like aRect.x, aRect.y, aRect.width, aRect.height
//tried adding it various ways
addChild(aRect);
Application.addChild(aRect);
Application.application.addChild(aRect);
stage.addChild(aRect);
But I keep getting the error
1067: Implicit coercion of a value of type spark.primitives:Rect to an unrelated type flash.display:DisplayObject
Originally in the mxml, it was right inside <s:Application> not nested inside anything
<s:Application>
<s:Rect id="aRect" x="10" y="10" width="15%" height="15%">
//then fill code here, removed for readability
</s:Rect>
</s:Application>
What's the deal, I thought actionscript would be nicer than mxml.
tried changing addChild(aRect); to addElement(aRect); and that worked beautifully.
It's because Flex 4 significantly changed the way the display hierarchy works in MXML-based applications. This is a bit confusing since addChild() no longer works as simply as you'd want it to - you have to add elements to a dataprovider, and then the logic of displaying those elements (which ones to add where, how to skin them, etc) is handled elsewhere. It's kind of a useful change, though, because it forces you separate your concerns in a very concrete way. Once you have your elements all added to your dataProvider you can swap out Layout objects at will (even at runtime) to change the way your application looks.
EDIT: Technically it's not the displayList itself that they've changed. It's the fact that the basic unit used by Flex is now the "Group" - even s:Application extends group. You add your content to a a Group (or to the top level Application) and then you assign the group a layout to tell it how to display the items you've added.
Yes, you need a DisplayObject. I'm not familiar with spark.primitives.Rect, but perhaps you could just create a new Sprite and call methods on its Graphics object to draw the rectangle?
According to the live docs, the addChild method of the Application class does require it to be a displayObject.
Annoyingly we will often struggle to add flash assets ( swf swc ) (display objects) using addElement.
I'm working on a way to do this right now :( more hoops and jumping
Also my swc is not viewable in the package explorer (why not ?)

Flex component access other component

I have 2 components for example (editor.mxml using mx:windows), when I click an edit button, I want to get the current value from the other component's datafield? (datagrid.mxml using mx:window)
I do know how to access the main MXML's datagrid by parentDocument or Application.application method, but stumped block if I want to access other way as mentioned above. Keep the code as simple as possible.
You could either do dependency injection, that is, give component A a reference to component B so that they can communicate directly (example of tighter coupling,) or have both components communicate through a common mediator using events (example of more loose coupling.)
Both of those options would be implemented wherever it is that you're creating those components (A and B in this example) and adding them to the display list.
This might be more complicated than it deserves, and it smacks of Pattern-Fever, but you could use a mediator class that listens for the CLICK event from the button and knows enough about the other component to query its property. It could even transmit that data using a custom event, which the button listens for.
While this involves three classes instead of two, it often turns out to be easier to have two components that focus on looking good and one that worries about coordination.
Cheers
Try this:
FlexGlobals.topLevelApplication
This points Your root. From the root You can grab every element You want.
You can also add an id to the custom component like this,
<custom:Editor id="myCustomComponent">
</Editor:AddressForm>
and
access your datagrid's value like this,
var data:ArrayCollection = myCustomComponent.DatagridID.dataProvider;

Resources