白盒框架和黑盒框架的区别

前言

在复习过程中遇到了关于白盒框架和黑盒框架的问题。
课堂上给出了例子,但是理解的不是很好,现总结以区分。

一、综述

总的说来,白盒框架与黑盒框架有这样的特点:

白盒框架

1.通过继承和重写方法进行扩展
2.设计模式:模板模式(Template Method)
3.总的说来,白盒框架把框架中的主要方法放在父类中,子类对其进行实现

黑盒框架

1.通过委派的方法进行扩展
2.设计模式:策略模式(Strategy)和观察者模式(Observer)
3.总的说来,黑盒框架通过实现API的方式来完成

二、例子

白盒框架
我们对课件中的例子加以分析。
在这里插入图片描述
我们需要在框架中实现的方法是
String getApplicationTitle();
String getButtonText();
String getInititalText();
void buttonClicked();

通过继承这些方法扩展框架
如:
实现一个计算器
在这里插入图片描述
重写一个Ping的显示器
在这里插入图片描述
通过这些不同的方法重写,可达到程序可复用性的要求。

黑盒框架

public class Application extends JFrame {
	private JTextField textField;
	private Plugin plugin;
	public Application() { }
	protected void init(Plugin p )
	p.setApplication(this);
	this.plugin = p;
	JPanel contentPane = new JPanel(new BorderLayout());
	contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
	JButton button = new JButton();
	button.setText(
	plugin != null ? plugin.getButtonText() : "ok"
	contentPane.add(button, BorderLayout.EAST);
	textField = new JTextField("");
	if (plugin != null)
	textField.setText(
	plugin.getInititalText()
	textField.setPreferredSize(new Dimension(200, 20));
	contentPane.add(textField, BorderLayout.WEST);
	if (plugin != null)
	button.addActionListener((e) --> { plugin.buttonClicked(); }
	this.setContentPane(contentPane);
	...
	}
	public String getInput() { return textField.getText(); }
}
public interface Plugin {
	String getApplicationTitle();
	String getButtonText();
	String getInititalText();
	void buttonClicked();
	void setApplication(Application App);
}

在这里插入图片描述
注意到这里和白盒测试例子中的区别。
通过一个委托的方法,通过Plugin接口,来对框架实现扩展。

第二个例子如下
白盒框架

public abstract class PrintOnScreen {
    public void print() { 
        JFrame frame = new JFrame(); 
        JOptionPane.showMessageDialog(frame, textToShow());
        frame.dispose();
    } 
    protected abstract String textToShow(); 
}

public class MyApplication extends PrintOnScreen {
@Override protected String textToShow() {
        return "printing this text on " + "screen using PrintOnScreen " + "white Box Framework"; 
    }
}

这里同样地使用了重写,MyApplication对父类对象进行了继承,重写了testToShow方法。

如果是黑盒框架:

public interface TextToShow { 
    String text(); 
}

public class MyTextToShow implements TextToShow {
    @Override 
    public String text() { 
        return "Printing"; 
    }
}

public final class PrintOnScreen {
    TextToShow textToShow;   
    
    public PrintOnScreen(TextToShow tx) { 
        this.textToShow = tx; 
    }
    
    public void print() { 
        JFrame frame = new JFrame(); 
        JOptionPane.showMessageDialog(frame, textToShow.text());
        frame.dispose(); 
    }
} 

调用的时候如下

PrintOnScreen m = new PrintOnScreen(new MyTextToShow()); 
m.print();

总结

白盒框架之所以说是白盒框架,是因为在这种框架中,父类的方法对子类可见。
子类可以通过继承或重写父类的方法来实现更具体的方法。

黑盒框架时基于委派的组合方式,是不同对象之间的组合。之所以是黑盒,是因为不用去管对象中的方法是如何实现的,只需关心对象上拥有的方法。

这种方式较白盒框架更为灵活,用户只须了解构件的外部接口,无须了解内部的具体实现,另外,组装比继承更为灵活,它能动态地改变,继承只是一个静态编译时的概念。 而继承机制在静态编译时就已经确定好,后续改变起来比较麻烦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值