Tôi khá mới với các mẫu và tôi đang nghiên cứu Mẫu trang trí cho một chương trình tôi phải viết.Thiết kế mẫu trang trí
Nghiên cứu trên mạng, tôi thấy một ví dụ về mẫu Decorator (nó là Java pseudo-code):
class Solution1
{
static interface Component
{
void doStuff();
}
static class MyComponent implements Component
{
public void doStuff()
{
// ...
}
}
static class ComponentDecorator implements Component // This is the Decorator pattern.
{
private final Component component;
public ComponentDecorator(Component component)
{
this.component = component;
}
public void doStuff()
{
this.component.doStuff();
}
}
static class ComponentDecorator1 extends ComponentDecorator
{
public ComponentDecorator1(Component component)
{
super(component);
}
private void doExtraStuff1()
{
// ...
}
public void doStuff()
{
super.doStuff();
doExtraStuff1();
}
}
static class ComponentDecorator2 extends ComponentDecorator
{
public ComponentDecorator2(Component component)
{
super(component);
}
private void doExtraStuff2()
{
// ...
}
public void doStuff()
{
super.doStuff();
doExtraStuff2();
}
}
public static void main(String[] args)
{
MyComponent c = new MyComponent();
ComponentDecorator1 cd1 = new ComponentDecorator1(c);
ComponentDecorator2 cd2 = new ComponentDecorator2(cd1);
cd2.doStuff(); // Executes Component.doStuff, ComponentDecorator1.doExtraStuff1, ComponentDecorator2.doExtraStuff2
}
};
Khi tôi phân tích ví dụ này, tôi nhận ra rằng trong quá khứ tôi đã làm một mô hình rất giống nhau nhưng theo cách khác nhau:
import java.util.*;
class Solution2
{
static interface Component
{
void doStuff();
}
static class MyComponent implements Component
{
public void doStuff()
{
// ...
}
}
static class ComponentDecorator implements Component // This is NOT the Decorator pattern!
{
private final List<Component> components = new ArrayList<Component>();
public ComponentDecorator()
{
}
public ComponentDecorator addComponent(Component component)
{
this.components.add(component);
return this;
}
public void removeComponent(Component component) // Can Decorator do this?
{
// ...
}
public void doStuff()
{
for(Component c : this.components) c.doStuff();
}
}
static class ComponentDecorator1 implements Component
{
public ComponentDecorator1()
{
}
private void doExtraStuff1()
{
// ...
}
public void doStuff()
{
doExtraStuff1();
}
}
static class ComponentDecorator2 implements Component
{
public ComponentDecorator2()
{
}
private void doExtraStuff2()
{
// ...
}
public void doStuff()
{
doExtraStuff2();
}
}
public static void main(String[] args)
{
ComponentDecorator cd = new ComponentDecorator();
cd.addComponent(new MyComponent());
cd.addComponent(new ComponentDecorator1());
cd.addComponent(new ComponentDecorator2());
cd.doStuff(); // Executes MyComponent.doStuff, ComponentDecorator1.doExtraStuff1, ComponentDecorator2.doExtraStuff2
}
}
theo tôi, ví dụ thứ hai có thể được sử dụng trong cùng một tình huống mà một mẫu trang trí có thể được sử dụng, nhưng nó linh hoạt hơn (bạn có thể, bởi ví dụ, loại bỏ hoặc sắp xếp lại các thành phần trong danh sách), vì vậy câu hỏi của tôi:
- Giải pháp 1 (Mẫu trang trí chính xác) có tốt hơn giải pháp 2 không? Tại sao?
- Có thể thêm các chức năng để xóa các phiên bản trong giải pháp 1 không?
- Có thể thêm các chức năng để sắp xếp lại các thể hiện trong giải pháp 1 không?
nó là không thể nói đó là tốt hơn bởi vì họ làm những việc khác nhau. –
Dường như với tôi rằng họ cũng làm như vậy. Bạn có thể cụ thể hơn không? – Carlo
Trang trí là về việc mở rộng một triển khai khác bằng cách ** gói ** nó trong mã mở rộng. Giải pháp của bạn chỉ gọi các triển khai khác nhau theo trình tự. Đây là [câu trả lời của tôi về trang trí] (http://stackoverflow.com/questions/12379848/decorator-design-pattern-vs-inheritance/12379948#12379948). Cố gắng thực hiện điều đó bằng cách tiếp cận thay đổi mà bạn mô tả và bạn sẽ thấy sự khác biệt –