by Gaurav Kabra
Read this README as a webpage here
Logger, DB Connector Disadv:
- Multi threaded env: access must be thread safe
- Dependency inversion regards this as an anti-pattern since use of private and static methods
Singleton, in some sense, is also a factory pattern but returns only one type of object.
Also, in factory pattern, clients do not even know the name of class for which object is to be generated. They only know Factory name.
E.g. DB Connector Factory using getConnection method, depending on DB driver. E.g. Oracle driver:
Connection conn = DriverManager.getConnection("url", "uname", "pwd");
Now different DB vendors have different URL formats.
Different vendors implement the JDBC interfaces.
Factory for factories. E.g. a factory that returns Maruti factory or Skoda factory:
For cloning - shallow or deep.
In Java, unlike Python, we have to implement our own shallow/deep copy methods.
When we do not have src code, so no modification is possible.
Unlike Adapter, which does "conversion", in Proxy pattern, one object represents another.
Both RealSubject and Proxy must have same method signatures.
Useful in caching responses (optimizations), where first time original class fetches from DB but then proxy caches the response.
(Now deprecated due to RPC)
A method can call only method within same JVM. But RMI allows it to invoke method in another JVM, may be on another OS.
Communication happens via RMI interface (proprietary to Java) unlike HTTPS. Stub and Skeleton are generated by RMI. Stub passes information like method to be invoked and parameters. Skeleton executes using this info and returns results.
E.g. customer service can answer all queries, we reach to warden regarding any hostel query etc.
If classA
has features f1
and f2
and a classB
has features f13
and f4
and we just want features f1
and f4
.
It helps avoid inheritance.
Base class will be same to have common behaviour.
Unlike inheritance, decorator is plug-n-play.
With composition, it's easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type.
Does TypeB want to expose the complete interface (all public methods no less) of TypeA? Indicates Inheritance.
Does TypeB want only some/part of the behavior exposed by TypeA? Indicates need for Composition.
When enter corporate SEZ, multiple layer of security. Similarly in govt work, file moves through hierarchy/layers.
Useful in performing incremental operations (pipeline), doing auth then logging and so on (Servlet FilterChain).
If 100 cannot handle 2140, it delegates to 50 and so on:
1-N dependency, event handling and decoupling
- In State, they act on same object, so that they form finite state automaton
- In CoR, each class gets different objects from previous class that does processing and then passes
We don't need if we use Reflection, but that is generally an anti-pattern.
On a complex operation on DB, invoker should not know what is to execute. Whatever instructions you set as a command, it should execute.