Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
factory_methof_design
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rakesh.pv
factory_methof_design
Commits
ddfbb8a6
Commit
ddfbb8a6
authored
Feb 20, 2023
by
rakesh.pv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
factory v3
parent
129e66bb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
0 deletions
+70
-0
test3.py
+70
-0
No files found.
test3.py
0 → 100644
View file @
ddfbb8a6
from
abc
import
ABC
,
abstractmethod
""" this is a bsic program to implemnt factory design patter
factory method comes under creational design pattern
here It separates the process of creating an object from the code that
depends on the interface of the object."""
""" the factory pattern adds an extra abstraction between creation of object
and where is it used"""
class
Selling_plan
(
ABC
):
#interface
#this is a abstract method with base method definition
#that will be called in all concrete classes
@abstractmethod
def
sell_crypto
(
self
,
x
:
float
,
y
:
float
):
pass
class
SellAll
(
Selling_plan
):
def
sell_crypto
(
self
,
x
:
float
,
y
:
float
):
btc
=
0
usdt
=
x
*
y
return
{
"btc"
:
btc
,
"usdt"
:
usdt
}
class
SellHalf
(
Selling_plan
):
def
sell_crypto
(
self
,
x
:
float
,
y
:
float
):
btc
=
x
/
2
usdt
=
(
x
/
2
)
*
y
return
{
"btc"
:
btc
,
"usdt"
:
usdt
}
class
SellLittle
(
Selling_plan
):
def
sell_crypto
(
self
,
x
:
float
,
y
:
float
):
btc
=
x
*
0.9
usdt
=
(
x
*
0.1
)
*
y
return
{
"btc"
:
btc
,
"usdt"
:
usdt
}
class
TradingApp
:
# this is a factory class
assets
=
{
"btc"
:
100
,
"usdt"
:
0
}
y
=
30000
def
sell_order
(
self
,
sell_decision
:
Selling_plan
):
self
.
assets
=
sell_decision
.
sell_crypto
(
self
.
assets
[
"btc"
],
self
.
y
)
return
self
.
assets
choice
=
int
(
input
(
" enter selling option "
"
\n
1.sellAll "
"
\n
2.sellHalf "
"
\n
3.sellLittle
\n
"
))
A
=
TradingApp
()
# mapping user input to correct concrete classes
if
choice
==
1
:
assets
=
A
.
sell_order
(
SellAll
())
elif
choice
==
2
:
assets
=
A
.
sell_order
(
SellHalf
())
else
:
assets
=
A
.
sell_order
(
SellLittle
())
print
(
assets
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment