DesignPatterns | The 23 Gang of Four Design Patterns | Architecture library
kandi X-RAY | DesignPatterns Summary
kandi X-RAY | DesignPatterns Summary
The 23 Gang of Four design patterns, defined in the book "Design Patterns: Elements of Reusable Object-Oriented Software".
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of DesignPatterns
DesignPatterns Key Features
DesignPatterns Examples and Code Snippets
Community Discussions
Trending Discussions on DesignPatterns
QUESTION
The Gang of Four book states the following:
[...] The main relationships in MVC are given by the Observer, Composite and Strategy design patterns.
The best example of this I could find was this github repo, which shows the following class diagram:
I'm particularly interested in the Strategy part of the MVC, where the following holds:
MVC Name GoF Name View Context Controller Strategy ConcreteController ConcreteStrategyRegarding the relationship between View and Controller, The book Head First Design Patterns states the following:
The controller may also ask the view to change. When the controller receives an action from the view, it may need to tell the view to change as a result. For example, the controller could enable or disable certain buttons or menu items in the interface
It also shows an example where the ConcreteController
equivalent BeatController
) defines an additional association with the ConcreteView
equivalent (DJView
):
ANSWER
Answered 2022-Feb-15 at 15:40To address one part of your question: the relationship between Context
and Strategy
in the Strategy pattern. The UML diagram you see in books etc. is just a sample implementation, and this usually shows just the link from Context
and Strategy
. However the Strategy
has to know something about its Context
to do its job and this is usually passed as arguments in the method invocation on the Strategy
object. When there are many arguments to pass, they may be replaced by passing a reference to the Context
object to the Strategy
constructor. This decision is an example of 'tailoring a pattern to fit its context'.
QUESTION
In the Head First Design Patterns book, the authors describe using an iterator to traverse over composite data structures. They provide some sample code which, when executed, prints out a series of menu items stored within the composite. However, if you try to call the iterator more than once, it no longer works as expected and won't produce any results. The following code appears to be causing the problem:
...ANSWER
Answered 2021-Mar-18 at 13:51As the linked issue says in the comments:
QUESTION
I am learning design pattern in python and the subject is singleton objects so, I was writing my main code as PRO003
and import it into PRO004
. This is PRO003
Code:
ANSWER
Answered 2021-Jan-29 at 08:07Class __SingletonObject
is not be instantiated
QUESTION
I just want a quick lookover that I implemented the different fly strategies correctly.
The program simply consists of a duck class that uses an interface for its fly method. The interface has different implementations (namely SimpleFly and NoFly), and a switch statement chooses the correct method based on the specie enum.
As I understand, the strategy pattern is meant to avoid duplicate code between child classes at the same level, which decreases maintainability and extensibility. So instead we abstract out the related algorithms to interfaces and choose them as needed.
CODE:
...ANSWER
Answered 2020-Dec-18 at 20:48I would point out a couple issues of semantics and terminology.
- It's confusing to have a method named
fly
that can be implemented as not flying. Naming the methodtryToFly
or documenting the method as merely an attempt are two ways of addressing this confusion. The software principle to reference here is Liskov Substitution. - The base class does not implement one of the strategies; rather, it composes a strategy. The purpose of the Strategy pattern is to avoid subclassing through composition.
- To reiterate one of the comments,
Duck
should accept an instance ofIFly
directly in its constructor (or a setter method) rather than switching on an enum. Another goal of the Strategy pattern is to avoid branching logic.
The essence of the pattern is that you've avoided creating multiple subclasses of Duck
by instead creating multiple implementations of IFly
. This has the advantage that those IFly
implementations can be reused without a complex inheritance hierarchy, e.g. WILD
and CITY
can share one strategy.
As mentioned in the comments, strategies also have the advantage that a Duck
could change its strategy at runtime. For example, IFly
might be implemented by Soar
and Glide
so that a Duck
would switch between these different strategies depending on the wind.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install DesignPatterns
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page