package ioEx;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class Serial {
private static final String TMP_FILE = "text.txt";
public static void main(String[] args) {
testWrite();
testRead();
}
private static void testWrite() {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(TMP_FILE));
AnObject box = new AnObject(1, 1, "1");
out.writeObject(box);
out.writeBoolean(true);
out.writeByte((byte)65);
out.writeChar('a');
out.writeInt(20160415);
out.writeFloat(3.14F);
out.writeDouble(Math.PI);
HashMap<String,String> map = new HashMap<String,String>();
map.put("a", "a");
map.put("b", "b");
map.put("c", "c");
out.writeObject(map);
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void testRead() {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(TMP_FILE));
AnObject box = (AnObject) in.readObject();
System.out.println("testWrite box: " + box);
System.out.println("boolean:"+ in.readBoolean());
System.out.println("byte:" + (in.readByte()&0xff));
System.out.println("char:" + in.readChar());
System.out.println("int:" + in.readInt());
System.out.println("float:" + in.readFloat());
System.out.println("double:" + in.readDouble());
// 读取HashMap对象
HashMap<String,String> map = (HashMap<String,String>) in.readObject();
Iterator<Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String,String> entry = (Entry<String, String>)iter.next();
System.out.println(entry.getKey()+"--"+ entry.getValue());
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class AnObject implements Serializable {
private int obja;
private int objb;
private String objc;
private static int statica;
private transient int transienda;
//必须用static或transient修饰才可能序列化,否则运行报错
private static transient Thread thread = new Thread() {
@Override
public void run() {
System.out.println("Serializable");
}
};
public AnObject(int obja, int objb, String objc) {
this.obja = obja;
this.objb = objb;
this.objc = objc;
this.statica=obja;
this.transienda=obja;
}
//如果要使transient序列化要重写writeObject,和readObject 方法
// private void writeObject(ObjectOutputStream out) throws IOException{
// out.defaultWriteObject();
// out.writeInt(transienda);
// }
//
// private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException{
// in.defaultReadObject();
// transienda = in.readInt();
// }
@Override
public String toString() {
return "obja:"+obja+","+ "objb:"+objb+","+ "objc:"+objc+","+ "statica:"+statica+","+ "transienda:"+transienda;
}
}JAVA IO的设计模式 package Test;
public class Wrapper {
public static void main(String[] args) {
Component c = new RealizeComponent();
Component c1 = new RealizeDecorator1(c);
c1.say();
System.out.println("--");
Component c2 = new RealizeDecorator2(c1);
c2.say();
}
}
interface Component{
public void say();
}
class RealizeComponent implements Component{
@Override
public void say() {
System.out.println("A");
}
}
class Decorator implements Component{
private Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public void say(){
component.say();
}
}
class RealizeDecorator1 extends Decorator{
public RealizeDecorator1(Component component){
super(component);
}
@Override
public void say(){
super.say();
this.sayAnother();
}
private void sayAnother(){
System.out.println("B");
}
}
class RealizeDecorator2 extends Decorator{
public RealizeDecorator2(Component component){
super(component);
}
@Override
public void say(){
super.say();
this.sayAnother();
}
private void sayAnother(){
System.out.println("C");
}
}· 装饰模式的优点package AdapterEx;
class Apple {
public void getAColor(String str) {
System.out.println("Apple color is: " + str);
}
}
class Orange {
public void getOColor(String str) {
System.out.println("Orange color is: " + str);
}
}
class AppleAdapter extends Apple {
private Orange orange;
public AppleAdapter(Orange orange) {
this.orange = orange;
}
public void getAColor(String str) {
orange.getOColor(str);
}
}
public class AdapterEx {
public static void main(String[] args) {
Apple apple = new Apple();
apple.getAColor("green");
Orange orange = new Orange();
AppleAdapter aa = new AppleAdapter(orange);
aa.getAColor("red");
}
}Java I/O 库大量使用了适配器模式,例如 ByteArrayInputStream 是一个适配器类,它继承了 InputStream 的接口,并且封装了一个 byte 数组。换言之,它将一个 byte 数组的接口适配成 InputStream 流处理器的接口。ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。