design-pattern-java | 设计模式 Java版本 | Architecture library
kandi X-RAY | design-pattern-java Summary
kandi X-RAY | design-pattern-java Summary
设计模式 Java版本
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 design-pattern-java
design-pattern-java Key Features
design-pattern-java Examples and Code Snippets
Community Discussions
Trending Discussions on design-pattern-java
QUESTION
I read this link enter link description here, to learn State Desing Patern.
interface class:
...ANSWER
Answered 2019-Jan-01 at 11:57- Why
TVContext
class implementsState
and has composition together?
The example is incorrect, TVContext
should not implement interface State
. From the UML diagram for State Design Pattern we can see that class Context
only
compose an attribute that implements interface State
.
- If the final programmer in this TestClass pass context to
context.setState()
instead tvStartState or tvStopState , program successfully compiles but errors in run_time.
The reason it compiles is because context is implementing interface State
, but it fails in run-time with a java.lang.StackOverflowError
because function context.setState()
is recursively invoking itself with no exit condition. Removing the interface State
from TVContext
class fix this issue.
In Decorator Design Pattern the intent is to add new behavior to the Component class. That is why inheritance is used to add new methods to the Component.
In State Design Pattern the intent is to change the behavior of the Context class. For example if we implement it using inheritance with an abstract class instead of the interface the operation on the concrete state classes need to override the operation defined in the abstract class. That is why an interface makes more sense in this case.
QUESTION
I am trying to read design patterns and currently going thru Bridge Pattern.
It states that
Decouple the functional abstraction from the implementation so that the two can vary independently
I was going thru this example on this link : https://www.journaldev.com/1491/bridge-design-pattern-java
Could somebody explain me how this example to this bold statement?
Thanks a lot.
...ANSWER
Answered 2019-Mar-11 at 13:10Bridge
is splitting the interface and implementation in multiple parts. In your example you'll get 2 different interfaces Shape
, Color
. They will generate their own class hierarchies and because they are independent they can both vary.
You'll end up with multiple shapes and multiple colors that can be combined at runtime. This is achieved using composition
instead of inheritance
. Each instance of a Shape
needs an instance of a Color
when it gets created and that's the way you get a red triangle or a green pentagon or any other combination of a Shape
and a Color
.
The hierarchies aren't tightly coupled and they only communicate at an interface level.
QUESTION
For example is this appropriate for the model:
...ANSWER
Answered 2017-Dec-15 at 19:59Short answer: Yes. It's just code.
Long answer: No, that's not how MVC is designed. MVC is specifically designed to separate the logic from the view. In fact, in .NET the data layer is normally a completely separate class library that's just referenced by the frontend. The server and the client are designed to be separate, to keep the backend and the frontend away from each other.
I know the following you probably know already, but I'd just like to reiterate and clarify: MVC stands for Model, View, Controller (obviously), but this is what each does:
Model: The Model can be thought of as your average class. It contains a structure and functions relating to a specific object, as well as the CRUD for the model (most of the time the actual model and the CRUD are also separated, but very closely linked and related). The Model is what your application uses to understand how to communicate, calculate, and construct.
View: The View is what the client sees. It is the actual web page, the actual front end. Ideally (although impractically), it contains no logic whatsoever. Most of the time there is SOME logic here (mostly JavaScript logic, sometimes server logic can be embedded here too to create a dynamic user experience). This is where your HTML, CSS, and (client side) JavaScript lives.
Controller: This is the liaison between the Model and the View. It takes a request from the client, sends it to the Model, which then returns a constructed model. The Controller then performs any necessary calculations, and passes the model to the view. The Controller essentially described the Model to the View - it explains how entities interact and relate with each other.
Think of it this way: I have a model called ApplicationUser and a CRUD class called ApplicationUserCRUD. A client logs onto my site (View Layer), which sends a request to my Controller. The Controller takes their request (in this case, let's say the request is their username and password), and passes it to the CRUD (Controller Layer). The CRUD then checks whether or not the user exists, and if the password matches (Model Layer). Then, when it's confirmed, it sends the information to the Model. The Model then returns to the Controller, which passes it to the View in order to construct the View.
If you are trying to perform logic and calculations in your Model, you're not using MVC properly. Logic and calculations are performed in the Controller, SOMETIMES the View, and in some cases a Helper class.
A Helper class is a Controller that doesn't relate to a specific Area/View.
Also, the CRUD is normally called the DAL, or Data-Access-Layer. It's yet another layer that essentially sits parallel with the Model Layer, but contains the logic for interacting with the server/database. I mention this in order to demonstrate how much people dislike adding logic to the Model - so much so that they create an entirely new layer just to hold the logic.
Hope this helped!
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install design-pattern-java
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