1 / 23

Design Patterns: Brief Examples

Design Patterns: Brief Examples.

adonis
Download Presentation

Design Patterns: Brief Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns: Brief Examples

  2. “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. Recall What a design pattern is

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

  4. 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. Common Causes of Redesign

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

  6. 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. Essential Elements of a Pattern

  7. 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. An Example design pattern: Singleton

  8. 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). Singleton design pattern (cont’d)

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

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

  11. 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 ( inti = 0; i < bank.getNumOfCustomers(); i++ ) { Customer customer = bank.getCustomer(i); System.out.println("Customer [" + (i+1) + "] is " + customer.getLastName() + ", " + customer.getFirstName()); } } } Java – Bank assignment (no singleton)

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

  13. 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 intnumberOfCustomers; private Bank() { customers = new Customer[MAX_CUSTOMERS]; numberOfCustomers = 0; } // Other code here . . . } Singleton example in Java (declaration)

  14. 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, 500.00)); bank.addCustomer("Owen", "Bryant"); customer = bank.getCustomer(1); customer.addAccount(new CheckingAccount(200.00)); // Other code here . . . } } Singleton example in Java

  15. 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. Singleton- an Example Creational Pattern

  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. structural design patterns

  17. 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. Adapter- An example structural pattern

  18. 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. Adapter- An example structural pattern

  19. 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. Behavioral design patterns

  20. 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. Strategy-an example object behavioral design pattern

  21. 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. Strategy-an example object behavioral design pattern

  22. 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. Summary

  23. 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. Summary

More Related