Skip to content

Commit bcaea79

Browse files
committed
add factory method intial code
1 parent db84344 commit bcaea79

File tree

7 files changed

+208
-0
lines changed

7 files changed

+208
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Win32",
5+
"includePath": ["${workspaceFolder}/**"],
6+
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
7+
"windowsSdkVersion": "10.0.18362.0",
8+
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe",
9+
"cStandard": "c11",
10+
"cppStandard": "c++17",
11+
"intelliSenseMode": "msvc-x64"
12+
}
13+
],
14+
"version": 4
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "g++.exe - Build and debug active file",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"environment": [],
16+
"externalConsole": false,
17+
"MIMode": "gdb",
18+
"miDebuggerPath": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
19+
"setupCommands": [
20+
{
21+
"description": "Enable pretty-printing for gdb",
22+
"text": "-enable-pretty-printing",
23+
"ignoreFailures": true
24+
}
25+
],
26+
"preLaunchTask": "g++.exe build active file"
27+
}
28+
]
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files.associations": {
3+
"istream": "cpp",
4+
"xstring": "cpp",
5+
"ostream": "cpp",
6+
"iostream": "cpp"
7+
}
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "shell",
6+
"label": "g++.exe build active file",
7+
"command": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
8+
"args": [
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
13+
],
14+
"options": {
15+
"cwd": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
16+
},
17+
"problemMatcher": ["$gcc"],
18+
"group": {
19+
"kind": "build",
20+
"isDefault": true
21+
}
22+
},
23+
{
24+
"type": "shell",
25+
"label": "g++.exe build active file",
26+
"command": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
27+
"args": [
28+
"-g",
29+
"${file}",
30+
"-o",
31+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
32+
],
33+
"options": {
34+
"cwd": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
35+
}
36+
}
37+
]
38+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
enum shapeType
6+
{
7+
Circle,
8+
Rectangle,
9+
Square
10+
};
11+
12+
class shape
13+
{
14+
public:
15+
virtual void draw() = 0;
16+
static shape *create(shapeType type);
17+
};
18+
class circle : public shape
19+
{
20+
public:
21+
void draw()
22+
{
23+
cout << "Circle" << endl;
24+
}
25+
};
26+
class rectangle : public shape
27+
{
28+
public:
29+
void draw()
30+
{
31+
cout << "Rectangle" << endl;
32+
}
33+
};
34+
class square : public shape
35+
{
36+
public:
37+
void draw()
38+
{
39+
cout << "Square" << endl;
40+
}
41+
};
42+
shape *shape::create(shapeType type)
43+
{
44+
if (type == Circle)
45+
return new circle();
46+
else if (type == Square)
47+
return new square();
48+
else if (type == Rectangle)
49+
return new rectangle();
50+
}
51+
class Client
52+
{
53+
private:
54+
shape *pShape = NULL;
55+
shapeType type;
56+
57+
public:
58+
Client(shapeType type)
59+
{
60+
this->type = type;
61+
pShape = shape::create(type);
62+
}
63+
~Client()
64+
{
65+
if (pShape)
66+
{
67+
delete[] pShape;
68+
pShape = NULL;
69+
}
70+
};
71+
shape *getShap()
72+
{
73+
return pShape;
74+
}
75+
};
76+
77+
main()
78+
{
79+
Client *pClient = new Client(Circle);
80+
shape *pShape = pClient->getShap();
81+
82+
pShape->draw();
83+
}
78 KB
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var Circle = function() {
2+
this.draw = function() {
3+
console.log("Circle");
4+
};
5+
};
6+
var Rectangle = function() {
7+
this.draw = function() {
8+
console.log("Rectangle");
9+
};
10+
};
11+
var Square = function() {
12+
this.draw = function() {
13+
console.log("Square");
14+
};
15+
};
16+
function shapeFactory() {
17+
this.create = function(shapeType) {
18+
var shape = "";
19+
if (shapeType === "Circle") shape = new Circle();
20+
else if (shapeType === "Rectangle") shape = new Rectangle();
21+
else if (shapeType === "Square") shape = new Square();
22+
else console.log("not Supported yet");
23+
24+
return shape;
25+
};
26+
}
27+
var factoty = new shapeFactory();
28+
29+
var rectangle = factoty.create("Rectangle");
30+
var circle = factoty.create("Circle");
31+
var square = factoty.create("Square");
32+
33+
rectangle.draw();
34+
circle.draw();
35+
square.draw();

0 commit comments

Comments
 (0)