-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Beni Valotker
authored and
Beni Valotker
committed
Feb 24, 2020
0 parents
commit 44c1499
Showing
7 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
''' | ||
configuration file | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from factory_pattern.factory import FactoryClass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
''' | ||
factory design pattern | ||
''' | ||
class FactoryClass: | ||
''' | ||
Factory Class | ||
''' | ||
# return the request class messages | ||
@staticmethod | ||
def get_class(class_name): | ||
if class_name == "thread_number_1": | ||
return Thread_1() | ||
elif class_name == "thread_number_2": | ||
return Thread_2() | ||
|
||
assert 0, 'Could not find shape ' + class_name | ||
|
||
''' | ||
Interface - all the class that inherit from this interface must declare methods. | ||
''' | ||
class FactoryInterface(ABC): | ||
''' | ||
Messages Interface | ||
''' | ||
@abstractmethod | ||
def run(self): pass | ||
|
||
|
||
''' | ||
methods: | ||
- run: print some text | ||
''' | ||
class Thread_1(FactoryInterface): | ||
def __init__(self): | ||
pass | ||
|
||
def run(self): | ||
print("run thread number 1") | ||
|
||
|
||
class Thread_2(FactoryInterface): | ||
def __init__(self): | ||
pass | ||
|
||
def run(self): | ||
print("run thread number 2") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#/path/path/ | ||
#title :Title | ||
#description :Description. | ||
#update date :01/01/2020 12:10 | ||
#version :1.0 | ||
#changes :new version changes description. | ||
#python_version :3.6 | ||
#============================================================================== | ||
|
||
import time | ||
import threading | ||
import thread_handler | ||
|
||
''' | ||
Daemon Threads - thread shut down when the caller script is done or after all the thread is close. | ||
- name = custom thread name | ||
- target = function to run | ||
- args = function args (as a tuples) | ||
- daemon = True/Flase | ||
''' | ||
# declare the threads object | ||
thread_obj_1 = threading.Thread( | ||
name="Custom Thread 1", | ||
target=thread_handler.thread_handler, | ||
args=("thread_number_1", ), | ||
daemon=True | ||
) | ||
|
||
thread_obj_2 = threading.Thread( | ||
name="Custom Thread 2", | ||
target=thread_handler.thread_handler, | ||
args=("thread_number_2", ), | ||
daemon=True | ||
) | ||
|
||
# start threads running | ||
thread_obj_1.start() | ||
thread_obj_2.start() | ||
|
||
|
||
# check if the thread is alive (finish is job or error) | ||
# in this case if one thread is dead the all thread shut down | ||
while(True): | ||
time.sleep(1) | ||
if thread_obj_1.is_alive() != None: | ||
break | ||
if thread_obj_2.is_alive() != None: | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
''' | ||
package dependencies version | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from factory_pattern import FactoryClass | ||
|
||
# handle the service by thread type class | ||
def thread_handler(thread_name): | ||
# get and create the object | ||
factory_obj = FactoryClass.get_class(thread_name) | ||
|
||
factory_obj.run() |