mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-31 16:07:51 +03:00 
			
		
		
		
	Add example for abstract factory provider
This commit is contained in:
		
							parent
							
								
									2aa85228d7
								
							
						
					
					
						commit
						f4992d842e
					
				
							
								
								
									
										25
									
								
								examples/providers/abstract_factory/cache.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								examples/providers/abstract_factory/cache.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,25 @@ | ||||||
|  | """Example hierarchy of cache clients with abstract base class.""" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class AbstractCacheClient(object): | ||||||
|  |     """Abstract cache client.""" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class RedisCacheClient(AbstractCacheClient): | ||||||
|  |     """Cache client implementation based on Redis.""" | ||||||
|  | 
 | ||||||
|  |     def __init__(self, host, port, db): | ||||||
|  |         """Initializer.""" | ||||||
|  |         self.host = host | ||||||
|  |         self.port = port | ||||||
|  |         self.db = db | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class MemcacheCacheClient(AbstractCacheClient): | ||||||
|  |     """Cache client implementation based on Memcached.""" | ||||||
|  | 
 | ||||||
|  |     def __init__(self, hosts, port, prefix): | ||||||
|  |         """Initializer.""" | ||||||
|  |         self.hosts = hosts | ||||||
|  |         self.port = port | ||||||
|  |         self.prefix = prefix | ||||||
							
								
								
									
										28
									
								
								examples/providers/abstract_factory/example.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								examples/providers/abstract_factory/example.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,28 @@ | ||||||
|  | """`AbstractFactory` providers example.""" | ||||||
|  | 
 | ||||||
|  | import cache | ||||||
|  | 
 | ||||||
|  | import dependency_injector.providers as providers | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Define abstract cache client factory: | ||||||
|  | cache_client_factory = providers.AbstractFactory(cache.AbstractCacheClient) | ||||||
|  | 
 | ||||||
|  | if __name__ == '__main__': | ||||||
|  |     # Override abstract factory with redis client factory: | ||||||
|  |     cache_client_factory.override(providers.Factory(cache.RedisCacheClient, | ||||||
|  |                                                     host='localhost', | ||||||
|  |                                                     port=6379, | ||||||
|  |                                                     db=0)) | ||||||
|  |     redis_cache = cache_client_factory() | ||||||
|  |     print(redis_cache)  # <cache.RedisCacheClient object at 0x10975bc50> | ||||||
|  | 
 | ||||||
|  |     # Override abstract factory with memcache client factory: | ||||||
|  |     cache_client_factory.override(providers.Factory(cache.MemcacheCacheClient, | ||||||
|  |                                                     hosts=['10.0.1.1', | ||||||
|  |                                                            '10.0.1.2', | ||||||
|  |                                                            '10.0.1.3'], | ||||||
|  |                                                     port=11211, | ||||||
|  |                                                     prefix='my_app')) | ||||||
|  |     memcache_cache = cache_client_factory() | ||||||
|  |     print(memcache_cache)  # <cache.MemcacheCacheClient object at 0x10975bc90> | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user