-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.bridge.h
68 lines (58 loc) · 1.58 KB
/
7.bridge.h
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
//
// Created by Tianyi Zhang on 12/6/20.
//
#ifndef DESIGN_PATTERN_7_BRIDGE_H
#define DESIGN_PATTERN_7_BRIDGE_H
#include <string>
#include <memory>
#include <iostream>
struct IResource{
virtual std::string snippet() const= 0;
virtual ~IResource(){}
};
struct IView{
virtual void show() = 0;
virtual ~IView(){}
};
struct Artist{
std::string bio() const{
return "Artist bio info. Artist full bio info";
}
};
struct Book{
std::string coverInfo() const{
return "Book cover info. Book full cover info";
}
};
struct ArtistResource : public IResource{
ArtistResource(Artist const& artist1): artist(artist1){}
std::string snippet() const override{
return artist.bio();
}
Artist artist;
};
struct BookResource : public IResource{
BookResource(Book const& book1): book(book1){}
std::string snippet() const override{
return book.coverInfo();
}
Book book;
};
struct ShortView : public IView{
ShortView(std::shared_ptr<IResource> resource1):resource(resource1){}
void show() override{
std::string content = resource->snippet();
int pos = content.find('.');
std::cout<<"shortView : "<< content.substr(0, pos) << std::endl;
}
std::shared_ptr<IResource> resource;
};
struct FullView : public IView{
FullView(std::shared_ptr<IResource> resource1):resource(resource1){}
void show() override{
std::string content = resource->snippet();
std::cout<<"Full View : "<<content<<std::endl;
}
std::shared_ptr<IResource> resource;
};
#endif //DESIGN_PATTERN_7_BRIDGE_H