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
129e66bb
Commit
129e66bb
authored
Feb 20, 2023
by
rakesh.pv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
factory v2
parent
d6e401a1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
0 deletions
+74
-0
test4.py
+74
-0
No files found.
test4.py
0 → 100644
View file @
129e66bb
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 an 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
@staticmethod
def
create_object
(
x
):
if
x
==
1
:
return
SellAll
()
if
x
==
2
:
return
SellHalf
()
if
x
==
3
:
return
SellLittle
()
x
=
int
(
input
(
" enter x value"
))
y
=
int
(
input
(
" enter y value"
))
choice
=
int
(
input
(
" enter selling option "
"
\n
1.sellAll "
"
\n
2.sellHalf "
"
\n
3.sellLittle
\n
"
))
client
=
TradingApp
()
.
create_object
(
choice
)
print
(
client
.
sell_crypto
(
10
,
10
))
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