Design that is more automated and require less maintenance is the standard.
Setters can be used for validation and constraining the value to be set to a particular range.
Getters can be used for returning default value, if the variable is not set yet or for lazy instantiation.
Can’t make objects out of abstract class. Abstract class can have some non abstract members.
Interface have only abstract methods.
- Can only have one instance of that particular class.
- President of a country, System in java.
- Private constructor, singleton using enum.
- @Singleton annotation.
- Difficult to unit test – why?
- Having a logic to return a particular subclass object, when asked for a class object.
- Abstract Factory:
- Separates object construction from its representation.
- Chess game initial setup.
- Copying/cloning the initial setup rather than creating the initial setup everytime you need it. Reduce redundant work.
- Copy a fully initialized instance.
- Link to code.
How to create objects?
Inheritance? Interface? etc.
How are different classes related?
How are objects composed?
- Match interfaces of different classes. helps to communicate.
- An object representing another object, like credit card as a proxy of bank account.
- Remote object and Home object(proxy).
- Reuse same objects by resetting values of the objects appropriately instead of creating new objects every time.
- Event managers, process, execute, group many steps into a single step.
- Add responsibilities to objects dynamically.
- Ex: adding different Toppings for different pizzas, adding discounts to different orders.
Interactions between different objects.
- Template method:
- instead of applications talking to each other, we use an enterprise service bus.
- Chain of responsibility:
- Passing a request through different objects.
- A way of notifying a change to a number of classes.
- This pattern is implemented in java.
- Subject extends Observable.
- Who wants to listen implements Observer and registers with the subject.
- change the implementation/strategy of an interface at a later point in time.
- Pass whatever implementation needs to be used as an argument.
- Encapsulate a command request as an object.
- java.lang.runnable threads are implemented like this.
- Adding new operations to a particular class without inheritance and wi
- Sequentially access the elements of a collection.
- Saving states of something as objects to restore them in future point of time if necessary.
- Undo/Redo operations.
Abstract Factory pattern:
Try to put state and behaviors in different classes.