Skip to content

Latest commit

 

History

History
71 lines (46 loc) · 3 KB

m6.md

File metadata and controls

71 lines (46 loc) · 3 KB

Composition

COMPOSITE design pattern

Composite allows us to treat both single objects and composite objects uniformly via inheritance (through method calls). To use composite:

  1. Create interface/parent class
  2. Create new single element class that implements the interface
  3. Create composite element class that implements the interface

The composite class has:

  • A List<> containing the single elements
  • A method for adding elements to it
  • Same methods as the single elements (from implementing interface)
  • Methods that delegate calls to its single elements

See Composite.java

DECORATOR design pattern

Decorator allows adding additional functionality to class without modifying or subclassing it. To use decorator:

  1. Create interface
  2. Create class that implements interface
  3. Create decorator class that implements interface and has the class created in step 2 as field

Characteristic of decorator class:

  • Decorated class as a field (often received from constructor)
  • Same methods as the decorated elements (from implementing interface)
  • Method makes calls to decorated elements

See Decorator.java

PROTOTYPE design pattern

Prototype is a template object that can be used as a shortcut to create new objects with similar attributes. You can either implement Cloneable (not recommended) or create your own copy() method

Note on using Cloneable

  • Shallow copy (immutable fields): simply return super.clone() in a try-catch block
  • Deep copy (mutable fields): create a new object and copy each field manually. If class has a lot of immutable fields, get clone from super.clone() and copy each mutable field manually

See Prototype.java

COMMAND design pattern

Command design pattern allows encapsulation of requests or operations as objects. It seperates the object that invokes the operation from the receiver of the action. It allows support for undo and redo, and simplifies code by isolating specific functionality into separate classes

  • Before: Invoke command $\rightarrow$ Perform action
  • After: Invoker creates command object $\rightarrow$ Execute command object $\rightarrow$ Perform action

To use the Command design pattern:

  1. Create a receiver class that actually performs actions. Each action should be a different method.
  2. Create a command interface that declares method for executing and potentially undo a command
  3. Create a concrete command object that implements the interface. This class is the request encapsulated as an object. It should execute a specific method in the receiver class.
  4. Create instances of the concrete command object and execute it

See Command.java

Law of Demeter

Law of Demeter promotes encapsulation and makes code more modular and easier to maintain. The main takeaway is to reduce dependency.

A method is allowed to access the following:

  1. Arguments passed to the method
  2. Fields and methods declared in the same class
  3. Global objects
  4. Anything created within the method