first issue
This commit is contained in:
parent
27e7184dd1
commit
4bff372ad3
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
|
|
@ -1,16 +1,11 @@
|
||||||
# ---> Java
|
# Java
|
||||||
# Compiled class file
|
target/
|
||||||
|
bin/
|
||||||
*.class
|
*.class
|
||||||
|
|
||||||
# Log file
|
# Log file
|
||||||
*.log
|
*.log
|
||||||
|
|
||||||
# BlueJ files
|
|
||||||
*.ctxt
|
|
||||||
|
|
||||||
# Mobile Tools for Java (J2ME)
|
|
||||||
.mtj.tmp/
|
|
||||||
|
|
||||||
# Package Files #
|
# Package Files #
|
||||||
*.jar
|
*.jar
|
||||||
*.war
|
*.war
|
||||||
|
|
@ -20,7 +15,5 @@
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
*.rar
|
*.rar
|
||||||
|
|
||||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
# do NOT ignores
|
||||||
hs_err_pid*
|
!lib/*
|
||||||
replay_pid*
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>exampleObserverPattern</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding/<project>=UTF-8
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=17
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||||
|
org.eclipse.jdt.core.compiler.release=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.source=17
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package mmk.patterns.oberver;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class Amazon {
|
||||||
|
|
||||||
|
private HashMap<Observer, Product> observers = new HashMap<Observer, Product>();
|
||||||
|
|
||||||
|
public void register(Observer observer, Product product) {
|
||||||
|
observers.put(observer, product);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unRegister(Observer observer) {
|
||||||
|
observers.remove(observer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void notifyAllObservers() {
|
||||||
|
observers.forEach((k, v) -> k.stockUpdate(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void notifyForProductName(String productName) {
|
||||||
|
observers.forEach((k, v) -> {
|
||||||
|
if(v.getName().equals(productName))
|
||||||
|
k.stockUpdate(v);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package mmk.patterns.oberver;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
var amazon = new Amazon();
|
||||||
|
var samsung = new Product("Samsung S23", 1000);
|
||||||
|
var apple = new Product("Apple 14", 2000);
|
||||||
|
var aliObserver = new UserObserver("Ali");
|
||||||
|
var zeynepObserver = new UserObserver("Zeynep");
|
||||||
|
|
||||||
|
amazon.register(aliObserver, samsung);
|
||||||
|
amazon.register(zeynepObserver, apple);
|
||||||
|
|
||||||
|
amazon.notifyForProductName("Apple 14");
|
||||||
|
|
||||||
|
System.out.println("------------------------------");
|
||||||
|
|
||||||
|
amazon.notifyAllObservers();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
package mmk.patterns.oberver;
|
||||||
|
|
||||||
|
public interface Observer {
|
||||||
|
void stockUpdate(Product product);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package mmk.patterns.oberver;
|
||||||
|
|
||||||
|
public class Product {
|
||||||
|
private String name;
|
||||||
|
private double price;
|
||||||
|
|
||||||
|
public Product(String name, double price) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
package mmk.patterns.oberver;
|
||||||
|
|
||||||
|
public class UserObserver implements Observer{
|
||||||
|
|
||||||
|
private String fullName;
|
||||||
|
|
||||||
|
public UserObserver(String fullName) {
|
||||||
|
super();
|
||||||
|
this.fullName = fullName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stockUpdate(Product product) {
|
||||||
|
System.out.printf("Dear %s, Product called %s has been in stock anymore!\n", fullName, product.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFullName() {
|
||||||
|
return fullName;
|
||||||
|
}
|
||||||
|
public void setFullName(String fullName) {
|
||||||
|
this.fullName = fullName;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue