This document provides an introduction to design patterns, including their motivation, history, definition, categories, and examples. Some key points:
- Design patterns solve common programming problems by describing reusable solutions that can be adapted for different situations.
- Common categories include creational, structural, and behavioral patterns. Example patterns discussed are Singleton, Decorator, Abstract Factory.
- Design patterns speed up development, promote code reuse, and make software more robust, maintainable and extensible. Resources for further information on design patterns are provided.
Design Pattern: Factory Pattern
In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.
Blog Article: http://jyaasa.com/blog/factory-design-pattern-in-ruby/
An introduction to structural design patterns in object orientation. Suitable for intermediate to advanced computing students and those studying software engineering.
The document discusses different creational design patterns, including the Abstract Factory pattern and Factory Method pattern. The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes. It allows a system to be independent of how its objects are created. The Factory Method pattern defines an interface for creating an object but lets subclasses decide which class to instantiate. It decouples class creation from use through delegation to subclasses.
Desing pattern prototype-Factory Method, Prototype and Builder paramisoft
油
The document discusses three design patterns: Factory Method, Prototype, and Builder. Factory Method defines an interface for creating objects but lets subclasses decide which class to instantiate. Prototype specifies the kinds of objects to create using a prototypical instance that new objects can be cloned from. Builder separates the construction of a complex object from its representation so that the same construction process can create different representations.
This document provides an overview of key object-oriented programming concepts including classes and objects, inheritance, encapsulation, polymorphism, interfaces, abstract classes, and design patterns. It discusses class construction and object instantiation. Inheritance is described as both exposing implementation details and potentially breaking encapsulation. Composition is presented as an alternative to inheritance. The document also covers method overriding, overloading, and duck typing as forms of polymorphism. Finally, it briefly introduces common design principles like SOLID and patterns like delegation.
This document discusses design patterns and provides examples of the Singleton and Abstract Factory patterns. It begins with an introduction to design patterns, their purpose and history. It then discusses the Singleton pattern in detail using a logger example and describes how to implement it using lazy instantiation. It also covers the Abstract Factory pattern using real world examples of a kitchen and chefs. It compares how these patterns would be implemented in code versus real objects.
This is Class 2 on a 6 week course I taught on Software Design Patterns.
This course discusses Strategy and Template pattern.
Class based on "Head First Design Patterns."
The prototype pattern specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype. It allows objects to be cloned without knowing their concrete classes. The prototype declares a cloning method that returns a copy of itself. A client creates an object by asking the prototype to clone itself. This pattern avoids subclassing and allows object types to vary at runtime. It is useful when object creation depends on runtime parameters or when classes have a small number of derived forms.
What is a constructor?
Constructor is a method which gets executed automatically when we create or instantiate object of that class having constructor.
More Highlights of Constructor
A single class can have multiple constructors means we can have more than one constructor in a class. It is also called as overloaded constructor.
A benefit of using a constructor is that it guarantees that the object will go through a proper initialization before an object being used means we can pre-initialize some of the class variables with values before an object being used.
A constructor can be called another constructor by using油"this" keyword. "this"油keyword is the current instance of a class.
The document discusses classes, objects, constructors, and other object-oriented programming concepts in C#:
1) A class defines the data and behavior of a type using variables, methods, and events. Objects are instances of classes that have identity, data, and behaviors defined by the class.
2) Constructors initialize objects and are called using the new keyword. Constructors can be overloaded, parameterized, static, or chained to call another constructor.
3) Classes support concepts like inheritance, hiding, overriding, and polymorphism to extend and customize behavior in derived classes. References and values can be passed into methods.
Name : Prasoon Dadhich
USN : 1MS09IS069
CODE
#include <iostream>
using namespace std;
class Singleton
{
private:
static bool instanceFlag;
static Singleton *single;
Singleton()
{
//private constructor
}
public:
static Singleton* getInstance();
void method();
~Singleton()
{
instanceFlag = false;
}
};
bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
if(! instanceFlag)
{
single = new Singleton();
instanceFlag = true;
return single;
}
else
{
return single;
}
}
void Singleton::method()
{
cout << "Method of the singleton class" << endl;
}
int main()
{
Singleton *sc1,*sc2;
sc1 = Singleton::getInstance();
sc1->method();
sc2 = Singleton::getInstance();
sc2->method();
return 0;
}
Explanation
If we create any object of this class then the static object (player) will be returned and assigned to the new object.
Finally only one object will be there. The point is not how many times something gets executed, the point is, that the work is done by a single instance of the the class. This is ensured by the class method Singleton::getInstance().
As you can see in the above code,Client can access any instance of the singleton only through the getInstance() method.Once an instance is created,the instanceFlag value becomes true and then any further objects pointers created will be given the reference to the same object that has been created.
Also,make sure you notice the presence of a private constructor.This ensures that no new objects of the class are created and that all the class pointers get the same reference to work on.
This document discusses three design patterns: Adapter, Bridge, and Composite.
The Adapter pattern allows classes with incompatible interfaces to work together by wrapping its interface into a compatible one. The Bridge pattern decouples an abstraction from its implementation, allowing them to vary independently. The Composite pattern allows treating individual objects and compositions of objects uniformly.
An introduction to behavioural design patterns in object orientation. Suitable for intermediate to advanced computing students and those studying software engineering.
All 23 classical GOF Design Patterns with illustrations that try to catch some essence of them.
Used in a presentation for Eindhoven Developers, March 17, 2015 http://www.meetup.com/Eindhoven-Developers-Meetup/events/220477660/
Design patterns are optimized, reusable solutions to the programming problems that we encounter every day. A design pattern is not a class or a library that we can simply plug into our system; it's much more than that. It is a template that has to be implemented in the correct situation. It's not language-specific either. A good design pattern should be implementable in mostif not alllanguages, depending on the capabilities of the language. Most importantly, any design pattern can be a double-edged sword if implemented in the wrong place, it can be disastrous and create many problems for you. However, implemented in the right place, at the right time, it can be your savior.
The document discusses various design principles and patterns in Java including the Singleton, Factory Method, Prototype, and Abstract Factory patterns. It provides examples of when and how to apply each pattern, describing their structures, participants, collaborations and consequences. It also covers design principles such as the open-closed principle, dependency inversion, and interface segregation.
The document discusses several creational design patterns: Singleton, Factory, Abstract Factory, and Prototype. The Singleton pattern ensures that only one instance of a class exists. The Factory pattern abstracts object creation by defining a common interface for creating objects. The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes. The Prototype pattern creates objects by cloning prototypes instead of using constructors.
Design patterns are general reusable solutions to common problems in software design. They are not specific designs that can be transformed directly into code, but descriptions that can be applied to many situations. In 1994, the "Gang of Four" authors published the influential book Design Patterns, which introduced design patterns to software development. The book categorized patterns into creational, structural, and behavioral groups. Factory pattern is a creational pattern that provides a way to create objects without exposing object creation logic to the client. It allows for more flexibility in deciding which objects need to be created.
This document provides an overview of design patterns, which are proven solutions to common problems in software design. It discusses different types of patterns like creational, structural, and behavioral patterns. It then gives examples of some common patterns like Iterator, Strategy, and Factory Method. The Factory Method pattern allows defining an interface for creating objects but letting subclasses decide which objects to instantiate. The Strategy pattern defines a family of algorithms, puts each of them in a separate class, and makes their objects interchangeable.
Introduction to Design Patterns and SingletonJonathan Simon
油
This is Class 1 on a 6 week course I taught on Software Design Patterns.
This course provides an introduction into Design Patterns and delves into the Singleton Pattern.
Class based on "Head First Design Patterns."
This document discusses several creational design patterns in software engineering including the Singleton pattern. The Singleton pattern ensures that only one instance of a class is created while providing a global access point to that instance. It defines an Instance operation that allows clients to access its unique instance, which is a class operation. The instance is typically responsible for creating itself and clients access the Singleton instance solely through the Singleton's Instance operation, such as through a static Instance() method that returns the sole instance.
Software Design Patterns. Part I :: Structural PatternsSergey Aganezov
油
In a nutshell, software design patterns are generally reusable solutions to a commonly occurring problems. And this says it all! We are going to learn when it is completely unnecessary for you to reinvent the wheel, and what are the best ways to approach each particular problem during software development process.
This document provides an overview of how to create test classes and methods in JUnit and mock objects using frameworks like PowerMock and EasyMock. It describes how to define a test class that extends TestCase and overrides setUp(), tearDown(), and testXXX() methods. It also explains how PowerMock allows mocking static methods, private methods, final classes/methods by using annotations and bytecode manipulation.
This document provides an overview and agenda for learning about Junit, Mockito, PowerMock, and Hamcrest for testing purposes. It discusses the goal of becoming familiar with these frameworks and libraries. The agenda covers test structure, using Junit with Spring, Mockito stubs and behavior, PowerMock for private/static methods, and key aspects of unit testing like structure, style, and scope. It also provides examples of using Mockito and PowerMock to mock objects, methods, and interactions for testing.
This document discusses design patterns, which are standard solutions to common problems in software design. It defines design patterns and provides examples of different categories of patterns, including creational patterns like Factory Method and Singleton, and structural patterns like Adapter and Facade. For each pattern, it describes the problem, solution, example use cases, and implementations in Java frameworks. Benefits of design patterns include enabling large-scale reuse and making expert knowledge widely available, while drawbacks can include potential for pattern overload.
The document discusses unit testing methods like stubbing and mocking. It introduces Mockito, a mocking framework that makes creating mock objects easier. Mockito allows generating mock objects, setting mock behavior using matchers, and verifying mock interactions. It also discusses partial mocking with spies and cautions against over-specifying mock behavior, which can lead to brittle tests. PowerMock is introduced as an extension to Mockito that enables mocking additional constructs like static methods.
This document discusses unit testing using PowerMock and Mockito frameworks. It explains reasons for and against unit testing and covers testing public API, static, and private methods. PowerMockito is used for mocking static methods and invoking private methods directly. The document also provides tips for writing good unit tests and references for further reading.
in these slides i have explained the factory method design pattern. slides contains complete notes on this pattern from definition to implementation by code example.
Design Patterns - The Ultimate Blueprint for SoftwareEdureka!
油
This document provides an overview of a course on design patterns from Edureka. The course aims to help students understand the need for design patterns, build flexible designs using patterns, design UML diagrams, and code with patterns like AbstractFactory and Observer. It discusses how design patterns provide solutions to common programming problems and help address issues like scalability. The document also provides examples of how patterns like Observer, Mediator and AbstractFactory can be applied.
What is a constructor?
Constructor is a method which gets executed automatically when we create or instantiate object of that class having constructor.
More Highlights of Constructor
A single class can have multiple constructors means we can have more than one constructor in a class. It is also called as overloaded constructor.
A benefit of using a constructor is that it guarantees that the object will go through a proper initialization before an object being used means we can pre-initialize some of the class variables with values before an object being used.
A constructor can be called another constructor by using油"this" keyword. "this"油keyword is the current instance of a class.
The document discusses classes, objects, constructors, and other object-oriented programming concepts in C#:
1) A class defines the data and behavior of a type using variables, methods, and events. Objects are instances of classes that have identity, data, and behaviors defined by the class.
2) Constructors initialize objects and are called using the new keyword. Constructors can be overloaded, parameterized, static, or chained to call another constructor.
3) Classes support concepts like inheritance, hiding, overriding, and polymorphism to extend and customize behavior in derived classes. References and values can be passed into methods.
Name : Prasoon Dadhich
USN : 1MS09IS069
CODE
#include <iostream>
using namespace std;
class Singleton
{
private:
static bool instanceFlag;
static Singleton *single;
Singleton()
{
//private constructor
}
public:
static Singleton* getInstance();
void method();
~Singleton()
{
instanceFlag = false;
}
};
bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
if(! instanceFlag)
{
single = new Singleton();
instanceFlag = true;
return single;
}
else
{
return single;
}
}
void Singleton::method()
{
cout << "Method of the singleton class" << endl;
}
int main()
{
Singleton *sc1,*sc2;
sc1 = Singleton::getInstance();
sc1->method();
sc2 = Singleton::getInstance();
sc2->method();
return 0;
}
Explanation
If we create any object of this class then the static object (player) will be returned and assigned to the new object.
Finally only one object will be there. The point is not how many times something gets executed, the point is, that the work is done by a single instance of the the class. This is ensured by the class method Singleton::getInstance().
As you can see in the above code,Client can access any instance of the singleton only through the getInstance() method.Once an instance is created,the instanceFlag value becomes true and then any further objects pointers created will be given the reference to the same object that has been created.
Also,make sure you notice the presence of a private constructor.This ensures that no new objects of the class are created and that all the class pointers get the same reference to work on.
This document discusses three design patterns: Adapter, Bridge, and Composite.
The Adapter pattern allows classes with incompatible interfaces to work together by wrapping its interface into a compatible one. The Bridge pattern decouples an abstraction from its implementation, allowing them to vary independently. The Composite pattern allows treating individual objects and compositions of objects uniformly.
An introduction to behavioural design patterns in object orientation. Suitable for intermediate to advanced computing students and those studying software engineering.
All 23 classical GOF Design Patterns with illustrations that try to catch some essence of them.
Used in a presentation for Eindhoven Developers, March 17, 2015 http://www.meetup.com/Eindhoven-Developers-Meetup/events/220477660/
Design patterns are optimized, reusable solutions to the programming problems that we encounter every day. A design pattern is not a class or a library that we can simply plug into our system; it's much more than that. It is a template that has to be implemented in the correct situation. It's not language-specific either. A good design pattern should be implementable in mostif not alllanguages, depending on the capabilities of the language. Most importantly, any design pattern can be a double-edged sword if implemented in the wrong place, it can be disastrous and create many problems for you. However, implemented in the right place, at the right time, it can be your savior.
The document discusses various design principles and patterns in Java including the Singleton, Factory Method, Prototype, and Abstract Factory patterns. It provides examples of when and how to apply each pattern, describing their structures, participants, collaborations and consequences. It also covers design principles such as the open-closed principle, dependency inversion, and interface segregation.
The document discusses several creational design patterns: Singleton, Factory, Abstract Factory, and Prototype. The Singleton pattern ensures that only one instance of a class exists. The Factory pattern abstracts object creation by defining a common interface for creating objects. The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes. The Prototype pattern creates objects by cloning prototypes instead of using constructors.
Design patterns are general reusable solutions to common problems in software design. They are not specific designs that can be transformed directly into code, but descriptions that can be applied to many situations. In 1994, the "Gang of Four" authors published the influential book Design Patterns, which introduced design patterns to software development. The book categorized patterns into creational, structural, and behavioral groups. Factory pattern is a creational pattern that provides a way to create objects without exposing object creation logic to the client. It allows for more flexibility in deciding which objects need to be created.
This document provides an overview of design patterns, which are proven solutions to common problems in software design. It discusses different types of patterns like creational, structural, and behavioral patterns. It then gives examples of some common patterns like Iterator, Strategy, and Factory Method. The Factory Method pattern allows defining an interface for creating objects but letting subclasses decide which objects to instantiate. The Strategy pattern defines a family of algorithms, puts each of them in a separate class, and makes their objects interchangeable.
Introduction to Design Patterns and SingletonJonathan Simon
油
This is Class 1 on a 6 week course I taught on Software Design Patterns.
This course provides an introduction into Design Patterns and delves into the Singleton Pattern.
Class based on "Head First Design Patterns."
This document discusses several creational design patterns in software engineering including the Singleton pattern. The Singleton pattern ensures that only one instance of a class is created while providing a global access point to that instance. It defines an Instance operation that allows clients to access its unique instance, which is a class operation. The instance is typically responsible for creating itself and clients access the Singleton instance solely through the Singleton's Instance operation, such as through a static Instance() method that returns the sole instance.
Software Design Patterns. Part I :: Structural PatternsSergey Aganezov
油
In a nutshell, software design patterns are generally reusable solutions to a commonly occurring problems. And this says it all! We are going to learn when it is completely unnecessary for you to reinvent the wheel, and what are the best ways to approach each particular problem during software development process.
This document provides an overview of how to create test classes and methods in JUnit and mock objects using frameworks like PowerMock and EasyMock. It describes how to define a test class that extends TestCase and overrides setUp(), tearDown(), and testXXX() methods. It also explains how PowerMock allows mocking static methods, private methods, final classes/methods by using annotations and bytecode manipulation.
This document provides an overview and agenda for learning about Junit, Mockito, PowerMock, and Hamcrest for testing purposes. It discusses the goal of becoming familiar with these frameworks and libraries. The agenda covers test structure, using Junit with Spring, Mockito stubs and behavior, PowerMock for private/static methods, and key aspects of unit testing like structure, style, and scope. It also provides examples of using Mockito and PowerMock to mock objects, methods, and interactions for testing.
This document discusses design patterns, which are standard solutions to common problems in software design. It defines design patterns and provides examples of different categories of patterns, including creational patterns like Factory Method and Singleton, and structural patterns like Adapter and Facade. For each pattern, it describes the problem, solution, example use cases, and implementations in Java frameworks. Benefits of design patterns include enabling large-scale reuse and making expert knowledge widely available, while drawbacks can include potential for pattern overload.
The document discusses unit testing methods like stubbing and mocking. It introduces Mockito, a mocking framework that makes creating mock objects easier. Mockito allows generating mock objects, setting mock behavior using matchers, and verifying mock interactions. It also discusses partial mocking with spies and cautions against over-specifying mock behavior, which can lead to brittle tests. PowerMock is introduced as an extension to Mockito that enables mocking additional constructs like static methods.
This document discusses unit testing using PowerMock and Mockito frameworks. It explains reasons for and against unit testing and covers testing public API, static, and private methods. PowerMockito is used for mocking static methods and invoking private methods directly. The document also provides tips for writing good unit tests and references for further reading.
in these slides i have explained the factory method design pattern. slides contains complete notes on this pattern from definition to implementation by code example.
Design Patterns - The Ultimate Blueprint for SoftwareEdureka!
油
This document provides an overview of a course on design patterns from Edureka. The course aims to help students understand the need for design patterns, build flexible designs using patterns, design UML diagrams, and code with patterns like AbstractFactory and Observer. It discusses how design patterns provide solutions to common programming problems and help address issues like scalability. The document also provides examples of how patterns like Observer, Mediator and AbstractFactory can be applied.
The Factory Method pattern defines an interface for creating objects but lets subclasses decide which class to instantiate. It provides a way to delegate object instantiation to subclasses. For example, a hotel front desk acts as a factory by creating the appropriate type of room object based on a customer's needs. The pattern involves a Creator class with a factory method that returns a Product object, while ConcreteCreator subclasses override the method to instantiate a ConcreteProduct. This decouples client code from the instantiation process and makes a system extensible to new product types.
Creational Pattern of the design pattern.
Introducing 6 type patterns and also including the pros and cons. It's easy to understand what's kind of scenario it should be used.
The document discusses several design patterns including Observer, State, Template Method, Memento, Command, Chain of Responsibility, Interpreter, Mediator, Iterator, Strategy, Visitor, Flyweight, and Singleton patterns. For each pattern, it provides the definition, participants, structure, intent, caveats, and examples.
Design patterns provide general reusable solutions to common problems in software design. They help structure code and define the way components interact. Some key design patterns discussed in the document include Singleton, Strategy, Decorator, and State. The Singleton pattern ensures a class only has one instance, Strategy encapsulates algorithms to allow flexible changes, Decorator adds functionality dynamically at runtime, and State changes object behavior based on its internal state. Design patterns improve code organization, reuse, and maintenance.
Patterns (contd)Software Development ProcessDesign patte.docxdanhaley45372
油
Patterns (contd)
Software Development Process
Design patterns used to handle change
More time extending and changing code than developing it.
The Strategy design pattern handle change by selecting from a family of external algorithms rather than rewrite.
Design point: Make code closed for modification of code, but open for extension
Problem
Computer object created
Description Method returns
Getting a Computer
Problem
Program has to change every time
Customer changes options
Decorator Pattern
Wrapper code used to extend your core code
Extend a class dynamically at runtime
Decorator uses wrapper code to extend core functionality - decorating the code
Decorator Pattern
description() returns You are getting a computer
Wrapper description() returns
You are getting a computer and a disk
Wrapper description() returns
You are getting a computer and a disk and a monitor
Decorator Pattern
Core component: Computer
Variables holding computer objects should also be able to hold objects that wrap computer objects.
Extend the wrapper classes from the Computer class.
Abstract class cannot be instantiated
Ensures all wrappers are consistent
Developers have to provide their own description
Decorator Pattern
Method calls the core computer objects
description method and adds and a disk
Decorator Pattern
Method calls the core computer objects
description method and adds and a disk
Extend the core object by wrapping it in decorator wrappers. Avoids modification of the core code.
Each successive wrapper called the description method of the object it wrapped and added something to it.
Factory Pattern
Based on type, call the
Connection method
Factory Pattern
Create a method that returns the
correct connection type
Factory Pattern
New operator used to create OracleConnection objects.
New operator used to create SqlServerConnection objects, and MySqlConnection objects.
New operator to instantiate many different concrete classes
Code becomes larger and needs to be replicated in many places
Factor that code out into a method.
Code keeps changing
Encapsulate code into a factory object
Goal: Separate out the changeable code and leave the core code closed for modification
Building the Factory
Creating the Factory
FirstFactory class encapsulates the connection object creation
Pass to it the type of connection (Oracle, SQL Server,)
Use the factory object to create connection objects with a factory method named createConnection
Building the Factory
Create the FirstFactory class.
Save the type of the database, passed to the FirstFactory classs constructor.
Object-creation code changes
Check which type of object to be created
(OracleConnection, SqlServerConnection,
and then create it.
Factory Class
Create the Abstract Connection Class
Core code should not be modified or has to be modified
as little as possible.
Using the connection object returned by the
new factory object
Use t.
The document discusses software design patterns and how they can help address common problems that arise during design. It describes three main categories of patterns - creational, structural, and behavioral - and provides examples of specific patterns like Singleton, Factory, Adapter, and Bridge. The key benefits of design patterns are that they provide proven, reusable solutions to general problems and help create flexible, modular designs that can more easily adapt to changes.
The first seminar in the mini-seminars periodical sessions I've prepared and lead in my spare time while being employed at Exigen Services. Kudos, guys!
Since these presentations were spare time hobby - I've decided to share them :)
Hopefully someone will find them useful.
The intro is - what designs patters are about, some simple examples and a lots of colorful images.
Object design is the process of refining requirements analysis models and making implementation decisions to optimize execution time, memory usage, and other performance measures. It involves four main activities: service specification to define class interfaces; component selection and reuse of existing solutions; restructuring models to improve code reuse; and optimization to meet performance requirements. During object design, interfaces are fully specified with visibility, type signatures, and contracts to clearly define class responsibilities.
Luis Valencia will give a presentation on applying Typescript design patterns to React/SPFx. He has 17 years of experience including 10 years in SharePoint. The session will focus on writing clean, maintainable code and not flashy user interfaces. It will cover common design patterns like singleton, abstract factory, builder, and factory method. It will also discuss SOLID principles and building shared code libraries. The presentation aims to teach developers how to write code that is easy for others to maintain.
This document discusses the Factory Method design pattern. The Factory Method pattern defines an interface for creating objects but allows subclasses to decide which class to instantiate. This allows subclasses to vary the type of objects that will be created. The code examples demonstrate creating different types of pages for documents by overriding the factory method in subclasses to return the specific page product.
The document discusses the Composite design pattern, which composes objects into tree structures to represent part-whole hierarchies. The pattern allows clients to treat individual objects and compositions of objects uniformly. It also discusses the Adapter pattern, which converts the interface of one class into another interface that clients expect, and the Facade pattern, which provides a simplified interface to a complex subsystem.
This document discusses software design patterns, which provide reusable solutions to common problems in software design. It covers creational patterns like singleton and abstract factory, structural patterns like adapter and bridge, and behavioral patterns like iterator and observer. Patterns help developers solve recurring problems in an elegant and reusable way by providing tried-and-tested solutions to common design problems. The document also discusses categories of patterns, their elements, and when to use different patterns.
This is a presentation I did for the Cedar Rapids .NET User Group (CRineta.org). It was intended to present object oriented concepts and their application in .NET and C#.
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Steven Smith
油
Design patterns help developers and teams solve problems using proven approaches. In this talk, you'll learn how to solve a series of real world problems by applying patterns. Not only do patterns help individual developers solve particular problems, but they also enable teams to discuss design decisions using a richer, more descriptive language. By the end, you'll have some concrete tools you can apply, and hopefully the desire to master more patterns as you continue to improve!
Framework Design Guidelines For Brussels Users Groupbrada
油
This document summarizes 10 years of experience with framework design guidelines from Microsoft. It discusses core principles of framework design that have remained the same over 10 years, such as layering dependencies and managing types. It also outlines new advances like test-driven development, dependency injection, and tools for dependency management and framework design. The document concludes by emphasizing that framework design principles have stayed consistent while new techniques have emerged to help implement those principles.
This document discusses various design patterns in Python and how they compare to their implementations in other languages like C++. It provides examples of how common patterns from the Gang of Four book like Singleton, Observer, Strategy, and Decorator are simplified or invisible in Python due to features like first-class functions and duck typing. The document aims to illustrate Pythonic ways to implement these patterns without unnecessary complexity.
Framework design involves balancing many considerations, such as:
- Managing dependencies between components to allow for flexibility and evolution over time. Techniques like dependency injection and layering help achieve this.
- Designing APIs by first writing code samples for key scenarios and defining object models to support these samples to ensure usability.
- Treating simplicity as a feature by removing unnecessary requirements and reusing existing concepts where possible.
5. Motivation Advantages of a standard design Speed up the development process More robust solution Easy to maintain Easy to evolve Allow beginners to act as experienced professional
6. Brief history 1977 - Christopher Alexander - Architecture A Pattern Language: Towns, Buildings, Construction Mid-to-late 1980 Object oriented use 1995 - Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Gang of Four
7. Definition Christopher Alexander "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"
8. Definition A design pattern is defined using the following template: Pattern name and classification: A conceptual handle and category for the pattern Intent: What problem does the pattern address? Also known as: Other common names for the pattern Motivation: A scenario that illustrates the problem Applicability: In what situations can the pattern be used? Structure: Diagram UML Participants: Classes and objects in design
9. Definition A design pattern is defined using the following template: Collaborations: How classes and objects in the design collaborate Consequences: What objectives does the pattern achieve? What are the tradeoffs? Implementation: Implementation details to consider, language-specific issues Sample code: Sample code in a selected language (Smalltalk, Java or C++) Known uses: Examples from the real world Related patterns: Comparison and discussion of related patterns
10. Sample Singleton Creational Pattern Pattern name and Classification Ensure a class only has one (or a limited number of) instance, and provide a global point of access to it. Intention
11. Singleton Singleton define a getInstance operation that lets clients access its unique instance. Structure and Participants
12. Singleton Controlled access to sole instance. Improvement over global variables. Permits a variable number of instances Consequences
13. Singleton Implementation and Code public class Singleton { private static Singleton instance; private Singleton(){ } public static synchronized Singleton getInstance() { if (instance == null ) { instance = new Singleton(); } return instance; } }
17. Fundamental Patterns They are fundamental in the sense that they are widely used by other patterns or are frequently used in a large number of programs. Interface Immutable Proxy*
18. Structural Patterns A re concerned with how classes and objects are composed to form larger structures Structural class patterns use inheritance to compose interfaces or implementations Rather than composing interfaces or implementations, structural object patterns describe ways to compose objects to realize new functionality Proxy Facade Composite Decorator Adapter
19. Behavioral Patterns Patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication Related to interaction and responsability. Design Patterns (GoF): Observer Strategy Visitor Chain of Responsability Command Iterator
20. Creational Patterns Creational design patterns abstract the instantiation process Help make a system independent of how its objects are created, composed, and represented Encapsulate knowledge about which concrete classes the system uses. Hide how instances of classes are created and put together Design Patterns: Singleton Abstract Factory Factory Method Prototype
21. Some More Patterns Samples Structural Decorator Creational Abstract Factory
22. Decorator Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality Intention Motivation
23. Decorator To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects For responsibilities that can be withdrawn When extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing Applicability
25. Decorator Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request More flexibility than static inheritance Avoids feature-laden classes high up in the hierarchy A decorator and its component aren't identical Lots of little objects Consequences
26. Decorator Implementation and Code public class LowerCaseInputStream extends FilterInputStream { public LowerCaseInputStream( InputStream in) { super ( in ); } public int read() throws IOException { int c = super .read(); return ( c == -1 ? c : Character .toLowerCase( ( char )c ) ); } public int read( byte [] b, int offset, int len) throws IOException { int result = super.read(b, offset, len); for ( int i = offset; i < offset+result; i++) { b[i] = ( byte ) Character .toLowerCase( ( char )b[i] ); } return result; } } public class InputTest { public static void main( String [] args) { int c; try { InputStream in = new LowerCaseInputStream( new BufferedInputStream ( new FileInputStream ( test.txt ) ) ); while ( (c = in.read()) >= 0 ) { System . out . print ( ( char )c ); } in.close(); } catch ( IOException e) { e.printStackTrace(); } } }
28. Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Intention Motivation GUI
29. Abstract Factory independence of how its products are created, composed and represented a system should be configured with one of multiple families of products a family of related product objects is designed to be used together. provide a class library of products, and reveal just their interfaces, not their implementations. Applicability
31. Abstract Factory It isolates concrete classes. It makes exchanging product families easy. It promotes consistency among products. Supporting new kinds of products is hard. (new methods) Consequences
32. Abstract Factory public interface class DAOFactory { public User getUser(); } public class MySQLDAOFactory implements DAOFactory { public User getUser() { MySqlConnection conn = new MySqlConnection(); User u = new MySqlUser(); u.setName(rs.getString(NAME)); return u ; } } public class Client { public static void main(String[] args){ DAOFactory factory = new MySQLDAOFactory(); User userDao = factory.getUser(); } } public class Db2DAOFactory implements DAOFactory { public User getUser() { Db2Connection conn = new Db2Connection(); User u = new Db2User(); u.setName(rs.getString(NAME)); return u ; } }
36. Conclusion Design Patterns its based on three aspects: Context, problem and solution Can be implemented in N ways Speed up development Allow easier maintenance Easy to extend the solution Improve the solution
37. Resources Design Patterns bestseller book: Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides , Design Patterns: Elements of Reusable Object-Oriented Software , Addison-Wesley, 1994. ISBN 0-201-63361-2 Wikipedia website http:// en.wikipedia.org/wiki/Adapter_pattern and http:// en.wikipedia.org/wiki/Decorator_pattern . Object Oriented System course from Department of Computer Science of University of Texas at San Antonio http://vip.cs.utsa.edu/classes/cs4773s2004/index.html . Joseph Bergin professor at Pace University personal website http://csis.pace.edu/~bergin/