单例模式
- 饿汉式
#1
class Singleton{
private final static Singleton instance = new Singleton();
public static Singleton getInstance(){
return instance;
}
}
#2
public class Singleton{
private static Singleton instance;
static {
instance = new Singleton();
}
public static Singleton getInstance(){
return instance;
}
}
- 懒汉式
#1 线程不安全
public class Singleton{
private static Singleton instance;
public static Singleton getInstance(){
if(instance == null)
instance = new Singleton();
return instance;
}
}
#2 线程安全 效率低
public class Singleton{
private static Singleton instance;
public static synchronized Singleton getInstance(){
if(instance == null)
instance = new Singleton();
return instance;
}
}
#3 双重检测
public class Singleton{
private static volatile Singleton instance;
public static synchronized Singleton getInstance(){
if(instance == null){
synchronized (Singleton.class){
if(instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}
#4 静态内部类
class Singleton{
private static class SingletonInstance{
private static final Singleton instance = new Singleton();
}
public static synchronized Singleton getInstance(){
return SingletonInstance.instance;
}
}
工厂模式
#简单工厂模式
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
public class ShapeFactory {
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
}
#抽象工厂模式
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
public interface Color {
void fill();
}
public class Red implements Color {
@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape) ;
}
public class ShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("Rectangle")){
return new Rectangle();
}
return null;
}
@Override
public Color getColor(String color) {
return null;
}
}
public class ColorFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
return null;
}
@Override
public Color getColor(String color) {
if(color == null){
return null;
}
if(color.equalsIgnoreCase("RED")){
return new Red();
}
return null;
}
}
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
} else if(choice.equalsIgnoreCase("COLOR")){
return new ColorFactory();
}
return null;
}
}
原型模式
class Target implements Cloneable{
Target(String name,int age){
this.name = name;
this.age = age;
}
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
//默认自动拷贝基本数据类型
@Override
protected Object clone() throws CloneNotSupportedException {
Target target = null;
target = (Target)super.clone();
return target;
}
}
public static void main(String[] args) throws CloneNotSupportedException {
Target target = new Target("Tom",1);
Target clone_target = (Target)target.clone();
System.out.println(clone_target.getName()+":"+clone_target.getAge());
}
Tom:1
深度拷贝
class DeepCloneTarget implements Serializable,Cloneable{
private String name;
DeepCloneTarget(String name){
this.name = name;
}
public String getName() {
return name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Target implements Serializable,Cloneable{
private String name;
public DeepCloneTarget deepCloneTarget;
Target(String name){
this.name = name;
}
public String getName() {
return name;
}
//普通深度拷贝
@Override
protected Object clone() throws CloneNotSupportedException {
Target target = null;
target = (Target) super.clone();
target.deepCloneTarget = (DeepCloneTarget) deepCloneTarget.clone();
return target;
}
//序列化深度拷贝
public Object deepClone() throws IOException {
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try{
//序列化
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);
//反序列化
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
Target target = (Target) ois.readObject();
return target;
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
bos.close();
oos.close();
bis.close();
ois.close();
}
}
}
public static void main(String[] args) throws CloneNotSupportedException, IOException {
Target target = new Target("target");
target.deepCloneTarget = new DeepCloneTarget("deepTarget");
Target clone_target = (Target)target.clone();
Target deep_clone_target = (Target)target.deepClone();
System.out.println(clone_target.getName()+":"+clone_target.deepCloneTarget.getName());
System.out.println(deep_clone_target.getName()+":"+deep_clone_target.deepCloneTarget.getName());
}
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!