我在维基百科中读到,Decorator模式用于.Net和Java IO类.
任何人都能解释一下这是如何使用的吗?它有一个可能的例子,它有什么好处?
维基百科上有一个Windows窗体的例子,但我想知道Java IO类是如何发生的.
我刚遇到两种模式.
战略模式
装饰
策略模式: -
策略模式提供了几种可用于执行特定操作或任务的算法.
装饰图案: -
装饰器模式为组件添加了一些功能.
事实上,我发现策略模式和装饰模式也可以互换使用.
这是链接: - 何时以及如何应用策略模式而不是装饰模式?
Strategy Pattern和Decorator Pattern有什么区别?
什么时候应该使用策略模式,何时应该使用Decorator模式?
用同一个例子解释两者之间的区别.
Composite Pattern和Decorator Pattern有什么区别?
据说建造者模式适合比萨实例.
为什么不装饰?将奶酪,意大利辣香肠,培根作为基础披萨上的附加装饰品.
是因为奶酪/意大利辣香肠必须单独建造.我不认为,它们需要单独构建,因为它们可以随时可用.
请澄清一下.我也在寻找装饰模式的一个很好的现实世界的例子,以及为什么它适合那个特定的例子.谢谢.
什么时候需要使用装饰模式?如果可能的话,给我一个非常适合该模式的真实世界示例.
我正在编写面向对象语言的文档,我想知道哪种类是继承的好例子.
一些常见的例子:
class Person {
}
class Employee extends Person {
}
Run Code Online (Sandbox Code Playgroud)
目前我最喜欢的,但我不喜欢Person-> Employee,因为'Employee'看起来不太有趣.
class Bicycle {
}
class MountainBike extends Bicycle {
}
Run Code Online (Sandbox Code Playgroud)
我在一些Java教程中发现了这一点,但是自行车的属性并不是很明显.
class Animal {
}
class Bird extends Animal {
}
Run Code Online (Sandbox Code Playgroud)
和自行车一样.
class A {
}
class B extends A {
}
Run Code Online (Sandbox Code Playgroud)
太抽象了.主要问题是这样的类需要更抽象的属性和方法.
有没有人有一个简单的类层次结构的更好的例子?
我们什么时候需要去适配器模式?如果可能的话,给我一个适合这种模式的真实世界的例子......
在java中,围绕一个减少超类功能的子类设计的好方法是什么?
例如,考虑具有"See"函数的类"Man"和不应该具有该函数的子类"BlindMan",但应该具有"Man"具有的所有其他内容.
我能想出的唯一解决方案是使用抽象类"Man"和两个子类"SeeingMan"和"BlindMan",SeeinMan添加了一个"See"功能.
但是这个解决方案的问题在于,如果我现在想要添加一个"DeafMan"类 - 它会扩展什么?SeeingMan?如果那个男人既聋又瞎眼怎么办?
我相信了解Decorator和Visitor设计模式的意图.
虽然我可以列出以下差异
当我深入思考时,我无法说服自己两者之间的真正区别.
我在Python中看到了decorator 示例:
def makebold(fn):
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped
def makeitalic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
@makebold
@makeitalic
def hello():
return "hello world"
print hello() ## returns <b><i>hello world</i></b>
Run Code Online (Sandbox Code Playgroud)
并且有些好奇它如何在Java中实现,所以我搜索并使用Decorator Design Pattern获得了一些示例.
public class Main {
public static void main(String[] args) {
Wrapper word = new BoldWrapper(new ItalicWrapper());
// display <b><i>hello world</i></b>
System.out.println(word.make("Hello World"));
}
}
public interface Wrapper {
public String make(String str);
}
public class …Run Code Online (Sandbox Code Playgroud) 任何人都可以为我精心设计Bridge设计模式和Decorator模式.我发现它在某种程度上相似.我不知道如何区分它?
我的理解是,在Bridge中,它将实现与接口分开,通常您只能应用一个实现.装饰器是一种包装,你可以尽可能多地包装.
例如,
桥模式
class Cellphone {
private:
Impl* m_OS; // a cellphone can have different OS
}
Run Code Online (Sandbox Code Playgroud)
装饰图案
class Shirt {
private:
Person * m_p; //put a shirt on the person;
}
Run Code Online (Sandbox Code Playgroud) 我对模式很新,我正在研究装饰模式,我必须编写一个程序.
在线学习,我找到了一个Decorator模式的例子(它是Java伪代码):
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()
{ …Run Code Online (Sandbox Code Playgroud)