Skip to content

Commit

Permalink
Merge pull request eugenp#10915 from aang13/master
Browse files Browse the repository at this point in the history
BAEL-4994 code added for mvc and mvp architecture
  • Loading branch information
JonCook authored Aug 1, 2021
2 parents b80ed90 + c5bada9 commit 97e0f58
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.mvc_mvp.mvc;

public class MvcMainClass {

public static void main(String[] args) {

Product model = retrieveProductFromDatabase();
ProductView view = new ProductView();
model.setView(view);
model.showProduct();

ProductController controller = new ProductController(model);
controller.setName("SmartPhone");
model.showProduct();
}

private static Product retrieveProductFromDatabase() {
Product product = new Product();
product.setName("Mobile");
product.setDescription("New Brand");
product.setPrice(1000.0);
return product;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.baeldung.mvc_mvp.mvc;

public class Product {
private String name;
private String description;
private Double price;
private ProductView view;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

public ProductView getView() {
return view;
}

public void setView(ProductView view) {
this.view = view;
}

public void showProduct() {
view.printProductDetails(name, description, price);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.mvc_mvp.mvc;

public class ProductController {
private final Product product;

public ProductController(Product product) {
this.product = product;
}

public String getName() {
return product.getName();
}

public void setName(String name) {
product.setName(name);
}

public String getDescription() {
return product.getDescription();
}

public void setDescription(String description) {
product.setDescription(description);
}

public Double getPrice() {
return product.getPrice();
}

public void setPrice(Double price) {
product.setPrice(price);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.mvc_mvp.mvc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProductView {
private static Logger log = LoggerFactory.getLogger(ProductView.class);

public void printProductDetails(String name, String description, Double price) {
log.info("Product details:");
log.info("product Name: " + name);
log.info("product Description: " + description);
log.info("product price: " + price);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.baeldung.mvc_mvp.mvp;

public class MvpMainClass {

public static void main(String[] args) {

Product model = retrieveProductFromDatabase();
ProductView view = new ProductView();
ProductPresenter presenter = new ProductPresenter(model, view);
presenter.showProduct();
presenter.setName("SmartPhone");
presenter.showProduct();
}

private static Product retrieveProductFromDatabase() {
Product product = new Product();
product.setName("Mobile");
product.setDescription("New Brand");
product.setPrice(1000.0);
return product;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baeldung.mvc_mvp.mvp;

public class Product {
private String name;
private String description;
private Double price;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.baeldung.mvc_mvp.mvp;

public class ProductPresenter {
private final Product product;
private final ProductView view;

public ProductPresenter(Product product, ProductView view) {
this.product = product;
this.view = view;
}

public String getName() {
return product.getName();
}

public void setName(String name) {
product.setName(name);
}

public String getDescription() {
return product.getDescription();
}

public void setDescription(String description) {
product.setDescription(description);
}

public Double getProductPrice() {
return product.getPrice();
}

public void setPrice(Double price) {
product.setPrice(price);
}

public void showProduct() {
view.printProductDetails(product.getName(), product.getDescription(), product.getPrice());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.mvc_mvp.mvp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProductView {
private static Logger log = LoggerFactory.getLogger(ProductView.class);

public void printProductDetails(String name, String description, Double price) {
log.info("Product details:");
log.info("product Name: " + name);
log.info("product Description: " + description);
log.info("product price: " + price);
}
}

0 comments on commit 97e0f58

Please sign in to comment.