The document discusses design patterns. It begins with an introduction to design patterns, covering their history and definition. It then covers several fundamental design principles for patterns, including avoiding tight coupling, favoring composition over inheritance, and the single responsibility principle. Finally, it discusses several specific design patterns, including factory method, singleton, strategy, state, and proxy patterns. It provides examples of when and how to apply these patterns.
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 most—if not all—languages, 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.
Je vous partage l'un des présentations que j'ai réalisé lorsque j'étais élève ingénieur pour le module 'Anglais Business ' , utile pour les étudiants souhaitant préparer une présentation en anglais sur les Design Pattern - ou les patrons de conception .
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 most—if not all—languages, 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.
Je vous partage l'un des présentations que j'ai réalisé lorsque j'étais élève ingénieur pour le module 'Anglais Business ' , utile pour les étudiants souhaitant préparer une présentation en anglais sur les Design Pattern - ou les patrons de conception .
YouTube Link: https://youtu.be/C_oPLDaSy-8
**Java, J2EE & SOA Certification Training - https://www.edureka.co/java-j2ee-training-course **
This Edureka PPT on "design patterns in java" will provide you with detailed knowledge about Java Design Patterns and along with it, This PPT will also cover some real-time examples of some important Design Patterns in Java, in order to provide you with a deep understanding about their functionality. This PPT will cover the following topics:
Why do we need Design Patterns?
What are Design Patterns?
Structure of a Design Pattern
Types of Design Patterns
Creational Design Pattern
Structural Design Pattern
Behavioural Design Pattern
JEE Design Pattern
Overview of design patterns
Follow us to never miss an update in the future.
YouTube: https://www.youtube.com/user/edurekaIN
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
Castbox: https://castbox.fm/networks/505?country=in
ISO SQL:2016 introduced Row Pattern Matching: a feature to apply (limited) regular expressions on table rows and perform analysis on each match. As of writing, this feature is only supported by the Oracle Database 12c.
YouTube Link: https://youtu.be/C_oPLDaSy-8
**Java, J2EE & SOA Certification Training - https://www.edureka.co/java-j2ee-training-course **
This Edureka PPT on "design patterns in java" will provide you with detailed knowledge about Java Design Patterns and along with it, This PPT will also cover some real-time examples of some important Design Patterns in Java, in order to provide you with a deep understanding about their functionality. This PPT will cover the following topics:
Why do we need Design Patterns?
What are Design Patterns?
Structure of a Design Pattern
Types of Design Patterns
Creational Design Pattern
Structural Design Pattern
Behavioural Design Pattern
JEE Design Pattern
Overview of design patterns
Follow us to never miss an update in the future.
YouTube: https://www.youtube.com/user/edurekaIN
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
Castbox: https://castbox.fm/networks/505?country=in
ISO SQL:2016 introduced Row Pattern Matching: a feature to apply (limited) regular expressions on table rows and perform analysis on each match. As of writing, this feature is only supported by the Oracle Database 12c.
Design Pattern is advance topic in Software Engineering and it is very important,you will find in this presentation explanation State Machine Pattern,Singleton Pattern and
Façade Pattern
The SQL OFFSET keyword is evil. It basically behaves like SLEEP in other programming langauges: the bigger the number, the slower the execution.
Fetching results in a page-by-page fashion in SQL doesn't require OFFSET at all but an even simpler SQL clause. Besides being faster, you don't have to cope with drifting results if new data is inserted between two page fetches.
Software Design Patterns - Selecting the right design patternJoao Pereira
This is part of my course on Object Oriented Programming. It talks about design patterns, the famous GOF patterns, and how to select the right design pattern.
Modern SQL in Open Source and Commercial DatabasesMarkus Winand
SQL has gone out of fashion lately—partly due to the NoSQL movement, but mostly because SQL is often still used like 20 years ago. As a matter of fact, the SQL standard continued to evolve during the past decades resulting in the current release of 2016. In this session, we will go through the most important additions since the widely known SQL-92. We will cover common table expressions and window functions in detail and have a very short look at the temporal features of SQL:2011 and row pattern matching from SQL:2016.
Links:
http://modern-sql.com/
http://winand.at/
http://sql-performance-explained.com/
PATTERNS05 - Guidelines for Choosing a Design PatternMichael Heron
An discussion about choosing design patterns in object orientation. Suitable for intermediate to advanced computing students and those studying software engineering.
Prepared by,
Anindya Sundar Paul
CSE, University of Dhaka
Zahin Jawad
CSE, University of Dhaka
Factory pattern is one of the software design patterns used in order to design reusable and extensible systems. The pattern deals with the creation of objects. In this presentation we tried to give an overview of the pattern and how it works both in an informal and then a formal manner.
in these slides i have explained the Abstract Factory Design pattern. slides includes definition, explanation and then implementation by code examples.
In these slides i have explained an important design pattern that is "singleton pattern".
slides includes everything required about it, from definition to implementation and also different ways to achieve it according to situation and requirements.
Lecture two,
An introduction to Design Pattern
History
Pattern Language,
Categorization according to GoF
MVC
Creational Design Patterm
Factory Method
Abstract Factory
Singleton
Builder
Object Oriented Programming is revisited. It is assumed that students know OO languages so this is more of a review. We will cover concepts such as encapsulation, interfaces and polymorphism. These are important concepts that students must understand in order to write flexible and lasting code. We look at several design principles.
We also look at software design and take an example from a video games.
In this lecture we will also reveal the secret of programming which all good programmers must know.
This is an intermediate conversion course for C++, suitable for second year computing students who may have learned Java or another language in first year.
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisGlobus
JASMIN is the UK’s high-performance data analysis platform for environmental science, operated by STFC on behalf of the UK Natural Environment Research Council (NERC). In addition to its role in hosting the CEDA Archive (NERC’s long-term repository for climate, atmospheric science & Earth observation data in the UK), JASMIN provides a collaborative platform to a community of around 2,000 scientists in the UK and beyond, providing nearly 400 environmental science projects with working space, compute resources and tools to facilitate their work. High-performance data transfer into and out of JASMIN has always been a key feature, with many scientists bringing model outputs from supercomputers elsewhere in the UK, to analyse against observational or other model data in the CEDA Archive. A growing number of JASMIN users are now realising the benefits of using the Globus service to provide reliable and efficient data movement and other tasks in this and other contexts. Further use cases involve long-distance (intercontinental) transfers to and from JASMIN, and collecting results from a mobile atmospheric radar system, pushing data to JASMIN via a lightweight Globus deployment. We provide details of how Globus fits into our current infrastructure, our experience of the recent migration to GCSv5.4, and of our interest in developing use of the wider ecosystem of Globus services for the benefit of our user community.
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Anthony Dahanne
Les Buildpacks existent depuis plus de 10 ans ! D’abord, ils étaient utilisés pour détecter et construire une application avant de la déployer sur certains PaaS. Ensuite, nous avons pu créer des images Docker (OCI) avec leur dernière génération, les Cloud Native Buildpacks (CNCF en incubation). Sont-ils une bonne alternative au Dockerfile ? Que sont les buildpacks Paketo ? Quelles communautés les soutiennent et comment ?
Venez le découvrir lors de cette session ignite
Globus Connect Server Deep Dive - GlobusWorld 2024Globus
We explore the Globus Connect Server (GCS) architecture and experiment with advanced configuration options and use cases. This content is targeted at system administrators who are familiar with GCS and currently operate—or are planning to operate—broader deployments at their institution.
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar
The European Union Agency for Law Enforcement Cooperation (Europol) has suffered an alleged data breach after a notorious threat actor claimed to have exfiltrated data from its systems. Infamous data leaker IntelBroker posted on the even more infamous BreachForums hacking forum, saying that Europol suffered a data breach this month.
The alleged breach affected Europol agencies CCSE, EC3, Europol Platform for Experts, Law Enforcement Forum, and SIRIUS. Infiltration of these entities can disrupt ongoing investigations and compromise sensitive intelligence shared among international law enforcement agencies.
However, this is neither the first nor the last activity of IntekBroker. We have compiled for you what happened in the last few days. To track such hacker activities on dark web sources like hacker forums, private Telegram channels, and other hidden platforms where cyber threats often originate, you can check SOCRadar’s Dark Web News.
Stay Informed on Threat Actors’ Activity on the Dark Web with SOCRadar!
Advanced Flow Concepts Every Developer Should KnowPeter Caitens
Tim Combridge from Sensible Giraffe and Salesforce Ben presents some important tips that all developers should know when dealing with Flows in Salesforce.
Cyaniclab : Software Development Agency Portfolio.pdfCyanic lab
CyanicLab, an offshore custom software development company based in Sweden,India, Finland, is your go-to partner for startup development and innovative web design solutions. Our expert team specializes in crafting cutting-edge software tailored to meet the unique needs of startups and established enterprises alike. From conceptualization to execution, we offer comprehensive services including web and mobile app development, UI/UX design, and ongoing software maintenance. Ready to elevate your business? Contact CyanicLab today and let us propel your vision to success with our top-notch IT solutions.
top nidhi software solution freedownloadvrstrong314
This presentation emphasizes the importance of data security and legal compliance for Nidhi companies in India. It highlights how online Nidhi software solutions, like Vector Nidhi Software, offer advanced features tailored to these needs. Key aspects include encryption, access controls, and audit trails to ensure data security. The software complies with regulatory guidelines from the MCA and RBI and adheres to Nidhi Rules, 2014. With customizable, user-friendly interfaces and real-time features, these Nidhi software solutions enhance efficiency, support growth, and provide exceptional member services. The presentation concludes with contact information for further inquiries.
Unleash Unlimited Potential with One-Time Purchase
BoxLang is more than just a language; it's a community. By choosing a Visionary License, you're not just investing in your success, you're actively contributing to the ongoing development and support of BoxLang.
How to Position Your Globus Data Portal for Success Ten Good PracticesGlobus
Science gateways allow science and engineering communities to access shared data, software, computing services, and instruments. Science gateways have gained a lot of traction in the last twenty years, as evidenced by projects such as the Science Gateways Community Institute (SGCI) and the Center of Excellence on Science Gateways (SGX3) in the US, The Australian Research Data Commons (ARDC) and its platforms in Australia, and the projects around Virtual Research Environments in Europe. A few mature frameworks have evolved with their different strengths and foci and have been taken up by a larger community such as the Globus Data Portal, Hubzero, Tapis, and Galaxy. However, even when gateways are built on successful frameworks, they continue to face the challenges of ongoing maintenance costs and how to meet the ever-expanding needs of the community they serve with enhanced features. It is not uncommon that gateways with compelling use cases are nonetheless unable to get past the prototype phase and become a full production service, or if they do, they don't survive more than a couple of years. While there is no guaranteed pathway to success, it seems likely that for any gateway there is a need for a strong community and/or solid funding streams to create and sustain its success. With over twenty years of examples to draw from, this presentation goes into detail for ten factors common to successful and enduring gateways that effectively serve as best practices for any new or developing gateway.
Enhancing Research Orchestration Capabilities at ORNL.pdfGlobus
Cross-facility research orchestration comes with ever-changing constraints regarding the availability and suitability of various compute and data resources. In short, a flexible data and processing fabric is needed to enable the dynamic redirection of data and compute tasks throughout the lifecycle of an experiment. In this talk, we illustrate how we easily leveraged Globus services to instrument the ACE research testbed at the Oak Ridge Leadership Computing Facility with flexible data and task orchestration capabilities.
Designing for Privacy in Amazon Web ServicesKrzysztofKkol1
Data privacy is one of the most critical issues that businesses face. This presentation shares insights on the principles and best practices for ensuring the resilience and security of your workload.
Drawing on a real-life project from the HR industry, the various challenges will be demonstrated: data protection, self-healing, business continuity, security, and transparency of data processing. This systematized approach allowed to create a secure AWS cloud infrastructure that not only met strict compliance rules but also exceeded the client's expectations.
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...informapgpstrackings
Keep tabs on your field staff effortlessly with Informap Technology Centre LLC. Real-time tracking, task assignment, and smart features for efficient management. Request a live demo today!
For more details, visit us : https://informapuae.com/field-staff-tracking/
How Recreation Management Software Can Streamline Your Operations.pptxwottaspaceseo
Recreation management software streamlines operations by automating key tasks such as scheduling, registration, and payment processing, reducing manual workload and errors. It provides centralized management of facilities, classes, and events, ensuring efficient resource allocation and facility usage. The software offers user-friendly online portals for easy access to bookings and program information, enhancing customer experience. Real-time reporting and data analytics deliver insights into attendance and preferences, aiding in strategic decision-making. Additionally, effective communication tools keep participants and staff informed with timely updates. Overall, recreation management software enhances efficiency, improves service delivery, and boosts customer satisfaction.
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTier1 app
Even though at surface level ‘java.lang.OutOfMemoryError’ appears as one single error; underlyingly there are 9 types of OutOfMemoryError. Each type of OutOfMemoryError has different causes, diagnosis approaches and solutions. This session equips you with the knowledge, tools, and techniques needed to troubleshoot and conquer OutOfMemoryError in all its forms, ensuring smoother, more efficient Java applications.
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Shahin Sheidaei
Games are powerful teaching tools, fostering hands-on engagement and fun. But they require careful consideration to succeed. Join me to explore factors in running and selecting games, ensuring they serve as effective teaching tools. Learn to maintain focus on learning objectives while playing, and how to measure the ROI of gaming in education. Discover strategies for pitching gaming to leadership. This session offers insights, tips, and examples for coaches, team leads, and enterprise leaders seeking to teach from simple to complex concepts.
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus
As part of the DOE Integrated Research Infrastructure (IRI) program, NERSC at Lawrence Berkeley National Lab and ALCF at Argonne National Lab are working closely with General Atomics on accelerating the computing requirements of the DIII-D experiment. As part of the work the team is investigating ways to speedup the time to solution for many different parts of the DIII-D workflow including how they run jobs on HPC systems. One of these routes is looking at Globus Compute as a way to replace the current method for managing tasks and we describe a brief proof of concept showing how Globus Compute could help to schedule jobs and be a tool to connect compute at different facilities.
3. History
1977: Christopher Alexander introduces the idea of patterns:
successful solutions to problems. (Pattern Language)
1987: Ward Cunningham and Kent Beck leverage
Alexander’s idea in the context of an OO language.
1987: Eric Gamma’s dissertation on importance of patterns
and how to capture them.
1994: The book ====>
Definition
In software engineering, a software design pattern is a general reusable solution to a
commonly occurring problem within a given context in software design. It is not a
finished design that can be transformed directly into source or machine code.
6. ‘Code as modular black boxes’ && ‘Single responsibility principles’
Fundamental Principles
7. ‘Program to an interface and not to an implementation’
The class shouldn’t care about which class they use, but more which
behaviour they will use.
To send messages from class to class, send interfaces not classes.
Be careful ! Don’t over-use interface => 1 class != 1 interface
Fundamental Principles
8. public class Main {
public static void main(String[] args) {
Animal[] animals = new Animal[3];
animals[0] = new Dog();
animals[1] = new Cat();
animals[2] = new Fox();
for (Animal animal : animals) {
animal.speak();
}
}
}
An interface is a reference type in Java.
It is similar to class. It is a collection of abstract methods. A class implements an interface,
thereby inheriting the abstract methods of the interface. Along with abstract methods, an
interface may also contain constants, default methods, static methods, and nested types.
Interlude: Interface - Recall
Animal
Dog Cat Fox
public interface Animal {
public void speak();
}
public class Dog implements Animal {
public void speak() {System.out.println("Wouaf Wouaf!");}
}
public class Cat implements Animal {
public void speak() {System.out.println("Miaouh!");}
}
public class Fox implements Animal {
public void speak() {
System.out.println("What does the Fox say ?");
}
}
9. ‘Favor Object Composition over inheritance’
The class shouldn’t care about which class they use, but more which
behaviour they will use.
To send messages from class to class, send interfaces not classes.
Bad use of inheritance:
My personal opinion is that there is no "better" or "worse" principle to design. There is "appropriate" and "inadequate" design for the concrete task. In
other words - I use both inheritance or composition, depending on the situation. The goal is to produce smaller code, easier to read, reuse and
eventually extend further.
class Stack extends ArrayList {
public void push(Object value) { … }
public Object pop() { … }
}
Fundamental Principles
1
4
54
2
class Stack extends {
private ArrayList myList;
public void push(Object value) { … }
public Object pop() { … }
}
10. ‘A design must be open for extension but closed for modification’
(Open/Close Principle)
=> Once a class is written, you should not have to modify it to add new
functionality.
Fundamental Principles
12. - SRP: Single Responsibility Principle
- OCP: Open Close Principle
- LSP: Liskov Substitution Principle. (Derived classes must be substitutable for their
base classes).
- ISP: Interface Segregation Principle. (Client should not be forced to depend upon
interfaces they do not use).
- DIP: Dependency Inversion Principle. High level modules should not depend upon
low level modules. Both should depend upon abstraction).
Fundamental Principles
13. Keep in mind: “When Coding, 80% of your time is spent by reading code”. So take
care of formatting and naming !
Others Principles
14. - DRY: Don’t Repeat Yourself. Can Lead to maintenance nightmare
- YAGNI: You ain’t gonna need it. Do not add functionality until necessary.
- KISS: Keep It Simple, Stupid. Avoid accidental complexity.
- Boy Scout Rules: Always leave the campground cleaner than you found it.
Others Principles
16. Class Diagram
· Upper part contains the name of the class
· Middle part describes attributes of the class
· Bottom part describes the methods of the class
public class Example {
public static int CONSTANT= 0;
private String attribute1;
public String method() {
return attribute1;
}
private static void privateMethod(int c) {
CONSTANT+=c;
}
}
UML - Recalls
17. Relations between classes
The simplest of relation :
· Class A depends on Class B if, at some point, it uses Class B
· Symbolized by dotted line and and open arrow indicating the dependency direction
public class A {
public String method(B b) {
return b.toString();
}
}
public class B {
public toString() {
return "B";
}
}
UML - Recalls
18. Association
Stronger dependency from class A to class B, namely, A uses and contains an instance of class B:
· It is represented by a plain line and an open arrow
· This association can have a cardinality 0..1 or 1..n
· It is directed , in the example below A knows B, but B does not know anything about A
public class A {
private B b;
public void setB(B b) {
this.b = b;
}
}
public class B {
public toString() {
return "B";
}
}
UML - Recalls
19. Aggregation
Our example models Rockband and members. A rockband can have several members. And Members can be in several Rockand.
The addition of the diamond adds information about the life of the objects
· The empty diamond signifies that ‘Member’ objects can be shared between several ‘RockBand’ objects
· When an ‘RockBand’ object is destroyed, then the instances of ‘Member’ it was associated with do not disappear.
UML - Recalls
20. Composition
· The plain/full diamond indicates that the contained objects are not shared
· When the container object is destroyed, the contained objects disappear with it
UML - Recalls
class Tattoo {
String description;
public Tattoo(String description) {
this.description = description;
}
}
class Member {
private String name;
private List<Tattoo> tattoos = new
ArrayList<>();
[...]
public ink(String description) {
tattoos.add(new Tattoo(description));
}
}
class RockBand {
private List<Member> members = new ArrayList<>();
public void addMember(Member m) {
members.add(m);
}
public Member getMember(int num) {
return members.get(num);
}
public void toString() {
[..]
}
}
21. Inheritance / Generalization
· Inheritance enables to create classes hierarchies sharing same attributes
· Children classes also have the attributes and methods of the parent classes,
· These attributes and methods are defined for the objects the children classes.
· This mechanism enables Polymorphism
Properties
· Transitivity: If B inherits from A and C inherits from B then C inherits from A
· Not reflexive: No class can inherit from itself
· Not symmetric: If B inherits from A, A does not inherit from B
· Not cyclic : If B inherits from A and C inherits from B then A can't inherit from C
UML - Recalls
22. Inheritance / Generalization - Code
public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class Cat extends Animal {
public Cat(String name) {
super(name); // Call to the parent class constructor
}
public void meow() {
System.out.println("Miaou " + super.getName() + " Miaou");
}
}
public class Dog extends Animal {
public Dog(String name) {
super(name); // Call to the parent class constructor
}
public void bark() {
System.out.println("Wouf " + super.getName() + " Wouf");
}
}
UML - Recalls
25. Creational Pattern
Creational design patterns are design patterns that deal with object creation
mechanisms, trying to create objects in a manner suitable for the situation.
- Factory Method Pattern
- Abstract Factory
- Singleton
- Builder
- Prototype
26. Intent:
It allows to create objects without specifying their class (method whose main goal
is to create a class instance).
Application:
- A class can not anticipate the type of objects it must create (i.e.: framework).
- A class wants its subclasses to specify the type of objects it creates.
- A class needs controls over the creation of its objects.
Factory Method Pattern
// Problem: A depends on B
public class A {
public void doIt() {
B b = new B()
}
}
27. Structure:
public abstract class Animal() {}
public class Dog extend Animal() {}
public class Cat extend Animal() {}
public class Dolphin extend Animal() {}
public interface AnimalFactory {
abstract public Animal create(String type);
}
public AnimalFactory () {
public Animal create(String type) {
switch(type) {
case "Dog": return new Dog(); break;
case "Cat": return new Cat(); break;
case "Dolphin": return new Dolphin(); break;
default : throw Exception("Animal " + type + " is unknown.");
}
}
}
public Main() {
public static void main(String[] args) {
Animalfactory factory = new Animalfactory();
Animal cat = factory.create("cat");
}
}
Factory Method Pattern
28. Intent:
We want to have only one instance of a class.
Application:
- Instantiate an object can take time and
memory.
- We only need one instance.
// Example:
// Problem: For factory, we only need one
instance
Singleton Pattern
29. Singleton Pattern
Structure:
The trick is to use a static variable to
remember the instance.
Then, why not to create a static method ?
The big difference between a singleton and a bunch of static
methods is that singletons can implement interfaces, so you can
pass around the singleton as if it were "just another"
implementation.
MyClass
- static MyClass myClass;
static getInstance(): MyClass
C
public class AnimalFactory {
private AnimalFactory();
private static AnimalFactory animalFactory;
public static AnimalFactory getInstance() {
if (animalFactory == null) {
animalFactory = new AnimalFactory();
}
return animalFactory;
}
//…...
}
public Main() {
public static void main(String[] args) {
Animal cat = Animalfactory.getInstance().create("cat");
}
}
30. Creational Pattern
- Factory Method Pattern
- Abstract Factory
- Singleton
- Builder
- Prototype (très utilisé dans Javascript)
31. Behavioral Pattern
Behavioral patterns are concerned with the assignment of responsibilities between
objects, or, encapsulating behavior in an object and delegating requests to it.
- Observer
- Strategy
- State
- Visitor
- Memento
- Mediator
- Iterator
32. Intent:
Supports a relation One To Many so that
several objects get notified when an
object changes its state and can react.
Application:
- Widely use with IHM when several
elements of the view change when
the model changes.
Observer Pattern
33. Roles: subject and several observers
- Subject:
- Can modify his state.
- He notifies observers when he has
changed.
- Can give his new state
- Observers
- Can subscribe/unsubscribe to a subject
Observer Pattern
subscribe
34. public abstract class AbstractSubject {
private List<Observer> observers = new ArrayList<Observer>();
public void attach(Observer observer){observers.add(observer);}
public void notifyAllObservers(){
for (Observer observer : observers) {
observer.update();
}
}
}
public class Subject extends AbstractSubject{
private int state;
public int getState() {return state;}
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
}
public class BinaryObserver extends Observer{
public BinaryObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Binary String: " + Integer.toBinaryString(
subject.getState() ) );
}
}
public class ObserverPatternDemo {
public static void main(String[] args) {
AbstractSubject subject = new Subject();
new HexaObserver(subject);
new OctalObserver(subject);
new BinaryObserver(subject);
System.out.println("First state change: 15");
subject.setState(15);
System.out.println("Second state change: 10");
subject.setState(10);
}
}
Observer Pattern
35. Observer Pattern
Abstract Subject => Classe java.util.Observable
- addObserver(Observer obs)
- notifyObservers();
Concrete Subject => extends Observable
- When state change, you should call notifyObservers();
Abstract Observer: Interface java.util.Observer
- update(Observable obs, Object args);
Concrete Observer: implements java.util.Observer
37. Intent:
It allows to switch between differents algorithms to accomplish a task.
Application:
- Different variants of an algorithm
- Many related classes differ only in their behaviour
Strategy Pattern
38. Subject => Interface to outside world
Strategy (Algorithm) => common interface for the differents
algorithms.
Strategy Pattern
39. Program
- Input: read file
- Output: filtered file
Examples of 4 filters:
- No filtering
- Only words that start with t
- Only words longer than 5 characters
- Only words that are palindroms
Strategy Pattern - Example
40. public class StrategyPattern {
public static void main(String[] args) {
Context context = new Context();
String filename = args[0];
System.out.println("n* Default(Alll): ");
context.filter(filename);
System.out.println("n* Start with T: ");
context.changeStrategy(new StartWithT());
context.filter(filename);
}
}
public interface CheckStrategy {
public boolean check(String s);
}
public class All implements CheckStrategy {
@Override
public boolean check(String s) {
return true;
}
}
public class StartWithT implements CheckStrategy {
@Override
public boolean check(String s) {
return s != null && s.startWith("t");
}
}
public class Context {
private CheckStrategy strategy;
public Context() {this.strategy = new All();}
public void changeStrategy(CheckStrategy strategy) {
this.strategy = strategy;
}
public void filter(String filename) throws IOException {
BufferedReader infile = new BufferedReader(new
FileReader(filename));
String buffer = null;
while ((buffer = infile.readLine() != null) {
StringTokenizer word = new StringTokenizer(buffer);
while (words.hasMoreTokens()) {
String word = words.nextToken();
if (strategy.check(word)) {
Ssytem.out.println(word);
}
}
}
}
}
Strategy Pattern - Example
42. State Pattern
Intent:
Implements a state machine in an object-
oriented way.
It lets an object show other methods after a
change of internal state.
Application:
- an application is characterized by large and
numerous case statements. These cases
vector flow of control based on the state of
the application.
43. interface Statelike {
void writeName(StateContext context, String name);
}
class StateLowerCase implements Statelike {
@Override
public void writeName(final StateContext context, final String name) {
System.out.println(name.toLowerCase());
context.setState(new StateMultipleUpperCase());
}
}
class StateMultipleUpperCase implements Statelike {
/** Counter local to this state */
private int count = 0;
@Override
public void writeName(final StateContext context, final String name) {
System.out.println(name.toUpperCase());
/* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
if (++count > 1) {context.setState(new StateLowerCase());}
}
}
class StateContext {
private Statelike myState;
StateContext() {
setState(new StateLowerCase());
}
void setState(final Statelike newState) {myState = newState;}
public void writeName(final String name) {
myState.writeName(this, name);
}
}
State Pattern
public class DemoOfClientState {
public static void main(String[] args) {
final StateContext sc = new StateContext();
sc.writeName("Monday");
sc.writeName("Tuesday");
sc.writeName("Wednesday");
sc.writeName("Thursday");
sc.writeName("Friday");
sc.writeName("Saturday");
sc.writeName("Sunday");
}
}
44. Structural Patterns
Design patterns that ease the design by identifying a simple way to realize
relationships between entities.
- Proxy
- Decorator
- Facade
- Adapter
- Aggregate
- Bridge
45. Proxy Pattern
Translation in French:
Proxy <=> ?????????
Intent:
- A proxy, in its most general form, is a
class functioning as an interface to
something else.
Application:
- The proxy could interface to
anything: a network connection, a
large object in memory, a file.
- For sensitive objects: can verify that
the caller has the authorization.
46. interface Image {
public void displayImage();
}
// On System A
class RealImage implements Image {
private String filename = null;
public RealImage(final String filename) {
this.filename = filename;
loadImageFromDisk();
}
private void loadImageFromDisk() {System.out.println("Loading " + filename);}
public void displayImage() {System.out.println("Displaying " + filename);}
}
// On System B
class ProxyImage implements Image {
private RealImage image = null;
private String filename = null;
public ProxyImage(final String filename) {
this.filename = filename;
}
public void displayImage() {
if (image == null) {
image = new RealImage(filename);
}
image.displayImage();
}
}
class ProxyExample {
public static void main(final String[] arguments) {
final Image image1 = new ProxyImage("HiRes_10MB_Photo1");
final Image image2 = new ProxyImage("HiRes_10MB_Photo2");
image1.displayImage(); // loading necessary
image1.displayImage(); // loading unnecessary
image2.displayImage(); // loading necessary
image2.displayImage(); // loading unnecessary
image1.displayImage(); // loading unnecessary
}
}
Proxy Pattern - Example
48. Decorator Pattern
Intent:
- A wrapper that adds functionality to a
class. It is stackable.
Application:
- The decorator pattern is often useful
for adhering to the “Single
Responsibility Principle”.
- It allows functionality to be divided
between classes with unique areas
of concern.
49. Decorator Pattern
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {System.out.println("Shape: Rectangle");}
}
public class Circle implements Shape {
@Override
public void draw() {System.out.println("Shape: Circle");}
}
public abstract class ShapeDecorator implements Shape {
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape){
this.decoratedShape = decoratedShape;
}
public void draw(){decoratedShape.draw();}
}
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}
@Override
public void draw() {
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape){
System.out.println("Border Color: Red");
}
public class DecoratorPatternDemo {
public static void main(String[] args) {
Shape circle = new Circle();
Shape redCircle = new RedShapeDecorator(new Circle());
Shape redRectangle = new RedShapeDecorator(new Rectangle());
System.out.println("Circle with normal border");
circle.draw();
System.out.println("nCircle of red border");
redCircle.draw();
System.out.println("nRectangle of red border");
redRectangle.draw();
}
}
51. Intent:
- Provide a unified interface to a set of
interfaces in a subsystem.
- Defines a higher-level interface that
makes the subsystem easier to use.
Application:
- A simple interface is required to
access a complex system.
- An entry point is needed to each
level of layered software.
Façade Pattern
52. Façade Pattern
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {System.out.println("Rectangle::draw()");}
}
public class Square implements Shape {
@Override
public void draw() {System.out.println("Square::draw()");}
}
public class Circle implements Shape {
@Override
public void draw() {System.out.println("Circle::draw()");}
}
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){circle.draw();}
public void drawRectangle(){rectangle.draw();}
public void drawSquare(){square.draw();}
}
public class FacadePatternDemo {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
Generic:
Example:
54. Intent:
- Allows the interface of an existing
class to be used as another interface
- Converts one interface to another so
that it matches what the client is
expecting
Application:
- Example: an adapter that converts the
interface of a Document Object Model
of an XML document into a tree
structure that can be displayed.
Adapter Pattern (Wrapper)
55. public interface MediaPlayer {
public void play(String audioType, String fileName);
}
public interface AdvancedMediaPlayer {
public void playVlc(String fileName);
public void playMp4(String fileName);
}
public class VlcPlayer implements AdvancedMediaPlayer{
@Override
public void playVlc(String fileName) {System.out.println("Playing vlc file: "+
fileName);}
@Override
public void playMp4(String fileName) {// do nothing}
}
public class Mp4Player implements AdvancedMediaPlayer{
@Override
public void playVlc(String fileName) {// do nothing}
@Override
public void playMp4(String fileName) {System.out.println("Playing mp4 file: "+
fileName);}
}
Example:
Adapter Pattern
56. public class MediaAdapter implements MediaPlayer {
AdvancedMediaPlayer advancedMusicPlayer;
public MediaAdapter(String audioType) {
If (audioType.equalsIgnoreCase("vlc") ) {
advancedMusicPlayer = new VlcPlayer();
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer = new Mp4Player();
}
}
@Override
public void play(String audioType, String fileName) {
if(audioType.equalsIgnoreCase("vlc")){
advancedMusicPlayer.playVlc(fileName);
} else if(audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer.playMp4(fileName);
}
}
}
public class AudioPlayer implements MediaPlayer {
MediaAdapter mediaAdapter;
@Override
public void play(String audioType, String fileName) {
// inbuilt support to play mp3 music files
if(audioType.equalsIgnoreCase("mp3")) {
System.out.println("Playing mp3 file. Name: " + fileName);
} else if (audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")) {
// mediaAdapter is providing support to play other file formats
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
} else {
System.out.println("Invalid media. " + audioType + " format not supported");
}
Example:
Adapter Pattern
public class AdapterPatternDemo {
public static void main(String[] args) {
AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp3", "beyond the horizon.mp3");
audioPlayer.play("mp4", "alone.mp4");
audioPlayer.play("vlc", "far far away.vlc");
audioPlayer.play("avi", "mind me.avi");
}
}
58. - Encapsulate what varies, OCP (Open Close Principle).
- 1 class, 1 responsability.
- Program against an interface, not an implementation, DIP.
- Prefer composition, over inheritance.
- Loose coupling, modular black boxes.
Golden OO-principles
59. Approach:
- Understand your design context
- Examine the patterns catalogue
- Identify and study related pattern
- Apply suitable pattern
Pitfalls:
- Selectins wrong patterns
- Inappropriate use of patterns
How to choose a Pattern ?