Yutt's Blog

面向对象(设计模式)

2018/03/29 Share

面向对象基础

面向对象的基本概念:

  • 对象
  • 消息
  • 继承
  • 多态
  • 动态绑定

面向对象分析(OOA)包含5个活动:

  • 认定对象
  • 组织对象
  • 对象的相互作用
  • 基于对象的操作
  • 定义对象的内部信息

面向对象设计(OOD)
遵循抽象、信息隐蔽、功能独立、模块化等设计原则。
1.面向对象设计的活动:

  • 识别类及对象
  • 定义属性
  • 定义服务
  • 识别关系
  • 识别包

2.面向对象的设计的原则

  • 单一责任原则
  • 开放-封闭原则
  • 依赖倒置原则
  • 接口分离原则
  • 重用发布等价原则
  • 共同封闭原则
  • 共同重用原则
  • 无环以来原则
  • 稳定依赖原则
  • 稳定抽象原则

创建型设计模式

工厂模式

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
// 产品接口
interface Product {
public void production();
}

// 具体产品
class clothes implements Product {
@Override
public void production() {
System.out.print("clothes");
}
}

// 具体产品
class toursers implements Product {
@Override
public void production() {
System.out.print("toursers");
}
}

// 工厂接口
interface Creator {
Product FactoryMethod(String produtType);
}

// 具体工厂
class ConcreleCreator implements Creator {
@Override
public Product FactoryMethod(String produtType) {
if (produtType == null) {
return null;
} else if (produtType.equalsIgnoreCase("CLOTHES")) {
return new clothes();
} else if (produtType.equalsIgnoreCase("TOURSERS")) {
return new toursers();
}
return null;
}
}

// 使用工厂
public class Main {

public static void main(String[] args) {
Product product = new ConcreleCreator().FactoryMethod("CLOTHES");
product.production();
}

}

工厂模式(反射)

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
// 工厂接口
interface Creator {
<T extends Product> T FactoryMethod(Class<T> produtType);
}

// 具体工厂
class ConcreleCreator implements Creator {
@Override
public <T extends Product> T FactoryMethod(Class<T> produtType) {
T result = null;
try {
result = (T) Class.forName(produtType.getName()).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}

public class Main {

public static void main(String[] args) {
Clothes clothes = new ConcreleCreator().FactoryMethod(Clothes.class);
clothes.production();
}

}

抽象工厂

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
// 抽象产品A
interface AbstructProductA {
void product();
}

// 具体产品A1
class ProductA1 implements AbstructProductA {
@Override
public void product() {
System.out.println("ProductA1");
}
}

// 具体产品A2
class ProductA2 implements AbstructProductA{
@Override
public void product() {
System.out.println("ProductA2");
}
}

// 抽象产品B
interface AbstructProductB {
void product();
}

// 具体产品B1
class ProductB1 implements AbstructProductB {
@Override
public void product() {
System.out.println("ProductB1");
}
}

// 具体产品B2
class ProductB2 implements AbstructProductB{
@Override
public void product() {
System.out.println("ProductB2");
}
}

// 抽象工厂
abstract class AbstractFactory {
abstract AbstructProductA createProductA(String productName);
abstract AbstructProductB createProductB(String productName);
}

// 具体工厂A
class ProductAFactory extends AbstractFactory {
@Override
public AbstructProductA createProductA(String productName) {
if (productName.equalsIgnoreCase("ProductA1")) {
return new ProductA1();
} else if (productName.equalsIgnoreCase("ProductA2")) {
return new ProductA2();
}
return null;
}

@Override
public AbstructProductB createProductB(String productName) {
return null;
}
}

// 具体工厂B
class ProductBFactory extends AbstractFactory {
@Override
public AbstructProductA createProductA(String productName) {
return null;
}

@Override
public AbstructProductB createProductB(String productName) {
if (productName == null) return null;
if (productName.equalsIgnoreCase("ProductB1")) {
return new ProductB1();
} else if (productName.equalsIgnoreCase("ProductB2")) {
return new ProductB2();
}
return null;
}
}

public class AbstractFactoryTest {
public static void main(String[] args) {
new ProductAFactory().createProductA("ProductA1").product();
}
}

单例模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Singleton {

private static Singleton uniqueInstance = new Singleton();

private Singleton() {}

public static Singleton getInstance() {
return uniqueInstance;
}

public void singletonOperation() {
System.out.println("Singleton");
}

public static void main(String[] args) {
Singleton object= Singleton.getInstance();
object.singletonOperation();
}
}

生成器模式

用简单对象创建复杂对象

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
interface  MyBuidler {
void buildPart();
}

interface MyProduct {
void productOperation();
}

class ConcreteBuilder implements MyBuidler {
private Product resultProduct;

public Product getResult() {
return resultProduct;
}

@Override
public void buildPart() {

}
}

class Director {

private MyBuidler builder;

public Director(MyBuidler builder) {
this.builder = builder;
}

public void construct() {
builder.buildPart();
}

}

原型模式

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
abstract class Shape implements  Cloneable {
private String id;
protected String type;

abstract void draw();

public String getType() {
return type;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Object clone() {
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}

}

class Rectangle extends Shape {

public Rectangle() {
type = "Rectangle";
}

@Override
public void draw() {
System.out.print("Rectangle");
}

}

class Circle extends Shape {

public Circle() {
type = "Circle";
}

@Override
void draw() {
System.out.print("Circle");
}

}

class ShapeProtoType {

private static Hashtable<String, Shape> shapeMap = new Hashtable<>();

public static Shape getShape(String shapeId) {
Shape cachedShape = shapeMap.get(shapeId);
return (Shape) cachedShape.clone();
}

public static void loadCache() {
Circle circle = new Circle();
circle.setId("1");
shapeMap.put(circle.getId(), circle);

Rectangle rectangle = new Rectangle();
rectangle.setId("2");
shapeMap.put(rectangle.getId(), rectangle);
}
}

public class PrototypeModal {
public static void main(String[] args) {
ShapeProtoType.loadCache();
ShapeProtoType.getShape("1").draw();
}
}
CATALOG
  1. 1. 面向对象基础
  2. 2. 创建型设计模式
    1. 2.1. 工厂模式
    2. 2.2. 抽象工厂
    3. 2.3. 单例模式
    4. 2.4. 生成器模式
    5. 2.5. 原型模式