Commit 129e66bb by rakesh.pv

factory v2

parent d6e401a1
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 "
"\n1.sellAll "
"\n2.sellHalf "
"\n3.sellLittle \n"))
client = TradingApp().create_object(choice)
print(client.sell_crypto(10, 10))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment