Compare commits
No commits in common. 'master' and 'main' have entirely different histories.
@ -1,18 +0,0 @@
|
||||
public abstract class Animal {
|
||||
protected String name;
|
||||
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// 进阶题要求:抽象方法 makeSound
|
||||
public abstract void makeSound();
|
||||
|
||||
public void eat() {
|
||||
System.out.println(name + " is eating.");
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
public class Bird extends Animal {
|
||||
public Bird(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeSound() {
|
||||
System.out.println(getName() + " says: Chirp!");
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
public class Cat extends Animal {
|
||||
public Cat(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeSound() {
|
||||
System.out.println(getName() + " says: Meow!");
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
public class Dog extends Animal {
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeSound() {
|
||||
System.out.println(getName() + " says: Woof!");
|
||||
}
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Animal dog = new Dog("Buddy");
|
||||
Animal cat = new Cat("Kitty");
|
||||
Animal bird = new Bird("Tweety");
|
||||
|
||||
dog.makeSound();
|
||||
cat.makeSound();
|
||||
bird.makeSound();
|
||||
}
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
public class Animal {
|
||||
protected String name;
|
||||
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void eat() {
|
||||
System.out.println(name + " is eating.");
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
public class Fish extends Animal {
|
||||
private double swimSpeed; // m/s
|
||||
|
||||
public Fish(String name, double swimSpeed) {
|
||||
super(name);
|
||||
this.swimSpeed = swimSpeed;
|
||||
}
|
||||
|
||||
public void swim() {
|
||||
System.out.println(getName() + " is swimming at " + swimSpeed + " m/s.");
|
||||
}
|
||||
|
||||
public double getSwimSpeed() {
|
||||
return swimSpeed;
|
||||
}
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Fish nemo = new Fish("Nemo", 1.5);
|
||||
nemo.eat();
|
||||
nemo.swim();
|
||||
}
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
public class Animal {
|
||||
protected String name;
|
||||
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void eat() {
|
||||
System.out.println(name + " is eating.");
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
public class Bird extends Animal {
|
||||
public Bird(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
// Bird 不实现 Pet
|
||||
public void fly() {
|
||||
System.out.println(getName() + " is flying.");
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
public class Cat extends Animal implements Pet {
|
||||
public Cat(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void play() {
|
||||
System.out.println(getName() + " plays with a ball of yarn.");
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
public class Dog extends Animal implements Pet {
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void play() {
|
||||
System.out.println(getName() + " plays fetch.");
|
||||
}
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
public interface Pet {
|
||||
void play();
|
||||
}
|
||||
@ -1,6 +0,0 @@
|
||||
public class ConcreteProductA implements Product {
|
||||
@Override
|
||||
public void operation() {
|
||||
System.out.println("ConcreteProductA operation");
|
||||
}
|
||||
}
|
||||
@ -1,6 +0,0 @@
|
||||
public class ConcreteProductB implements Product {
|
||||
@Override
|
||||
public void operation() {
|
||||
System.out.println("ConcreteProductB operation");
|
||||
}
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
public class Factory {
|
||||
public static Product createProduct(String type) {
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
if (type.equalsIgnoreCase("A")) {
|
||||
return new ConcreteProductA();
|
||||
} else if (type.equalsIgnoreCase("B")) {
|
||||
return new ConcreteProductB();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
public interface Product {
|
||||
void operation();
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
/**
|
||||
* 简单的 Map 接口示例
|
||||
*/
|
||||
public interface MapInterface<K,V> {
|
||||
void put(K key, V value);
|
||||
V get(K key);
|
||||
int size();
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
/**
|
||||
* 简单的 Set 接口示例
|
||||
*/
|
||||
public interface SetInterface<E> {
|
||||
void add(E e);
|
||||
boolean contains(E e);
|
||||
int size();
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SimpleHashMap<K,V> implements MapInterface<K,V> {
|
||||
private final Map<K,V> delegate = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void put(K key, V value) {
|
||||
delegate.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(K key) {
|
||||
return delegate.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return delegate.size();
|
||||
}
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class SimpleHashSet<E> implements SetInterface<E> {
|
||||
private final Set<E> delegate = new HashSet<>();
|
||||
|
||||
@Override
|
||||
public void add(E e) {
|
||||
delegate.add(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(E e) {
|
||||
return delegate.contains(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return delegate.size();
|
||||
}
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
/**
|
||||
* 信用卡支付实现
|
||||
*/
|
||||
public class CreditCardPayment implements PaymentProcessor {
|
||||
@Override
|
||||
public boolean process(double amount) {
|
||||
System.out.println("Processing credit card payment: " + amount);
|
||||
// 模拟处理逻辑
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
/**
|
||||
* PayPal 支付实现
|
||||
*/
|
||||
public class PayPalPayment implements PaymentProcessor {
|
||||
@Override
|
||||
public boolean process(double amount) {
|
||||
System.out.println("Processing PayPal payment: " + amount);
|
||||
// 模拟处理逻辑
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
# Simple Factory Pattern in Java
|
||||
|
||||
This project demonstrates the Simple Factory design pattern in Java. It includes an abstract product interface, concrete product implementations, and a factory class to create product instances based on specified types.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
simple-factory-java
|
||||
├── src
|
||||
│ └── main
|
||||
│ └── java
|
||||
│ ├── Product.java
|
||||
│ ├── ConcreteProductA.java
|
||||
│ ├── ConcreteProductB.java
|
||||
│ └── Factory.java
|
||||
├── pom.xml
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## Files Description
|
||||
|
||||
- **Product.java**: Defines the `Product` interface with basic methods that all products must implement, such as `use()`.
|
||||
|
||||
- **ConcreteProductA.java**: Implements the `Product` interface, defining the behavior of Concrete Product A and overriding the `use()` method.
|
||||
|
||||
- **ConcreteProductB.java**: Implements the `Product` interface, defining the behavior of Concrete Product B and overriding the `use()` method.
|
||||
|
||||
- **Factory.java**: Defines the `Factory` class, which contains a static method `createProduct(String type)` that returns the appropriate product instance based on the provided type.
|
||||
|
||||
- **pom.xml**: Maven configuration file that defines the project's dependencies and build settings.
|
||||
|
||||
## Usage
|
||||
|
||||
To use this project, you can call the `Factory.createProduct(String type)` method with either "A" or "B" to get an instance of `ConcreteProductA` or `ConcreteProductB`, respectively. Then, you can invoke the `use()` method on the product instance to see its behavior.
|
||||
|
||||
## Build and Run
|
||||
|
||||
1. Ensure you have Maven installed on your machine.
|
||||
2. Navigate to the project directory.
|
||||
3. Run `mvn clean install` to build the project.
|
||||
4. Use the generated classes in your Java application as needed.
|
||||
@ -1,32 +0,0 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>simple-factory-java</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Add any dependencies here if needed -->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@ -1,6 +0,0 @@
|
||||
public class ConcreteProductA implements Product {
|
||||
@Override
|
||||
public void use() {
|
||||
System.out.println("Using Concrete Product A");
|
||||
}
|
||||
}
|
||||
@ -1,6 +0,0 @@
|
||||
public class ConcreteProductB implements Product {
|
||||
@Override
|
||||
public void use() {
|
||||
System.out.println("Using Concrete Product B");
|
||||
}
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
public class Factory {
|
||||
public static Product createProduct(String type) {
|
||||
if ("A".equalsIgnoreCase(type)) {
|
||||
return new ConcreteProductA();
|
||||
} else if ("B".equalsIgnoreCase(type)) {
|
||||
return new ConcreteProductB();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
public interface Product {
|
||||
void use();
|
||||
}
|
||||
Loading…
Reference in new issue