Python
Programming Concepts
Instance methods: methods that can be called on instances of a class. They must accept a reference to the actual instance on which it was called
def instance_method(_self).
To make a class configurable at the point it’s created, you need an initialization method It too, must accept a reference to the instance on which it is called:
__init__(self).
Design Patterns:
A design pattern is a model solution to a common design problem. it describes the problem and a general approach to solving it.
SOLID Principles of Object-oriented Design
Single responsibility: A class should have only one responsibility. e.g. Either cooking or washing up, but not both.
Open / Closed: A class should be open for extension. Usually by inheritance. Closed for modification.
Liskov Substitution: A child class should be able to stand in for its parent, without breaking anything.
Interface Segregation: Many specific interfaces are better than one do-it-all interface.
Dependency Inversion: We should program towards abstractions, not implementations (concrete objects). Implementations can vary. Abstractions, should not.
Classifications
Creational Patterns
The Factory Pattern: Defines an interface for creating an object. Lets subclasses decide with object to build. Defers instantiation to subclasses. Also known as Virtual Constructor.
The Abstract Factory Pattern: Close cousing of the Factory pattern. Factory creates one product, whereas the Abstract Factory creates families. Enforces dependencies between classes. Defers creation of objects to concrete subclasses. Also known as the Kit pattern.
The Builder Pattern: Separates construction of an object from its representation. Encapsulates the construction of the object, obeying the principal ‘encapsulate what varies’, which is a corollary of the single-responsibility principle. Allows for a multi-step construction process. Implementation can vary. The client only sees the abstraction.
The Prototype Pattern: Uses a given instance as a prototype. Creates a clone of the prototype. Clone can have different attributes. Reduce number of classes. Run-time inheritance.
Shallow Cloning: Constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
Deep Cloning: Recursively copies objects found in the original. Be careful – may cause a recursive loop.
Prototype Manager: maintains a dictionary for easy access to prototypes.
Singleton: Ensures that a class has only one instance. Useful when you need to control access to a limited resource – e.g. device access, buffer pools, web/db connection pools. Provides a global point of access to the one instance and is responsible for creating that instance. Can also provide for lazy instantiation, which is good if the object is costly to instantiate or may not always be used.
Drawbacks of Singleton Classes: They violate the single responsibility principle. Non-standard class access. Harder to test. Carry global state. Hard to sub-class. Considered a harmful anti-pattern.
Structural Patterns
Adapter: Converts interface of a class into another that is expected by clients. Let’s classes work together. Can provide additional functionality. There are two types. Favor inheritance, whenever you can. They are easier to maintain and debug. Also known as the wrapper pattern.
Object adapters: Composition
Class adapters: Inheritance
Bridge
Composite
Decorator
Facade
Proxy
Behavioral: Object interaction and responsibility
Strategy
Command
State
Observer
Visitor
Chain of Responsibility
Mediator
Memento
Null
Template
Iterator
Interpreter