code
//餓漢
public class A {
private static A instance = new A();
private A() {
}
public static A getInstance() {
return instance;
}
}
//懶漢
public class A {
private static A instance ;
public static A getInstance() {
if (instance == null){
synchronized (A.class){
if (instance == null){
instance = new A();
}
}
}
return instance;
}
}
//枚舉
public enum A {
ONE(1),TWO(2),THREE(3),FOUR(4),FIVE(5);
private int value;
A(int i) {
this.value = i;
}
public int getValue() {
return value;
}
}
//靜態(tài)內(nèi)部類
public class A {
private static class B{
private static A instance = new A();
}
public static A getInstance() {
return B.instance;
}
}
code
public interface IParseConfig {
void parse(String input);
}
public class XMLParse implements IParseConfig {
@Override
public void parse(String input) {
//xml 解析
}
}
public class JsonParse implements IParseConfig {
@Override
public void parse(String input) {
//Json 解析
}
}
public class PropertyParse implements IParseConfig {
@Override
public void parse(String input) {
//properties 解析
}
}
public class ParseFactory {
private static final HashMap<String,IParseConfig> cacheParse = new HashMap<>();
static {
cacheParse.put("json",new JsonParse());
cacheParse.put("xml",new XMLParse());
cacheParse.put("properties",new PropertyParse());
}
public void parse(String filePath){
//簡單工廠
String fileExtension = getFileExtension(filePath);
String input = "解析出來的文件流";
IParseConfig parseConfig;
if ("json".equals(fileExtension)){
parseConfig = new JsonParse();
}else if ("xml".equals(fileExtension)){
parseConfig = new XMLParse();
}else if ("properties".equals(fileExtension)){
parseConfig = new PropertyParse();
}else {
throw new UnsupportedOperationException("UnSupport format");
}
parseConfig.parse(input);
//節(jié)省創(chuàng)建時(shí)間 直接在緩存拿出來
IParseConfig parseConfig1 = cacheParse.get(fileExtension);
parseConfig1.parse(input);
//工廠方法
IParseFactory parseFactory;
if ("json".equals(fileExtension)){
parseFactory = new JsonParseFactory();
}else if ("xml".equals(fileExtension)){
parseFactory = new XMLParseFactory();
}else if ("properties".equals(fileExtension)){
parseFactory = new PropertyParseFactory();
}else {
throw new UnsupportedOperationException("UnSupport format");
}
parseConfig = parseFactory.createParse();
parseConfig.parse(input);
}
private String getFileExtension(String filePath){
return "json";
}
}
//xml properties相似 省略
public class JsonParseFactory implements IParseFactory {
@Override
public IParseConfig createParse() {
return new JsonParse();
}
}
//抽象工廠 工廠創(chuàng)建多個(gè)實(shí)例
public interface IConfigParseFactory {
IParseFactory createRuleParser();
ISystemFactory createSystemParser();
}
建造者模式
如果必填參數(shù)比較多,通過構(gòu)造函數(shù)不是很合適
各個(gè)參數(shù)有依賴關(guān)系,通過set不太好檢驗(yàn)
如果有需求臀规,對象創(chuàng)建之后,各個(gè)屬性不可變
public class Dialog {
private String title;
private int width;
private int height;
private Dialog(Builder builder) {
title = builder.title;
width = builder.width;
height = builder.height;
}
public static class Builder{
private String title;
private int width;
private int height;
public Builder setWidth(int width){
if (width < 0){
throw new UnsupportedOperationException("width should >= 0");
}
this.width = width;
return Builder.this;
}
public Builder setHeight(int height){
if (height < 0){
throw new UnsupportedOperationException("width should >= 0");
}
this.height = height;
return Builder.this;
}
public Builder setTitle(String title){
this.title = title;
return Builder.this;
}
public Dialog build(){
if (title.isEmpty()){
throw new IllegalArgumentException("title should not empty");
}
return new Dialog(this);
}
}
}
Dialog dialog = new Dialog.Builder()
.setTitle("標(biāo)題")
.setWidth(100)
.setHeight(100)
.build();