锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久热精品视频,91久久精品国产91性色,国产乱视频在线观看http://www.aygfsteel.com/jedikings/category/12126.htmlzh-cnWed, 28 Feb 2007 07:42:44 GMTWed, 28 Feb 2007 07:42:44 GMT60Reading notes -- Singleton patternhttp://www.aygfsteel.com/jedikings/archive/2006/07/09/57330.htmlJediJediSun, 09 Jul 2006 04:42:00 GMThttp://www.aygfsteel.com/jedikings/archive/2006/07/09/57330.htmlhttp://www.aygfsteel.com/jedikings/comments/57330.htmlhttp://www.aygfsteel.com/jedikings/archive/2006/07/09/57330.html#Feedback0http://www.aygfsteel.com/jedikings/comments/commentRss/57330.htmlhttp://www.aygfsteel.com/jedikings/services/trackbacks/57330.html public class 聽Singleton聽{

聽聽聽聽
private volatile static 聽Singleton聽uniqueInstance;
聽聽聽聽
private 聽Singleton(){
聽聽聽聽聽聽聽聽
聽聽聽聽}
聽聽聽聽
聽聽聽聽
public static 聽Singleton聽getInstance(){
聽聽聽聽聽聽聽聽
if (uniqueInstance == null ){
聽聽聽聽聽聽聽聽聽聽聽聽
synchronized (Singleton. class ){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if (uniqueInstance == null ){
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽uniqueInstance聽
= new 聽Singleton();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
return 聽uniqueInstance;聽
聽聽聽聽}

}

1. private constructor
2. static getInstance
3. syncronized..waste a lot of time
4. double check..modified syncronize, so time-waste might occurs only when first time the instance construct


Jedi 2006-07-09 12:42 鍙戣〃璇勮
]]>
Reading note of "Head First Design Patterns": Strategy Patternhttp://www.aygfsteel.com/jedikings/archive/2006/06/13/52422.htmlJediJediTue, 13 Jun 2006 04:42:00 GMThttp://www.aygfsteel.com/jedikings/archive/2006/06/13/52422.htmlhttp://www.aygfsteel.com/jedikings/comments/52422.htmlhttp://www.aygfsteel.com/jedikings/archive/2006/06/13/52422.html#Feedback0http://www.aygfsteel.com/jedikings/comments/commentRss/52422.htmlhttp://www.aygfsteel.com/jedikings/services/trackbacks/52422.html聽聽聽Joe's company聽makes a duck pond simulation game, SimUDuck, The game can show a large variety of duck species swimming and making quacking sounds.

Initial Design:
diagram.JPG
But now some new functionality should be added, for example: we need some of the ducks to FLY.

First Design:
聽聽聽We add a method fly() into the Duck class. It seems worked, but something went horribly wrong because not all ducks can fly. so....

Second Design:聽Using inheritance and polymorphism
聽聽聽Always override the fly() mehtod in the subclass where needed.

test.JPG
聽聽聽

聽聽聽Drawbacks:聽Everytime a new duck is added, you will be forced to look at and possibly override fly() and quack(). so is there a cleaner way of having only some of the duck types fly or quack?

Third Design: Using interface!
聽聽聽test1.JPG
Drawbacks: It completely destroy code reuse for those behaviors.

1. Design Principles: Identify the aspects of your application that vary and separate them form what stays the same!
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 which means Encapsulate the parts that vary!
2. Design Principles: Program to an interface, not an implementation! (interface here means supertype! including interface
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽and abstract class!.. making use of the polymorphism functionality).
3. Design聽Principles:聽Favor composition over interface!

Strategy Pattern: Using Composition!
test2.JPG

Code implement:
FlyBehavior.java
publicinterface聽FlyBehavior{
聽聽聽
publicvoid聽fly();
}

FlyWithWings.java
publicclass聽FlyWithWings聽implements聽FlyBehavior{
聽聽聽
publicvoid聽fly(){
聽聽聽聽聽聽System.out.println(
"I'm聽flying!!");
聽聽聽}

}

FlyNoWay.java
publicclass聽FlyNoWay聽implements聽FlyBehavior{
聽聽聽
publicvoid聽fly(){
聽聽聽聽聽聽System.out.println(
"I聽can't聽fly");
聽聽聽}

}

Duck.java
publicabstractclass聽Duck{
聽聽聽FlyBehavior聽flyBehavior;
聽聽聽
public聽Duck(){聽聽聽聽聽聽
聽聽聽}

聽聽聽
聽聽聽
publicabstractvoid聽display();
聽聽聽
聽聽聽
publicvoid聽performFly(){
聽聽聽聽聽聽flyBehavior.fly();
聽聽聽}

聽聽聽
聽聽聽
publicvoid聽swim(){
聽聽聽聽聽聽System.out.println(
"All聽ducks聽float,聽even聽decoys!");
聽聽聽}

}

MallardDuck.java
publicclass聽MallardDuck{
聽聽聽
public聽MallardDuck(){
聽聽聽聽聽聽flyBehavior
=new聽FlyWithWings();聽聽聽聽聽聽
聽聽聽}

聽聽聽
聽聽聽
publicvoid聽display(){
聽聽聽聽聽聽System.out.println(
"I'm聽a聽real聽mallard聽duck");
聽聽聽}

}

The Definition聽of Strategy Pattern:聽The聽Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them intercahgeable. Strategy lets the algorithm vary indepanedtl from client聽that use it

Problems:
1. It's weird to have a class that's jast a behavior: classes represent things both have state and methods. a flying behavior might have instance variables representing the attributes for the flying behavior.
2.聽Be care聽of聽Over-Design: implement your code first, then refractoring!



Jedi 2006-06-13 12:42 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 南陵县| 浑源县| 拜泉县| 民权县| 板桥市| 东阿县| 永平县| 霸州市| 桐梓县| 麻江县| 拜城县| 东光县| 介休市| 托里县| 寻甸| 通海县| 甘孜县| 峡江县| 土默特左旗| 招远市| 桦南县| 台东县| 邛崃市| 霍邱县| 镇安县| 章丘市| 中卫市| 平潭县| 格尔木市| 永春县| 洪江市| 常山县| 惠来县| 道孚县| 青海省| 景泰县| 宿迁市| 阜新市| 南昌市| 定襄县| 襄垣县|