AdvC++ - converter zrobiony przez wykładowcę bo brak czasu
This commit is contained in:
parent
24943a4282
commit
385967d3b8
1 changed files with 9 additions and 11 deletions
|
|
@ -16,20 +16,20 @@ private:
|
|||
|
||||
class Converter {
|
||||
public:
|
||||
virtual void Convert(Resource* resource) = 0;
|
||||
virtual void Convert(const Resource& resource) = 0;
|
||||
};
|
||||
|
||||
class CurlyBracketConverter : public Converter {
|
||||
public:
|
||||
virtual void Convert(Resource* resource) {
|
||||
std::cout << "{" << resource->str() << "}\n";
|
||||
virtual void Convert(const Resource& resource) {
|
||||
std::cout << "{" << resource.str() << "}\n";
|
||||
}
|
||||
};
|
||||
|
||||
class SquareBracketConverter : public Converter {
|
||||
public:
|
||||
virtual void Convert(Resource* resource) {
|
||||
std::cout << "[" << resource->str() << "]\n";
|
||||
virtual void Convert(const Resource& resource) {
|
||||
std::cout << "[" << resource.str() << "]\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -37,7 +37,7 @@ class Printer {
|
|||
public:
|
||||
Printer(Converter* converter): converter_(converter) {}
|
||||
|
||||
void Print(Resource* resource) {
|
||||
void Print(const Resource& resource) {
|
||||
converter_->Convert(resource);
|
||||
}
|
||||
|
||||
|
|
@ -46,14 +46,12 @@ private:
|
|||
};
|
||||
|
||||
int main() {
|
||||
Resource* resource = new Resource("Ala has a cat");
|
||||
auto resource = std::make_unique<Resource>("Ala has a cat");
|
||||
Printer printer(new SquareBracketConverter{});
|
||||
Printer printer2(new CurlyBracketConverter{});
|
||||
|
||||
printer.Print(resource);
|
||||
printer2.Print(resource);
|
||||
|
||||
delete resource;
|
||||
printer.Print(*resource);
|
||||
printer2.Print(*resource);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue