Em có đọc quyển sách về Design Pattern for Dummies kiểu decorator thấy rất hay, nhưng e chỉ mới học c++ lập trình hướng đối tượng thui nên không biết code java này nếu chuyển qua c++ sẽ ntn. Nên mong các bác giúp e.
public class computer
{
public computer ()
{
}
public string description ()
{
return "computer";
}
public abstract class ComponentDecorator extends Computer
{
public abstract string description();
}
public class Disk extends ComponentDecorator
{
Computer computer;
public Disk (computer c)
{
computer = c;
}
public string description ()
{
return computer.description() + "and a disk";
}
}
public class CD extends ComponentDecorator
{
Computer computer;
public CD (computer c)
{
computer = c;
}
public string description ()
{
return computer.description() + "and a CD";
}
}
public class Monitor extends ComponentDecorator
{
Computer computer;
public Monitor (computer c)
{
computer = c;
}
public string description ()
{
return computer.description() + "and a monitor";
}
}
public class Test
{
public static voidmain (String args[])
{
Computer computer = new computer();
computer = new Disk(computer);
computer = new Monitor(computer);
computer = new CD(computer);
computer = new CDcomputer);
system.out.println("You're getting a" + computer.description() + ".";
}
}
Nhấn để mở rộng...
public class computer
{
public computer ()
{
}
public string description ()
{
return "computer";
}
public abstract class ComponentDecorator extends Computer
{
public abstract string description();
}
public class Disk extends ComponentDecorator
{
Computer computer;
public Disk (computer c)
{
computer = c;
}
public string description ()
{
return computer.description() + "and a disk";
}
}
public class CD extends ComponentDecorator
{
Computer computer;
public CD (computer c)
{
computer = c;
}
public string description ()
{
return computer.description() + "and a CD";
}
}
public class Monitor extends ComponentDecorator
{
Computer computer;
public Monitor (computer c)
{
computer = c;
}
public string description ()
{
return computer.description() + "and a monitor";
}
}
public class Test
{
public static voidmain (String args[])
{
Computer computer = new computer();
computer = new Disk(computer);
computer = new Monitor(computer);
computer = new CD(computer);
computer = new CDcomputer);
system.out.println("You're getting a" + computer.description() + ".";
}
}
Nhấn để mở rộng...