工厂模式(Factory Method)

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
interface food{}

class A implements food{}
class B implements food{}
class C implements food{}

public class StaticFactory {

private StaticFactory(){}

public static food getA(){ return new A(); }
public static food getB(){ return new B(); }
public static food getC(){ return new C(); }
}

class Client{
//客户端代码只需要将相应的参数传入即可得到对象
//用户不需要了解工厂类内部的逻辑
public void get(String name){
food x = null ;
if ( name.equals("A")) {
x = StaticFactory.getA();
}else if ( name.equals("B")){
x = StaticFactory.getB();
}else {
x = StaticFactory.getC();
}
}
}

抽象工厂模式(Abstract Factory)

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
//静态工厂模式在不改动StaticFactory类的代码时无法新增产品,如果采用了抽象工厂模式,就可以轻松的新增拓展类。
interface food{}

class A implements food{}
class B implements food{}

interface produce{ food get();}

class FactoryForA implements produce{
@Override
public food get() {
return new A();
}
}
class FactoryForB implements produce{
@Override
public food get() {
return new B();
}
}
public class AbstractFactory {
public void ClientCode(String name){
food x= new FactoryForA().get();
x = new FactoryForB().get();
}
}

单例模式(Singleton)

1
2
3
4
5
6
7
8
9
10
public class Singleton {
private Singleton(){}

private static class SingletonBuild{
private static Singleton value = new Singleton();
}

public static Singleton getInstance(){ return SingletonBuild.value;}

}

建造者模式(Builder)

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
public class Builder {

static class Student{
String name = null ;
int number = -1 ;
String sex = null ;
int age = -1 ;
String school = null ;

     //构建器,利用构建器作为参数来构建Student对象
static class StudentBuilder{
String name = null ;
int number = -1 ;
String sex = null ;
int age = -1 ;
String school = null ;
public StudentBuilder setName(String name) {
this.name = name;
return this ;
}

public StudentBuilder setNumber(int number) {
this.number = number;
return this ;
}

public StudentBuilder setSex(String sex) {
this.sex = sex;
return this ;
}

public StudentBuilder setAge(int age) {
this.age = age;
return this ;
}

public StudentBuilder setSchool(String school) {
this.school = school;
return this ;
}
public Student build() {
return new Student(this);
}
}

public Student(StudentBuilder builder){
this.age = builder.age;
this.name = builder.name;
this.number = builder.number;
this.school = builder.school ;
this.sex = builder.sex ;
}
}

public static void main( String[] args ){
Student a = new Student.StudentBuilder().setAge(13).setName("LiHua").build();
Student b = new Student.StudentBuilder().setSchool("sc").setSex("Male").setName("ZhangSan").build();
}
}

原型模式(Protype)

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
//减少复杂对象的构建时间 clone后修改部分属性即可 必须实现Cloneable接口
public class Prototype implements Cloneable{

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
protected Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}

public static void main (String[] args){
Prototype pro = new Prototype();
Prototype pro1 = (Prototype)pro.clone();
}
}

适配器模式(Adapter)

1
2
3
4
5
6
/** 适配器模式的作用就是在原来的类上增加新功能。
* 1.定义适配器接口interface。
* 2.定义适配器类继承被适配者实现适配器接口。
* 3.直接调用适配器接口使用适配器。
* */
class adapter extends oldClass implements interface1

装饰模式(Decorator)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//增强原来的功能
interface Source{ void method();}

public class Decorator implements Source{
private Source source ;

public void decotate(){
System.out.println("decorate");
}

@Override
public void method() {
decotate();
source.method();
}
}

代理模式(Proxy)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
interface Source{ void method();}

class OldClass implements Source{
@Override
public void method() {}
}

class Proxy implements Source{
//被代理的类实例
private Source source = new OldClass();

//代理方法
void doSomething1(){}
void doSomething2(){}

@Override
public void method() {
doSomething1();
source.method();
doSomething2();
}
}

外观模式(Facade)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//顶级类包装初始化操作
public class Facade {
private subSystem1 subSystem1 = new subSystem1();
private subSystem2 subSystem2 = new subSystem2();
private subSystem3 subSystem3 = new subSystem3();

public void startSystem(){
subSystem1.start();
subSystem2.start();
subSystem3.start();
}

public void stopSystem(){
subSystem1.stop();
subSystem2.stop();
subSystem3.stop();
}
}

桥接模式(Bridge)

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
interface DrawAPI {
public void drawCircle(int radius, int x, int y);
}
class RedCircle implements DrawAPI {
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Circle[ color: red, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}
class GreenCircle implements DrawAPI {
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Circle[ color: green, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}

abstract class Shape {
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI){
this.drawAPI = drawAPI;
}
public abstract void draw();
}

class Circle extends Shape {
private int x, y, radius;

public Circle(int x, int y, int radius, DrawAPI drawAPI) {
super(drawAPI);
this.x = x;
this.y = y;
this.radius = radius;
}

public void draw() {
drawAPI.drawCircle(radius,x,y);
}
}

//客户端使用代码
Shape redCircle = new Circle(100,100, 10, new RedCircle());
Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
redCircle.draw();
greenCircle.draw();

组合模式(Composite)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
abstract class component{}

class File extends component{ String filename;}

class Folder extends component{
component[] files ; //既可以放文件File类,也可以放文件夹Folder类。Folder类下又有子文件或子文件夹。
String foldername ;
public Folder(component[] source){ files = source ;}

public void scan(){
for ( component f:files){
if ( f instanceof File){
System.out.println("File "+((File) f).filename);
}else if(f instanceof Folder){
Folder e = (Folder)f ;
System.out.println("Folder "+e.foldername);
e.scan();
}
}
}

}

享元模式(Flyweight)

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 flywei{ }

public class Flyweight extends flywei{
Object obj ;
public Flyweight(Object obj){
this.obj = obj;
}
}

class FlyweightFactory{
private HashMap<Object,Flyweight> data;

public FlyweightFactory(){ data = new HashMap<>();}

public Flyweight getFlyweight(Object object){
if ( data.containsKey(object)){
return data.get(object);
}else {
Flyweight flyweight = new Flyweight(object);
data.put(object,flyweight);
return flyweight;
}
}
}