我正在学习GoF Java设计模式,我想看看它们的一些真实例子.Java核心库中这些设计模式的一些很好的例子是什么?
我一直在博客中看到对访客模式的引用,但我必须承认,我只是不明白.我阅读了维基百科文章的模式,我理解它的机制,但我仍然对我何时使用它感到困惑.
作为最近刚刚获得装饰模式的人,现在看到它在任何地方的用途我都希望能够直观地理解这个看似方便的模式.
我正在研究GOF中记载的装饰模式.
请帮我理解装饰模式.有人可以给出一个用例,说明这在现实世界中有用吗?
我正在研究我的设计模式,我在编码中尚未认真使用的一种模式是装饰模式.
我理解这种模式,但我想知道的是现实世界中一些具体的例子,装饰者模式是最佳/最佳/优雅的解决方案.需要装饰器模式的特定情况非常方便.
谢谢.
Composite Pattern和Decorator Pattern有什么区别?
什么时候需要使用装饰模式?如果可能的话,给我一个非常适合该模式的真实世界示例.
我有以下界面.
PowerSwitch.java
public interface PowerSwitch {
public boolean powerOn();
public boolean powerOff();
public boolean isPowerOn();
}
Run Code Online (Sandbox Code Playgroud)
上面的接口应该包含可以从中派生出任何其他功能的最小方法集,以便尽可能简单地添加其他PowerSwitch实现.
我想在运行时向PowerSwitch接口添加功能(装饰器做什么),创建一个包含PowerSwitch实例组合的类并添加新方法,如下面的两个toggleOnOff()方法.这样我只需要实现两次切换方法,它将适用于所有PowerSwitch实现.
这被认为是一种好/坏的做法吗?如果不好,还有其他建议吗?
它并不真正符合装饰器模式,因为它增加了额外的方法.它是策略模式还是组合模式?或者它有另一个模式名称?有"界面装饰"这样的东西吗?
PowerSwitchDecorator.java
public class PowerSwitchDecorator {
private PowerSwitch ps;
public PowerSwitchDecorator(PowerSwitch ps) {
this.ps = ps;
}
public void toggleOnOff(int millis) throws InterruptedException{
powerOn();
Thread.sleep(millis);
powerOff();
}
public void toggleOnOff(){
powerOn();
powerOff();
}
public boolean powerOn() {
return ps.powerOn();
}
public boolean powerOff() {
return ps.powerOff();
}
public boolean isPowerOn() {
return ps.isPowerOn();
}
}
Run Code Online (Sandbox Code Playgroud) 我经常将文本输出到文件.我想知道:BufferedWriter工作怎么样?
我打电话时是否在文件上写文字writer.write(text)?如果它不写文本,我是否需要使用flush函数来写入数据?
例如:
File file = new File("statistics.txt");
if (!file.exists()) {
file.createNewFile();
}
else
{
file.delete();
file.createNewFile();
}
FileWriter fileWritter = new FileWriter(file.getAbsoluteFile(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
Iterator<Map.Entry<String,Token>> it = listofTakenPairMap.entrySet().iterator();
int isim_isim=0;
int isim_fiil=0;
int zarf_fiil=0;
while (it.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry pairs = (Map.Entry)it.next();
Token token=(Token)pairs.getValue();
String str=pairs.getKey()+ " = " +token.getCount();
if(token.getTypeOfPairofToken()==0){//isim-isim
isim_isim+=token.getCount();
}
else if(token.getTypeOfPairofToken()==1){
isim_fiil+=token.getCount();
}
else{ //zarf-fiil
zarf_fiil+=token.getCount();
}
System.out.println(str);
bufferWritter.write(str);
bufferWritter.newLine();
//it.remove(); // avoids a ConcurrentModificationException
}
bufferWritter.newLine();
bufferWritter.write("##############################"); …Run Code Online (Sandbox Code Playgroud) 我正在阅读方法Collections.synchronizedList()的实现,并对它是装饰模式还是代理模式的一个例子感到困惑?
我正在阅读这篇文章:
http://www.codeproject.com/Articles/479635/UnderstandingplusandplusImplementingplusDecoratorp
我正在考虑在学校项目中实施这种模式。这不是必需的,所以我可以半途而废。但是,我只是认为这将是扩展我的知识和专业知识的好机会。
学校项目是这样的:创建一个披萨订购应用程序,员工可以在其中输入客户的订单。所以比萨饼,它可以有任意数量的浇头。
上面的文章(以及Head First: Design Patterns一书中的描述)似乎与我的应用程序完美匹配。
这是我的问题:这似乎不是一个好的模式,原因如下:
每当“比萨店”在他们的菜单上添加一个新的配料时……他们将不得不添加一个全新的类,并重新编译他们的订购系统并重新分发它们?
我认为问题可能在于我在谷歌上搜索的所有示例都必须处理某种食物和浇头。
我使用FileWriter和BufferWriter写入文件.创建了文件"test.txt",但没有写入任何内容.
该文件应该写在我的按钮的ActionEvent中.那是什么原因?
那是我的代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class Cadre_fenetreA2 extends JFrame
{
JLabel phrase = new JLabel("Veuillez indiquer le nom de chaque groupe de generalisation pour chaque Niveau");
JButton boutonOK = new JButton ("OK");
public Cadre_fenetreA2 (String nom,int X, int Y, int lo, int la, int Na, int [] nbGrpGen,int maxnbGrpGen, File file)
{
super(nom);
setBounds(X,Y,lo,la);
setVisible(true);
JTextField[][] allField = …Run Code Online (Sandbox Code Playgroud) 我有基于Java的Spring MVC应用程序,它也使用Spring安全性.我正在使用hibernate作为此Web应用程序的ORM工具.
以下是我的要求 -
用户可以使用Web浏览器上传CSV文件.已知CSV文件的格式包含以下5个字段:
userId, location, itemId, quantity, tranDate 001, NY, 00A8D5, 2, 12/31/2012 002, MN, 00A7C1, 10, 12/22/2012 . .
像这样大约有100行.
我在项目中使用Super CSV:
private void readWithCsvBeanReader(String CSV_FILENAME) throws Exception {
String CSV_FILENAME = "\\\\Serv01\\Files\\QueryResult.csv";
//String CSV_FILENAME = "C:\\Files\\QueryResult.csv";
ICsvBeanReader beanReader = null;
try {
beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME),
CsvPreference.STANDARD_PREFERENCE);
// the header elements are used to map the values to the bean (names
// must match)
final String[] header = beanReader.getHeader(true);
// get Cell Processor
final CellProcessor[] …Run Code Online (Sandbox Code Playgroud) 我调查了java I/O. 现在我正在读管道.
我写了最简单的代码示例:
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
pipedOutputStream.connect(pipedInputStream);
pipedOutputStream.write(new byte[]{1});
System.out.println(pipedInputStream.read());
Run Code Online (Sandbox Code Playgroud)
我有以下问题.据我了解 - 在现实生活中传递字节是非常奇怪的.
例如,为了传递整个String,是否真的要扩展此示例?
java ×7
decorator ×6
concurrency ×2
file ×2
buffer ×1
composite ×1
file-upload ×1
io ×1
java-api ×1
oop ×1
spring ×1
streamwriter ×1
supercsv ×1