weekly meeting: basic design pattern

12
Design pattern [email protected]

Upload: nguyen-trung-kien

Post on 13-Apr-2017

55 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Weekly Meeting: Basic Design Pattern

Design [email protected]

Page 2: Weekly Meeting: Basic Design Pattern

Design pattern categories• Creational Patterns

• These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new opreator. This gives program more flexibility in deciding which objects need to be created for a given use case.

• Structural Patterns• These design patterns concern class and object composition. Concept of inheritance is

used to compose interfaces and define ways to compose objects to obtain new functionalities.

• Behavioral Patterns• These design patterns are specifically concerned with communication between objects.

• J2EE Patterns• These design patterns are specifically concerned with the presentation tier. These

patterns are identified by Sun Java Center.

Page 3: Weekly Meeting: Basic Design Pattern

Factory Pattern

• One of most used design pattern in Java• Create object without exposing the creation logic to the client

and refer to newly created object using a common interface

Page 4: Weekly Meeting: Basic Design Pattern

Factory Pattern• Pros

• Allows you to hide implementation of an application seam (the core interfaces that make up your application)

• Allows you to easily test the seam of an application (that is to mock/stub) certain parts of your application so you can build and test the other parts

• Allows you to change the design of your application more readily, this is known as loose coupling

• Con's• Makes code more difficult to read as all of your code is behind an abstraction that may in turn

hide abstractions• Can be classed as an anti-pattern when it is incorrectly used, for example some people use it to

wire up a whole application when using an IOC container, instead use Dependency Injection.• Too much abstract

Page 5: Weekly Meeting: Basic Design Pattern

Factory Pattern: Installation

• Procedural Solution - switch/case noob instantiation

• Most simple and intuitive• Add new concrete product call we should modify the Factory class? ->

not very flexible

Page 6: Weekly Meeting: Basic Design Pattern

Factory Pattern: Installation

Page 7: Weekly Meeting: Basic Design Pattern

Factory Pattern: Reflection

• Class Registration - reflection

Page 8: Weekly Meeting: Basic Design Pattern

Discussion• Factory Pattern vs Dependency Injection?

Page 9: Weekly Meeting: Basic Design Pattern

Visitor Pattern• What is that?

• Behavior pattern category• Do the right thing based on the type of two objects

• When: Lets you define a new operation without changing the classes of the elements on which it operates

• Real world example• Shopping cart when you shopping in supermarket which include product a,

product b…• The cashier acts as a visitor, taking the disparate set of elements. Some of

them using prices, some need to be weighted to provide you total

Page 10: Weekly Meeting: Basic Design Pattern

Visitor Pattern: How

Page 11: Weekly Meeting: Basic Design Pattern

Visitor Pattern: Example

Page 12: Weekly Meeting: Basic Design Pattern

Visitor Pattern: Pros & Cons• PROs

• Allows you to add operations to a Composite structure without changing the structure itself

• Adding new operations is relatively easy• The code for operations performed by the Visitor is centralized

• CONs• Encapsulation is broken when the Visitor is used • Because the traversal function is involved, changes to the Composite

structure are more difficult