Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns: Brief Examples

Similar presentations


Presentation on theme: "Design Patterns: Brief Examples"— Presentation transcript:

1 Design Patterns: Brief Examples

2 Recall What a design pattern is
“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” – Christopher Alexander, et al. A Pattern Language, Oxford Univ. Press, New York, 1977.

3 What is a design pattern?
An OO design pattern names, explains, and evaluates an important, recurring design in OO systems.

4 Common Causes of Redesign
Creating an object by specifying a class explicitly. Dependence on specific operations. Dependence on hardware and software platform. Dependence on object representations or implementations. Algorithmic dependencies. Tight coupling. Extending functionality by subclassing. Inability to alter classes conveniently.

5 Organization of design patterns
Design patterns can be placed into one of three broad categories: Creational Structural Behavioral Design patterns can have class scope or object scope.

6 Essential Elements of a Pattern
Pattern name Descriptive name indicative of the essence of the pattern and the problem for which it applies. Problem Describes when to apply the pattern. Solution Describes the elements of the design, their relationships, responsibilities and collaborations. It’s like a template that can be applied to many different situations. Consequences Describes results and trade-offs of applying the pattern.

7 An Example design pattern: Singleton
Intent Ensure a class has one instance that has a global point of access. Motivation Some classes need to have exactly one instance. Make the class responsible for Keeping track of its sole instance, Ensuring that no other instance can be created, and Providing access to the instance. Applicability: Use the Singleton pattern when There must be exactly one instance of a class that is accessible to clients from a well-known access point. The sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying code.

8 Singleton design pattern (cont’d)
Some Consequences (benefits): Controlled access to sole instance. Permits refinement of operations and representation. The Singleton class may be subclassed to configure the application with an instance of the class you need at run-time. Permits a variable number of instances (if you change your mind).

9 Singleton example in C++ (Declaration)
class Singleton { public: static Singleton* Instance(); protected: Singleton(); private: static Singlton* _instance; };

10 Singleton example in C++ (implementation)
Singleton* Singleton::_instance=0; Singleton* Singleton::Instance (){ if (_instance ==0) { _instance= new Singleton: } return _instance; }

11 Java – Bank assignment (no singleton)
import banking.*; public class TestBanking { public static void main(String[] args) { Bank bank = new Bank(); bank.addCustomer("Jane", "Simms"); bank.addCustomer("Owen", "Bryant"); bank.addCustomer("Tim", "Soley"); bank.addCustomer("Maria", "Soley"); for ( int i = 0; i < bank.getNumOfCustomers(); i++ ) { Customer customer = bank.getCustomer(i); System.out.println("Customer [" + (i+1) + "] is " + customer.getLastName() + ", " + customer.getFirstName()); }

12 Java – Bank assignment (no singleton)
package banking; public class Bank { private static int MAX_CUSTOMERS = 10; private Customer[] customers; private int numberOfCustomers; public Bank() { customers = new Customer[MAX_CUSTOMERS]; numberOfCustomers = 0; } public void addCustomer(String f, String l) { int i = numberOfCustomers++; customers[i] = new Customer(f, l); public Customer getCustomer(int customer_index) { return customers[customer_index]; public int getNumOfCustomers() { return numberOfCustomers;

13 Singleton example in Java (declaration)
package banking.domain; /** * The Bank class implements the Singleton design pattern, because * there should be only one bank object. */ public class Bank { * The class variable that holds the single Bank instance. private static final Bank bankInstance = new Bank(); public static Bank getBank() { return bankInstance; } private static final int MAX_CUSTOMERS = 10; private static final double SAVINGS_RATE = 3.5; private Customer[] customers; private int numberOfCustomers; private Bank() { customers = new Customer[MAX_CUSTOMERS]; numberOfCustomers = 0; // Other code here . . .

14 Singleton example in Java
import banking.domain.*; public class TestBanking { public static void main(String[] args) { Bank bank = Bank.getBank(); Customer customer; Account account; // Create two customers and their accounts bank.addCustomer("Jane", "Simms"); customer = bank.getCustomer(0); customer.addAccount(new SavingsAccount(500.00, 0.05)); customer.addAccount(new CheckingAccount(200.00, )); bank.addCustomer("Owen", "Bryant"); customer = bank.getCustomer(1); customer.addAccount(new CheckingAccount(200.00)); // Other code here . . . }

15 Singleton- an Example Creational Pattern
Creational design patterns Abstract the instantiation process. Help make a system independent of how its objects are created, composed, and represented. Have two recurring themes: They encapsulate knowledge about concrete classes the system uses. They hide how instances of these classes are created and put together.

16 structural design patterns
Are concerned with composing classes and objects to form larger structures. Structural class patterns use inheritance to compose interfaces or implementations. Structural object patterns describe ways to compose objects to realize new functionality.

17 Adapter- An example structural pattern
Intent Convert the interface of a class into another interface clients expect. Adapter lets classes work together that have incompatible interfaces. Motivation A class designed for reuse isn’t reusable because its interface doesn’t match domain-specific interfaces that an application requires. Applicability When a class you want to use has an interface that doesn’t match what you need. When you want to create a reusable class the cooperates with unrelated or unforeseen classes. When you need to use existing subclasses, but it’s impractical to adapt their interface by subclassing every one.

18 Adapter- An example structural pattern
Consequences Class adapter Adapts Adaptee to Target by committing to a concrete Adaptee class. Lets Adapter override some Adaptee’s behavior. Object adapter Lets a single Adapter work with the Adaptee and all of its subclasses. Makes it harder to override Adaptee behavior.

19 Behavioral design patterns
Concerned with algorithms and the assignment of responsibilities between objects Describe the patterns of communication between objects. Characterize complex control flow. Behavioral class patterns use inheritance to distribute behavior between classes. Behavioral object patterns use composition.

20 Strategy-an example object behavioral design pattern
Intent Define a family of algorithms, encapsulate each one, and make them interchangeable. Motivation Many algorithms may exist for a task, but it is impractical to “hard-wire” all such algorithms into classes that require them. We can avoid problems by defining classes that encapsulate different algorithms. An algorithm encapsulated in this way is called a strategy. Applicability When many related classes differ only in their behavior. When you need different variants of an algorithm When an algorithm uses data that clients shouldn’t see. When a class defines many behaviors that appear as multiple conditional statements in its operations.

21 Strategy-an example object behavioral design pattern
Consequences Creates families of related algorithms. Inheritance can factor out common functionality. Provides an alternative to subclassing. Can eliminate conditional statements. Provides a choice of implementations. Clients must be aware of different Strategies. Communication overhead. Increased number of objects.

22 Summary Design patterns are used in many creative activities to attack recurring problems. OOP is one such activity for which design patterns are being catalogued. Design patterns enhance reusability and can reduce time required to produce a solution.

23 Summary Design patterns can be placed into one of three broad categories: Creational Structural Behavioral Some are class patterns, some are object patterns. In order to use them successfully Become familiar with them. Consider how they solve design problems. Scan Intent sections. Study how they interrelate. Study patterns of like purpose. Examine causes of redesign to see whether applicable to your problem. Consider what should be variable in your design.


Download ppt "Design Patterns: Brief Examples"

Similar presentations


Ads by Google