适用情景: 假如有一组相关的算法, 客户需要从中动态选择一个来使用. 譬如: 学校中的新生分班, 有按照成绩分班, 按照性别分班, 按照住宿类型分班等等. (当然实际情况中的分班考虑因素会更复杂一些)
最简单的方法是一个Method, 通过判断类型进行相应操作. 但这样做的是这个Method会巨大无比, 不易维护. 因此我们可使用Strategy模式, 会多一些Interface或Class, 但会降低耦合, 拥有更佳的可扩展性.
负责进行分班的主体类包含IClassEnrollStrategy的一个Instance, 对于这个Instance可以使用以下方式注入:
1. 使用Constructor注入到ClassEnroll中, 可在ClassEnroll中增加一个Constructor:
ClassEnroll(strategy:IClassEnrollStrategy)
2. 使用Setter注入. ClassEnroll中对strategy的Setter方法:
setStrategy(strategy:IClassEnrollStrateStrategy)
3. 在ClassEnroll使用工厂模式创建IClassEnrollStrategy实例.
Those days I’m looking a new job related to Java and Flex… Seems that this is a popular interview question, I event didn’t encounter it in the ACE. But I was asked more than 3 times.
Talk about overloading in OOPS:
Overloading is between the methods with the same name, If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.
ActionScript 3 support overriding, But why it is not support overloading?
It’s a good question, and can be the answer of what’s the difference between Java and Flex?
Based on the Standard ECMA-262 (ECMAScript Language Specification), Action Script does not implement the overloading. and every method in the class is a Function object(property)
Since AS doesn’t support overloading, what can you do to implement the overloading?
As I known, Basically there are three way to handle the overload requirement:
1. Using optional parameters, such as:
getUser(userID:int = –1, userName: String = null):User {..}
2. Using rich parameters:
getUser(…args):User{
if(args.length == 0) {
return null;
}else if (args.length…)….
}
3. Using Object as the parameters, like:
getUser(arg: Object):User {
if(arg is String) {
return …. get user by the userName;
} else {
if (arg is int) {
return …get user by the userID;
}//end of if.
}
According those codes, you can see those methods all need very clear comments, and not easy for other to use them. in the comment we need to tell others the parameters type or sequence.
In fact, Since the AS does not support the overloading, and the comments need be very very clear, and usually, there will be two public methods: getUserByID(), getUserByName(), maybe a private or protected method using the above 3 ways to do the get user job.
Sometimes we use thread.sleep to let the thread stop running for a while in Java, but in Flex, we can’t control the thread, but sometimes we want to let the method sleeping, and here is what I’ll do:
public class SleepUtils { // Constructor public function SleepUtils() { } /** * Let the thread sleep. * @param ms Million seconds you want to sleep. * */ public static function startSleep(ms:Number):void { var timeBegin:Date = new Date(); trace("Sleep begin: " + timeBegin.toTimeString()); while((new Date()).getTime() - timeBegin.getTime() < ms) { } trace("Sleep end: " + new Date().toTimeString()); } } // End of class
What’s the difference ? Here is the test codes:
We will create a button in a Group, and listen the button click event(MouseEvent.Click):
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void { // Create button and container var hbox:HGroup = new HGroup(); var button:Button = new Button(); button.label = "Button"; // Add Eventer litener button.addEventListener(MouseEvent.CLICK, onButtonClick1); button.addEventListener(MouseEvent.CLICK, onButtonClick2); hbox.addEventListener(MouseEvent.CLICK, onButtonClick3); hbox.addElement(button); addElement(hbox); } // Button click handler 1 private function onButtonClick1(e:MouseEvent):void { e.stopImmediatePropagation(); // or e.stopPropagation(); trace("Handler 1"); } // Button click handler 2 private function onButtonClick2(e:MouseEvent):void { trace("Handler 2"); } // Button click handler 3 listened by hbox private function onButtonClick3(e:MouseEvent):void { trace("Handler 3"); }
Here is the difference:
when use stop propagation in the handler 1st, the trace is:
Handler 1
Handler 2
but when use stopImmediatePropagation(), the trace is:
Handler 1
That’s pretty easy to find the differences:
“The stopImmediatePropagation() method also prevents the Event objects from moving on to the next node, but it does not allow any other event listeners on the current node to execute.” (Adobe livedocs)
Class写的没有问题, 实现了IExternalizable 接口, 但总是报错:
ArgumentError: Error #2173: Unable to read object in stream. The class com.liguoliang.helloworld.Department does not implement flash.utils.IExternalizable but is aliased to an externalizable class.
at ObjectInput/readObject()
at mx.collections::ArrayList/readExternal()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:586]
at mx.collections::ArrayCollection/readExternal()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\collections\ArrayCollection.as:147]
at ObjectInput/readObject()
at mx.messaging.messages::AbstractMessage/readExternal()[E:\dev\4.0.0\frameworks\projects\rpc\src\mx\messaging\messages\AbstractMessage.as:486]
at mx.messaging.messages::AsyncMessage/readExternal()[E:\dev\4.0.0\frameworks\projects\rpc\src\mx\messaging\messages\AsyncMessage.as:170]
at mx.messaging.messages::AcknowledgeMessage/readExternal()[E:\dev\4.0.0\frameworks\projects\rpc\src\mx\messaging\messages\AcknowledgeMessage.as:95]
找来找去找不到原因, 后来发现在App中根本没有使用过这个类! 很久没开发Flex, 把这么个事给忘了… 个人感觉应该报Class not found的错啊 🙁
对于没有使用过的类, 可以在App中引用一下, 确保在编译时不要被忽略:
// 本例中未试过用Department类, 仅作引用; private static function __ref():void { Department; }
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.