1.工厂模式

1.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
public interface Sender {
public void Send();
}

public class MailSender implements Sender {
@Override
public void Send() {
System.out.println("this is mail sender!");
}
}

public class SmsSender implements Sender {
@Override
public void Send() {
System.out.println("this is sms sender!");
}
}

public class SendFactory {
public Sender produce(String type) {
if ("mail".equals(type)) {
return new MailSender();
} else if ("sms".equals(type)) {
return new SmsSender();
} else {
System.out.println("请输入正确的类型!");
return null;
}
}
}
1.2 多个工厂方法模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class SendFactory {
public Sender produceMail() {
return new MailSender();
}

public Sender produceSms() {
return new SmsSender();
}
}

public class FactoryTest {
public static void main(String[] args) {
SendFactory factory = new SendFactory();
Sender sender = factory.produceMail();
sender.send();
}
}
1.3 静态工厂方法模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class SendFactory {
public static Sender produceMail() {
return new MailSender();
}

public static Sender produceSms() {
return new SmsSender();
}
}

public class FactoryTest {
public static void main(String[] args) {
Sender sender = SendFactory.produceMail();
sender.send();
}
}
1.4 抽象工厂模式

工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,这违背了闭包原则,所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码。

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
public interface Provider {
public Sender produce();
}

public interface Sender {
public void send();
}

public class MailSender implements Sender {
@Override
public void send() {
System.out.println("this is mail sender!");
}
}

public class SmsSender implements Sender {
@Override
public void send() {
System.out.println("this is sms sender!");
}
}

public class SendSmsFactory implements Provider {
@Override
public Sender produce() {
return new SmsSender();
}
}

public class SendMailFactory implements Provider {
@Override
public Sender produce() {
return new MailSender();
}
}

public class Test {
public static void main(String[] args) {
Provider provider = new SendMailFactory();
Sender sender = provider.produce();
sender.send();
}
}

2.建造者模式

1
2
3
4
5
6
7
// 角色Builder
public interface PersonBuilder {
void buildHead();
void buildBody();
void buildFoot();
Person buildPerson();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ManBuilder implements PersonBuilder {
Person person;
public ManBuilder() {
person = new Man();
}
public void buildbody() {
person.setBody("建造男人的身体");
}
public void buildFoot() {
person.setFoot("建造男人的脚");
}
public void buildHead() {
person.setHead("建造男人的头");
}
public Person buildPerson() {
return person;
}
}

3.适配器模式

3.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
public class Source {
public void method1() {
System.out.println("this is original method!");
}
}

public interface Targetable {
/* 与原类中的方法相同 */
public void method1();

/* 新类的方法 */
public void method2();
}

public class Adapter extends Source implements Targetable {
@Override
public void method2() {
System.out.println("this is the targetable method!");
}
}

public class AdapterTest {
public static void main(String[] args) {
Targetable target = new Adapter();
target.method1();
target.method2();
}
}
3.2 对象的适配器模式

基本思路和类的适配器模式相同,只是将 Adapter 类作修改,这次不继承Source 类,而是持有 Source 类的实例,以达到解决兼容性的问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Wrapper implements Targetable {
private Source source;

public Wrapper(Source source) {
super();
this.source = source;
}

@Override
public void method2() {
System.out.println("this is the targetable method!");
}

@Override
public void method1() {
source.method1();
}
}
3.3 接口的适配器模式

接口的适配器是这样的:有时我们写的一个接口中有多个抽象方法,当我们写该接口的实现类时,必须实现该接口的所有方法,这明显有时比较浪费,因为并不是所有的方法都是我们需要的,有时只需要某一些,此处为了解决这个问题,我们引入了接口的适配器模式,借助于一个抽象类,该抽象类实现了该接口,实现了所有的方法,而我们不和原始的接口打交道,只和该抽象类取得联系,所以我们写一个类,继承该抽象类,重写我们需要的方法就行。

4.装饰模式(Decorator)

装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例。

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
public interface Sourceable {
public void method();
}

public class Source implements Sourceable {
@Override
public void method() {
System.out.println("the original method!");
}
}

public class Decorator implements Sourceable {
private Sourceable source;

public Decorator(Sourceable source) {
super();
this.source = source;
}

@Override
public void method() {
System.out.println("before decorator!");
source.method();
System.out.println("after decorator!");
}
}

public class DecoratorTest {
public static void main(String[] args) {
Sourceable source = new Source();
Sourceable obj = new Decorator(source);
obj.method();
}
}

5.策略模式(strategy)

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
public interface ICalculator {
public int calculate(String exp);
}

public class Minus extends AbstractCalculator implements ICalculator {
@Override
public int calculate(String exp) {
int arrayInt[] = split(exp, "-");
return arrayInt[0] - arrayInt[1];
}
}

public class Plus extends AbstractCalculator implements ICalculator {
@Override
public int calculate(String exp) {
int arrayInt[] = split(exp, "\\+");
return arrayInt[0] + arrayInt[1];
}
}

public class AbstractCalculator {
public int[] split(String exp, String opt) {
String array[] = exp.split(opt);
int arrayInt[] = new int[2];
arrayInt[0] = Integer.parseInt(array[0]);
arrayInt[1] = Integer.parseInt(array[1]);
return arrayInt;
}
}

public class StrategyTest {
public static void main(String[] args) {
String exp = "2+8";
ICalculator cal = new Plus();
int result = cal.calculate(exp);
System.out.println(result);
}
}

6.观察者模式(Observer)

观察者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public interface Observer {
public void update();
}

public class Observer1 implements Observer {
@Override
public void update() {
System.out.println("observer1 has received!");
}
}

public class Observer2 implements Observer {
@Override
public void update() {
System.out.println("observer2 has received!");
}
}

被观察者

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
public interface Subject {
/*增加观察者*/
public void add(Observer observer);

/*删除观察者*/
public void del(Observer observer);

/*通知所有的观察者*/
public void notifyObservers();

/*自身的操作*/
public void operation();
}

public abstract class AbstractSubject implements Subject {
private Vector<Observer> vector = new Vector<Observer>();

@Override
public void add(Observer observer) {
vector.add(observer);
}

@Override
public void del(Observer observer) {
vector.remove(observer);
}

@Override
public void notifyObservers() {
Enumeration<Observer> enumo = vector.elements();
while (enumo.hasMoreElements()) {
enumo.nextElement().update();
}
}
}

public class MySubject extends AbstractSubject {
@Override
public void operation() {
System.out.println("update self!");
notifyObservers();
}
}

测试代码

1
2
3
4
5
6
7
8
public class ObserverTest {
public static void main(String[] args) {
Subject sub = new MySubject();
sub.add(new Observer1());
sub.add(new Observer2());
sub.operation();
}
}