Commit b389c993 authored by arjun.b's avatar arjun.b

first commit

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (Refactoring-Guru)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Refactoring-Guru.iml" filepath="$PROJECT_DIR$/.idea/Refactoring-Guru.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
from abc import ABC, abstractmethod
# Abstract Product A
class Shape(ABC):
@abstractmethod
def draw(self):
pass
# Concrete Product A1, inherit the class Shape
class Rectangle(Shape):
def draw(self):
print("Inside Rectangle::draw() method.")
# Concrete Product A2,inherit the class Shape
class Circle(Shape):
def draw(self):
print("Inside Circle::draw() method.")
# Abstract Product B
class Color(ABC):
@abstractmethod
def fill(self):
pass
# Concrete Product B1,inherit the class Color
class Red(Color):
def fill(self):
print("Inside Red::fill() method.")
# Concrete Product B2,inherit the class Color
class Blue(Color):
def fill(self):
print("Inside Blue::fill() method.")
# Abstract Factory
class AbstractFactory(ABC):
@abstractmethod
def create_shape(self):
pass
@abstractmethod
def create_color(self):
pass
# Concrete Factory 1,inherit the class AbstractFactory
class ShapeColorFactory(AbstractFactory):
def create_shape(self):
return Circle()
def create_color(self):
return Red()
# Concrete Factory 2,inherit the class AbstractFactory
class ColorShapeFactory(AbstractFactory):
def create_shape(self):
return Rectangle()
def create_color(self):
return Blue()
# Client code
factory1 = ShapeColorFactory()
shape1 = factory1.create_shape()
color1 = factory1.create_color()
shape1.draw()
color1.fill()
factory2 = ColorShapeFactory()
shape2 = factory2.create_shape()
color2 = factory2.create_color()
shape2.draw()
color2.fill()
import math
class Shape:
def __init__(self, area):
self.area = area
def get_area(self):
return self.area.calculate_area()
class Area:
def calculate_area(self):
pass
class SquareArea(Area):
def calculate_area(self, side):
return side * side
class RectangleArea(Area):
def calculate_area(self, length, width):
return length * width
class TriangleArea(Area):
def calculate_area(self, base, height):
return 0.5 * base * height
class CircleArea(Area):
def calculate_area(self, radius):
return math.pi * radius * radius
# Create area objects
square_area = SquareArea()
rectangle_area = RectangleArea()
triangle_area = TriangleArea()
circle_area = CircleArea()
# Create shape objects and pass in the appropriate area object
square = Shape(square_area)
rectangle = Shape(rectangle_area)
triangle = Shape(triangle_area)
circle = Shape(circle_area)
# Calculate the area of each shape
square_area = square.get_area(5)
rectangle_area = rectangle.get_area(6, 4)
triangle_area = triangle.get_area(3, 2)
circle_area = circle.get_area(7)
# Print the area of each shape
print(square_area) # 25
print(rectangle_area) # 24
print(triangle_area) # 3.0
print(circle_area) # 153.93804002589985
import copy
class Company:
def clone(self):
pass
class Employee(Company):
def __int__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def clone(self):
return copy.copy(self)
emp1 = Employee("Amal", "k", 24)
emp2 = emp1.clone()
print("employee 1:", emp1.first_name, emp1.last_name, emp1.age)
print("employee 2", emp2.first_name, emp2.last_name, emp2.age)
from __future__ import annotations
from abc import ABC, abstractmethod
class Creator(ABC):
"""
The Creator class declares the factory method that is supposed to return an
object of a Product class. The Creator's subclasses usually provide the
implementation of this method.
"""
@abstractmethod
def factory_method(self):
"""
Note that the Creator may also provide some default implementation of
the factory method.
"""
pass
def some_operation(self) -> str:
"""
Also note that, despite its name, the Creator's primary responsibility
is not creating products. Usually, it contains some core business logic
that relies on Product objects, returned by the factory method.
Subclasses can indirectly change that business logic by overriding the
factory method and returning a different type of product from it.
"""
# Call the factory method to create a Product object.
product = self.factory_method()
# Now, use the product.
result = f"Creator: The same creator's code has just worked with {product.operation()}"
return result
"""
Concrete Creators override the factory method in order to change the resulting
product's type.
"""
class ConcreteCreator1(Creator):
"""
Note that the signature of the method still uses the abstract product type,
even though the concrete product is actually returned from the method. This
way the Creator can stay independent of concrete product classes.
"""
def factory_method(self) -> Product:
return ConcreteProduct1()
class ConcreteCreator2(Creator):
def factory_method(self) -> Product:
return ConcreteProduct2()
class Product(ABC):
"""
The Product interface declares the operations that all concrete products
must implement.
"""
@abstractmethod
def operation(self) -> str:
pass
"""
Concrete Products provide various implementations of the Product interface.
"""
class ConcreteProduct1(Product):
def operation(self) -> str:
return "{Result of the ConcreteProduct1}"
class ConcreteProduct2(Product):
def operation(self) -> str:
return "{Result of the ConcreteProduct2}"
def client_code(creator: Creator) -> None:
"""
The client code works with an instance of a concrete creator, albeit through
its base interface. As long as the client keeps working with the creator via
the base interface, you can pass it any creator's subclass.
"""
print(f"Client: I'm not aware of the creator's class, but it still works.\n"
f"{creator.some_operation()}", end="")
if __name__ == "__main__":
print("App: Launched with the ConcreteCreator1.")
client_code(ConcreteCreator1())
print("\n")
print("App: Launched with the ConcreteCreator2.")
client_code(ConcreteCreator2())
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