1. 单例模式

1.1 饿汉式

1
2
3
4
5
6
7
8
9
10
public class Singleton
{
private static Singleton instance = new Singleton();
//私有构造方法
private Singleton(){};
public static Singleton getInstance()
{
return instance;
}
}

1.2 懒汉式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Singleton
{
private static Singleton instance = null;
//私有构造方法
private Singleton(){};
//synchronized 保证线程安全
synchronized public static Singleton getInstance()
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}
}

2. 工厂模式

2.1 简单工厂模式

1
2
3
4
5
//Shape接口
public interface Shape
{
public void draw();
}
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
//具体的图形,实现Shape接口
class CircleShape implements Shape{
public CircleShape()
{
System.out.println("CircleShape Create!");
}
@Override
public void draw()
{
System.out.println("Draw CircleShape");
}
}
class RectShape implements Shape{
public RectShape()
{
System.out.println("RectShape Create!");
}
@Override
public void draw()
{
System.out.println("Draw RectShape");
}
}
class Trianglehape implements Shape{
public Trianglehape()
{
System.out.println("Trianglehape Create!");
}
@Override
public void draw()
{
System.out.println("Draw Trianglehape");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//工厂类具体实现
class ShapeFactory{
public static Shape getShape(String type){
if(type.equalsIgnoreCase("circle")){
Shape shape = new CircleShape();
}
else if(type.equalsIgnoreCase("rect")){
Shape shape - new RectShape();
}
else if(type.equalsIgnoreCase("triangle")){
Shape shape = new TriangleShape();
}
return shape;
}
}

2.2 工厂方法模式

每个对象都有对应的工厂方法,

1
2
3
4
5
//Draw接口
public interface Shape
{
public void draw();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//具体的图形,实现Shape接口
class CircleShape implements Shape{
@Override
public void draw()
{
System.out.println("Draw CircleShape");
}
}
class RectShape implements Shape{
@Override
public void draw()
{
System.out.println("Draw RectShape");
}
}
class Trianglehape implements Shape{
@Override
public void draw()
{
System.out.println("Draw Trianglehape");
}
}

1
2
3
4
//工厂接口
interface ShapeFactory{
Shape DrawShape();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//画每个图形的工厂类实现总的工厂接口,返回对应图形的创建器对象
public class DrawCricle implements ShapeFactory{
@Override
public Shape DrawShape(){
return new CricleShape();
}
}
public class DrawRect implements ShapeFactory{
@Override
public Shape DrawShape(){
return new RectShape();
}
}
public class DrawTriangle implements ShapeFactory{
@Override
public Shape DrawShape(){
return new TriangleShape();
}
}

3. 策略模式

1
2
3
4
5
//抽象策略,为策略对象定义一个接口
public abstract class Strategy{
//定义两个数可以计算
public abstract int calc(int num1,int num2);
}
1
2
3
4
5
6
7
8
9
10
11
12
//定义加法策略
public class AddStrategy extends Strategy{
public int calc(int num1,int num2){
return num1+num2;
}
}
//定义减法策略
public class SubStrategy extends Strategy{
public int calc(int num1,int num2){
return num1-num2;
}
}
1
2
3
4
5
6
7
8
9
10
//环境角色
public class Context{
private Strategy strategy = null;
public Context(Strategy strategy){
this.strategy = strategy;
}
public int calulate(int a, int b){
this.strategy.calc(a,b);
}
}
1
2
3
4
5
6
7
8
9
10
//测试
public class TestStrategy {
public static void main(String[] args) {
Context e = new Context(new AddStrategy());//传入具体的策略类
System.out.println("a+b="+e.calulate(5, 7));

Context r = new Context(new SubStrategy());
System.out.println("a-b="+r.calulate(9,3));
}
}

4. 观察者模式

1
2
3
4
5
//抽象观察者
public interface Observer{
//更新方法
void update();
}
1
2
3
4
5
6
7
//具体观察者
public class ConcreteObserver implements Observer{
@Override
public void update(){
System.out.println("观察者接收到信息,更新。");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//抽象被观察者
public abstract class Subject{
// 定义一个被观察者数组
private List<Observer> obsList = new ArrayList<>();
//添加观察者
public void addObserver(Observer observer){
obsList.add(observer);
}
//删除观察者
public void delObserver(Observer observer){
obsList.remove(observer);
}
// 通知所有观察者
public void notifyObservers(){
for (Observer observer : obsList){
observer.update();
}
}
}
1
2
3
4
5
6
7
//具体观察者
public ConcreteSubject extends Subject{
// 具体的业务
public void doSomething(){
super.notifyObservers();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
//测试
public class ObserverClient {
public static void main(String[] args) {
// 创建一个被观察者
ConcreteSubject subject = new ConcreteSubject();
// 定义一个观察者
Observer observer = new ConcreteObserver();
// 观察者观察被观察者
subject.addObserver(observer);
subject.doSomething();
}
}

5. 责任链模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//抽象处理者
abstract class Handler{
private Handler successor;
public abstract void handlerRequest();
public Handler getSuccessor(){
return successor;
}
public void setSuccessor(Handler successor){
this.successor = successor;
}
}
//具体处理者
class ConcreteHandler extends Handler{
//处理请求
public void handlerRequest(){
if(getSuccessor() != null){
System.out.println("请求传递给"+getSuccessor());
getSuccessor().handlerRequest();
}
else{
System.out.println("请求处理");
}
}
}