S
Shawn
Hi,
I am amazed at the following way transforming a Class into different
Class on the fly(Expert becomes Singer, Chef or Painter). Could you
throw in some comment for me? Is this called Strategy design pattern?
Now I do feel "has-a" is more powerful than "is-a".
Thank you very much.
<code>
interface Talent
{
void showTalent();
}
class Singing implements Talent
{
public void showTalent()
{
System.out.println("do-rei-mei");
}
}
class Painting implements Talent
{
public void showTalent()
{
System.out.println("yellow, blue, red");
}
}
class Cooking implements Talent
{
public void showTalent()
{
System.out.println("gong-bao chicken");
}
}
//Skills of Expert is undecided right now. His skills will be set on the
fly. I am truely amazed at how powerful this technique it is.
public class Expert {
private Talent myTalent = null; //undecided yet
public Expert(Talent t)
{
myTalent = t;
}
public void serve()
{
myTalent.showTalent();
}
public static void main(String[] args)
{
Expert anExpert = new Expert(new Singing()); //this expert is singer
anExpert.serve();
anExpert = new Expert(new Cooking()); //this expert is Chef
anExpert.serve();
anExpert = new Expert(new Painting()); //this expert is Painter
anExpert.serve();
}
}
</code>
I am amazed at the following way transforming a Class into different
Class on the fly(Expert becomes Singer, Chef or Painter). Could you
throw in some comment for me? Is this called Strategy design pattern?
Now I do feel "has-a" is more powerful than "is-a".
Thank you very much.
<code>
interface Talent
{
void showTalent();
}
class Singing implements Talent
{
public void showTalent()
{
System.out.println("do-rei-mei");
}
}
class Painting implements Talent
{
public void showTalent()
{
System.out.println("yellow, blue, red");
}
}
class Cooking implements Talent
{
public void showTalent()
{
System.out.println("gong-bao chicken");
}
}
//Skills of Expert is undecided right now. His skills will be set on the
fly. I am truely amazed at how powerful this technique it is.
public class Expert {
private Talent myTalent = null; //undecided yet
public Expert(Talent t)
{
myTalent = t;
}
public void serve()
{
myTalent.showTalent();
}
public static void main(String[] args)
{
Expert anExpert = new Expert(new Singing()); //this expert is singer
anExpert.serve();
anExpert = new Expert(new Cooking()); //this expert is Chef
anExpert.serve();
anExpert = new Expert(new Painting()); //this expert is Painter
anExpert.serve();
}
}
</code>