I know that by definition AS3 interfaces must be public, and the methods within them must be implemented as public as well.
I read this question and I suppose the answer is obvious if you want to have some classes choose whether or not to implement some methods, but have common base methods that must be implemented across all classes that implement either interface.
With that in mind, even with that 'private implementation' idea (which really isn't), is the best idea still to just explicitly define a private method for all classes, outside of the interface? The issue isn't forcing some classes to implement different methods, it's just the general visibility of those methods. I'm guessing the answer is "yes", but I figured I'd see if anyone had any insight.
Although AS3 doesn't support abstract classes , why not define a class to be used as an abstract class and have it implement that interface and define the non public methods inside that class.
interface IThing {
function thisMethodIsPublic():void;
}
public class ThingAbstract implements IThing
{
//throw an Error to avoid calling the class directly,
//this class needs to be subclassed
//and this method overridden
protected function thisMethodShouldOnlyBeVisibleToCertainClasses():void
{
throw new IllegalOperationError
('this method should be overriden in a subclass');
}
public function thisMethodIsPublic():void
{
}
}
Related
Is there anyway tom mock the super method call in a class using Powermock?
For example:
public void processRetrievedData() {
super.processRetrievedData();
}
is there anyway to mock “super.processRetrievedData()” ?
Thanks,
Rajan
Although this is bad practice, sometimes you need to mock out inheritance when your are working in an environment where there is no choice. For example, I needed to mock out super class methods in a Dialog Fragment in Android to isolate my unit tests. In your case, within your test use...
#PrepareForTest(ChildClass.class)
public class ChildClassTest {
#Test
public void testMethod() {
PowerMockito.suppress(PowerMockito.method(SuperClass.class, "processRetrievedData"))
// Run method and test
}
}
There are other overloaded methods listed in the API under the MemberMatcher class that are useful in other cases such as, method has parameters, there are additional inherited methods, etc. Hope that helps.
This is a terrible idea and a dirty hack.
If this is a part of your subject under test then you should never, ever do such things since you're testing the behaviour of that whole class and not only part of it.
If you really need to do sth similar you can extend the class and override the processRetrievedData method so that it doesn't call super - that way you stub that method.
I'm looking at a control that must output raw HTML and provide rich design-time support.
How to create a custom server control, extend the WebControl class and override the Render method?
Can you provide an example?
regards,
Blanco
This will create a control that extends web control and overrides the Render method (though doesn't actually do anything with it.
public class TestControl : System.Web.UI.WebControls.WebControl
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(writer);
}
}
As you can see this is a fairly trivial answer but complete so I suspect that you didn't actually ask the question that you intended to.
I want to make new .AS files for each and every object in my game for the sake of versatility, dynamism, and organization. I mean, in the case of a shooter game, I want to make a new class file for every type of bullet with all of their unique properties all spelled out in their respective classes. I want to do the same thing for every type of enemy in the game.
This is all assuming there may be 10+ different types of enemies/bullets.
Some people have been pushing me to keep the values of each type of, for example, bullet in one class and then just change the variables depending on the type of bullet being fired. That doesn't sound too fun to me and I would rather just create a bunch of different class files and just push all of the bullets into a common array(which works so far), but I would really like to know if I have the right, or even good, idea by doing so.
In my opinion, since bullets (or enemies, for that matter) represent the same family objects, it would make sense to have a common interface, or an abstract class, which is implemented, or extended by each concrete class. Is this a good idea? Let's think about it this way:
When you are creating the classes that will be used throughout your application, you essentially building an API. Good practice suggests that you should always program to an interface rather than an implementation. What this means is that your top-level classes, should not depend on low-level ones, but rather they should use abstractions. That way, the different-level components are loosely coupled and the overall code is more flexible. This principle is known as Dependency inversion, and is one of the five principles of the SOLID design.
The links provided should give some additional information on how to structure your code.
Have a great day!
You'll want to use inheritance to make it cleaner and more flexible with changes. Then even on multiple projects you can just extend the same generic base class.
Start with a base class (or interface) - Bullet.as for example - and put all the functionality and properties that are common to ALL bullets in that class. Anytime you have groups of bullets that share the same properties, keep making sub-classes. So if you had multiple kinds of bullets that all explode on contact, you could have the following kind of setup:
public class Bullet {
public function fire():void {};
public property size:int;
public property strength:Number;
public property label:String;
public property maxDistance:Number;
}
public class ExplodingBullet extends Bullet {
public property blastRadius:Number;
public function explode():void {
trace("Kaboom");
}
}
public class BazookaBullet extends ExplodingBullet {
public function BazookaBullet():void {
blastRadius = 10;
label = "Bazooka";
size = 5;
maxDistance = 120;
}
}
This would give your bazooka bullets all the functionality of the class it extends. There are a great many benefits to doing it this way as opposed to recreating all the same properties and methods in all your bullet classes.
I'm in the process of developing an advanced modular GUI system. I need it to be as flexible as possible and hopefully as useful to as many people as possible (however it will be reasonably niche).
The part of my design that is causing me to think, is how each component should best interact with the rest of the application.
I see two approaches:
Pass in the class and name of a setter, on which the component should act upon. This still allows events to be dispatched from within the setter if desired.
Pass in a CustomEvent which the component should dispatch, with relevant data. Though I feel this may lead to a lot of Event Listeners, and extra complexity.
But What is best practice? What are the pros and cons of each? Is there a better method?
Basic example:
public class Button extends Sprite
{
private var _property:String;
private var _object:*;
public function Button(label:String, object:*, property:String)
{
_property = property;
_object = object;
addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e:MouseEvent):void
{
_object[_property] = "Changed";
}
}
Basic Example:
public class Button extends Sprite
{
private var _event:GuiEvent;
public function Button(label:String, event:GuiEvent)
{
_event = event;
addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e:MouseEvent):void
{
dispatchEvent(new GuiEvent(_event.type, "Changed"));
}
}
If you want your GUI framework to be useful to everybody, you should use standard events instead of inventing new ones. The second option is the way to go but you should try to keep things simple. The code you wrote in example 2 seems incredibly complicated. It could simply be written like that:
public class Button extends Sprite
{
public function Button(label:String)
{
}
}
Then anybody can handle clicks using the event they know:
button.addEventListener(MouseEvent.CLICK, onClick);
Slightly offtopic and not really an answer to your question:
Why recreate something that already exists? There are already proven libraries with GUI components.
For example, take the AS3 Temple Library
http://code.google.com/p/templelibrary/
It has all types of form element components; checkbox, radiobutton, combobox, inputfield, filefield, scrollcomponent, date selectors, (step)sliders, list, scrollbars, autocomplete field and all types of buttons.
It also has a Form class, whichs helps in validation, deliver data to any type of services. It also manages the tabindexes right and all controls can be used with keyboard too, just like everyone expects it to work. There are also codecomponents for a mockup, but you can use the same code to style it with endless creativity with library items, timelines, or however you want it. Most of the gui components are designed so they can be extended to make it fit your needs.
Most of the gui components work with normal eventlisteners (most dispatch Event.CHANGE when content change), in most cases there is no need to wrap it into new type of events.
To put it simply as an example,
public abstract class AbstractFellow {
protected Thing buddy;
....
public class ConcreteFellow extends AbstractFellow {
public void someMethod() {
buddy.doSomething();
//OR
buddy = somethingElse;
//OR
somethingElse = buddy;
}
}
Is this bad practice?
Opinions vary. My opinion is that even in abstract classes, using private members and protected accessors (i.e. protected getBuddy()) is a better practice.
It allows for the same things encapsulation always allowed: to contain the logic of obtaining the "buddy" object in the super-class, and allowing you to change that logic without breaking all inheriting classes.
The super-class might not expect buddy to be changed, either. For example, you might want to unregister listeners or do some other cleanup when that happens - having a setter method helps achieve that.
In addition, it obviously allows you to have Buddy as a read-only member (since you can provide only a getBuddy and no setBuddy), something that is not as easy to accomplish with a member (you can always set it to be final, but then you prevent the super-class from changing it, too!)
It depends on you domain model and why you creating and abstract class. If you are defining your interface with it and want abstract class to keep some functionality it`s ok.
If you are just setting all the fields protected and then reusing them in your child classes. Well it depends, but I think a better way should be found. And it seems not very clear for your future reader to get data in the base class and all it's behavior in child classes.
If you do not need base class ability to implement methods (and you do not need to store any functionality in your base class) maybe it`s a better choice to implement an interface with every of these child classes.
If you use some of base class inner fields it seems natural to me and it's ok. Just if you are using some of them in your child classes for similar things you can implement a template method and enjoy with overriding only the parts you really need to override.