Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
COMS327
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jake Feddersen
COMS327
Commits
5513b254
Commit
5513b254
authored
5 years ago
by
Jake Feddersen
Browse files
Options
Downloads
Patches
Plain Diff
In-Class stuff
parent
8b136ead
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
notes/4_19/decorator
+0
-0
0 additions, 0 deletions
notes/4_19/decorator
notes/4_19/decorator.cpp
+90
-0
90 additions, 0 deletions
notes/4_19/decorator.cpp
with
90 additions
and
0 deletions
notes/4_19/decorator
0 → 100755
+
0
−
0
View file @
5513b254
File added
This diff is collapsed.
Click to expand it.
notes/4_19/decorator.cpp
0 → 100644
+
90
−
0
View file @
5513b254
#include
<iostream>
using
namespace
std
;
class
shape
{
public:
virtual
~
shape
()
{}
virtual
void
draw
()
=
0
;
};
class
square
:
public
shape
{
public:
void
draw
()
{
cout
<<
"[]"
<<
endl
;
}
};
class
circle
:
public
shape
{
public:
void
draw
()
{
cout
<<
"()"
<<
endl
;
}
};
// What if we want to add colors?
// We could extend square for every color,
// or attribute, but that doesn't scale well
class
green_square
:
public
square
{
public:
void
draw
()
{
cout
<<
"green-[]"
<<
endl
;
}
};
// Better way: Decorator pattern
class
shape_decorator
:
public
shape
{
private:
shape
*
s
;
public:
shape_decorator
(
shape
*
s
)
:
s
(
s
)
{}
~
shape_decorator
()
{
delete
s
;
}
void
draw
()
{
s
->
draw
();
}
};
class
green_shape
:
public
shape_decorator
{
public:
green_shape
(
shape
*
s
)
:
shape_decorator
(
s
)
{}
void
draw
()
{
cout
<<
"green-"
;
shape_decorator
::
draw
();
}
};
class
big_shape
:
public
shape_decorator
{
public:
big_shape
(
shape
*
s
)
:
shape_decorator
(
s
)
{}
void
draw
()
{
cout
<<
"BIG-"
;
shape_decorator
::
draw
();
}
};
class
cute_shape
:
public
shape_decorator
{
public:
cute_shape
(
shape
*
s
)
:
shape_decorator
(
s
)
{}
void
draw
()
{
cout
<<
"cute-"
;
shape_decorator
::
draw
();
}
};
int
main
(
int
argc
,
char
*
argv
[])
{
square
s
;
circle
c
;
green_square
gs
;
s
.
draw
();
c
.
draw
();
gs
.
draw
();
green_shape
d
(
new
square
);
d
.
draw
();
big_shape
bs
(
new
big_shape
(
new
circle
));
bs
.
draw
();
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment