设计模式-Java

单例模式

  • 饿汉式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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;
}
}

工厂模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#简单工厂模式
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;
}
}

原型模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

深度拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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());
}

设计模式-Java
https://jiaopaner.github.io/2020/04/20/设计模式-Java/
作者
JiaoPan
发布于
2020年4月20日
许可协议