02.创建型设计模式实验

写在前面

在网上抄个作业居然要不没有,要不要钱!老子有个屁的钱,气得我直接写完了,分享在这里供借鉴(抄可以,但你得会了再抄)。随便转载,可以不署我名但需要声明非原创。
这是一系列的作业,所以也气了我一系列,不想后来人受气了。
本作业对《图解设计模式》这本书有不小内容的借鉴,在此感谢并推荐。

创建型设计模式实验

1、简单工厂

题目:使用简单工厂模式设计一个可以创建不同几何形状( Shape)(例如圆形( Circle).、矩形 Rectangle)和三角形( ( Triangle)等的绘图工具类,每个几何图形均具有绘制方法daw()和擦除方法 erase(),要求在绘制不支持的几何图形时,抛出一个 UnsupportedShapeException异常。绘制类图并编程模拟实现。

类图

在这里插入图片描述

编程实现
package TemplateMethod;

public abstract class AbstractDraw {
   
    public void draw(){
   
        throw new UnsupportedShapedException("绘制了不能绘制的图形");
    }
    public abstract void erase();
}


package TemplateMethod;

public class Circle extends AbstractDraw{
   

    @Override
    public void erase() {
   
        System.out.println("圆形已经被清除");
    }
}


package TemplateMethod;

public class Rectangle extends AbstractDraw{
   
    @Override
    public void draw() {
   
        System.out.println("三角形已经绘制好了");
    }

    @Override
    public void erase() {
   
        System.out.println("矩形已经被清除");
    }
}


package TemplateMethod;

public class Triangle extends AbstractDraw{
   
    @Override
    public void draw() {
   
        System.out.println("矩形已经绘制好了");
    }

    @Override
    public void erase() {
   
        System.out.println("三角形已经被清除");
    }
}


package TemplateMethod;

public class UnsupportedShapedException extends RuntimeException{
   
    public UnsupportedShapedException(){
   

    }

    public UnsupportedShapedException(String strings){
   
        super(strings);
    }
}


package TemplateMethod;

public class Main{
   
    public static void main(String[] args){
   
        AbstractDraw c = new Circle();
        AbstractDraw r = new Rectangle();
        AbstractDraw t = new Triangle();

        r.draw();
        t.draw();
        t.erase();
        c.draw();
    }
}


运行结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ohxBORPd-1635506203748)(02.创建型设计模式实验/image-20211024190816353.png)]

2、工厂方法

题目:在某网络管理软件中,需要为不同的网络协议提供不同的连接类,例如针对POP3协议的连接类POP3Connection、针对IMAP协议的连接类 IMAPConnection、针对HTTP协议的连接类 HTTPConnection等。由于网络连接对象的创建过程较为复杂,需要将其创建过程封装到专门的类中,该软件还将支持更多类型的网络协议。现采用工厂方法模式进行设计,绘制类图并编程模拟实现。

类图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qBTw0f3H-1635506203749)(02.创建型设计模式实验/image-20211024204001215.png)]

编程实现
package FactoryMethod.Connection;

import FactoryMethod.FrameWork.Product;

public class HTTPConnection extends Product {
   
    private String owner;
    HTTPConnection(String owner){
   
        this.owner = owner;
        System.out.println("为用户" + owner + "创建了HTTPConnection连接类");
    }
    @Override
    public void use() {
   
        System.out.println("用户" + owner + "在使用HTTPConnection连接类");
    }

    public String  getOwner() {
   
        return owner;
    }
}


package FactoryMethod.Connection;

import FactoryMethod.FrameWork.Factory;
import FactoryMethod.FrameWork.Product;

import java.util.ArrayList;
import java.util.List;

public class HTTPFactory extends Factory {
   
    private List<String> owners = new ArrayList();

    @Override
    protected Product createProduct(String owner) {
   
        return new HTTPConnection(owner);
    }

    @Override
    protected void registerProduct(Product product) {
   
        owners.add(((HTTPConnection)product).getOwner());
    }

    public List getOwners(){
   
        return owners;
    }
}


package FactoryMethod.Connection;

import FactoryMethod.FrameWork.Product;

public class IMAPConnection extends Product {
   
    private String owner;
    IMAPConnection(String owner){
   
        this.owner = owner;
        System.out.println("为用户" + owner + "创建了IMAPConnection连接类");
    }
    @Override
    public void use() {
   
        System.out.println("用户" + owner + "在使用IMAPConnection连接类");
    }

    public String  getOwner() {
   
        return owner;
    }
}


package FactoryMethod.Connection;

import FactoryMethod.FrameWork.Factory;
import FactoryMethod.FrameWork.Product;

import java.util.ArrayList;
import java.util.List;

  • 34
    点赞
  • 93
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
很抱歉,作为人工智能,我无法编写和运行Java代码。但是,我可以提供一些关于创建设计模式的解释和示例,帮助你更好地理解这些概念。 1. 简单工厂模式 简单工厂模式是一种通过一个工厂类来创建不同对象的设计模式。在这种模式中,客户端只需要知道所需产品的名称或类,而不需要知道具体的实现细节。 示例代码: ``` public class AnimalFactory { public static Animal createAnimal(String type) { if (type.equals("dog")) { return new Dog(); } else if (type.equals("cat")) { return new Cat(); } else { return null; } } } public interface Animal { public void makeSound(); } public class Dog implements Animal { public void makeSound() { System.out.println("Woof!"); } } public class Cat implements Animal { public void makeSound() { System.out.println("Meow!"); } } public class Main { public static void main(String[] args) { Animal animal1 = AnimalFactory.createAnimal("dog"); Animal animal2 = AnimalFactory.createAnimal("cat"); animal1.makeSound(); // Output: Woof! animal2.makeSound(); // Output: Meow! } } ``` 2. 工厂方法模式 工厂方法模式是一种将对象的创建委托给子类的设计模式。在这种模式中,客户端只需要知道所需产品的抽象类,而具体的实现由子类来决定。 示例代码: ``` public interface Animal { public void makeSound(); } public class Dog implements Animal { public void makeSound() { System.out.println("Woof!"); } } public class Cat implements Animal { public void makeSound() { System.out.println("Meow!"); } } public abstract class AnimalFactory { public abstract Animal createAnimal(); } public class DogFactory extends AnimalFactory { public Animal createAnimal() { return new Dog(); } } public class CatFactory extends AnimalFactory { public Animal createAnimal() { return new Cat(); } } public class Main { public static void main(String[] args) { AnimalFactory factory1 = new DogFactory(); Animal animal1 = factory1.createAnimal(); AnimalFactory factory2 = new CatFactory(); Animal animal2 = factory2.createAnimal(); animal1.makeSound(); // Output: Woof! animal2.makeSound(); // Output: Meow! } } ``` 3. 抽象工厂模式 抽象工厂模式是一种提供一个接口来创建一系列相关或相互依赖对象的设计模式。在这种模式中,客户端通过调用工厂接口的方法来创建不同的对象。 示例代码: ``` public interface Animal { public void makeSound(); } public class Dog implements Animal { public void makeSound() { System.out.println("Woof!"); } } public class Cat implements Animal { public void makeSound() { System.out.println("Meow!"); } } public interface AnimalFactory { public Animal createAnimal(); } public class DomesticAnimalFactory implements AnimalFactory { public Animal createAnimal() { return new Dog(); } } public class WildAnimalFactory implements AnimalFactory { public Animal createAnimal() { return new Cat(); } } public class Main { public static void main(String[] args) { AnimalFactory factory1 = new DomesticAnimalFactory(); Animal animal1 = factory1.createAnimal(); AnimalFactory factory2 = new WildAnimalFactory(); Animal animal2 = factory2.createAnimal(); animal1.makeSound(); // Output: Woof! animal2.makeSound(); // Output: Meow! } } ``` 4. 单例模式 单例模式是一种保证一个类只有一个实例,并提供全局访问点的设计模式。在这种模式中,构造函数是私有的,只能通过静态方法来获取实例。 示例代码: ``` public class Singleton { private static Singleton instance = null; private Singleton() {} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } public class Main { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2); // Output: true } } ``` 5. 原模式 原模式是一种通过复制现有对象来创建新对象的设计模式。在这种模式中,通过调用对象的克隆方法来创建新对象。 示例代码: ``` public class Sheep implements Cloneable { private String name; public Sheep(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } public Sheep clone() { try { return (Sheep) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; } } } public class Main { public static void main(String[] args) { Sheep sheep1 = new Sheep("Dolly"); Sheep sheep2 = sheep1.clone(); sheep2.setName("Molly"); System.out.println(sheep1.getName()); // Output: Dolly System.out.println(sheep2.getName()); // Output: Molly } } ``` 6. 建造者模式 建造者模式是一种将复杂对象的构建过程分离出来的设计模式。在这种模式中,通过调用建造者类的方法来构建对象,可以灵活地定制对象的组成部分。 示例代码: ``` public class Computer { private String cpu; private String gpu; private String memory; private String storage; public Computer(String cpu, String gpu, String memory, String storage) { this.cpu = cpu; this.gpu = gpu; this.memory = memory; this.storage = storage; } public String getCpu() { return cpu; } public String getGpu() { return gpu; } public String getMemory() { return memory; } public String getStorage() { return storage; } } public interface ComputerBuilder { public void setCpu(String cpu); public void setGpu(String gpu); public void setMemory(String memory); public void setStorage(String storage); public Computer build(); } public class GamingComputerBuilder implements ComputerBuilder { private String cpu; private String gpu; private String memory; private String storage; public void setCpu(String cpu) { this.cpu = cpu; } public void setGpu(String gpu) { this.gpu = gpu; } public void setMemory(String memory) { this.memory = memory; } public void setStorage(String storage) { this.storage = storage; } public Computer build() { return new Computer(cpu, gpu, memory, storage); } } public class OfficeComputerBuilder implements ComputerBuilder { private String cpu; private String memory; private String storage; public void setCpu(String cpu) { this.cpu = cpu; } public void setGpu(String gpu) {} public void setMemory(String memory) { this.memory = memory; } public void setStorage(String storage) { this.storage = storage; } public Computer build() { return new Computer(cpu, null, memory, storage); } } public class ComputerDirector { private ComputerBuilder builder; public void setBuilder(ComputerBuilder builder) { this.builder = builder; } public Computer buildComputer() { builder.setCpu("Intel i7"); builder.setMemory("16GB"); builder.setStorage("512GB SSD"); return builder.build(); } } public class Main { public static void main(String[] args) { ComputerDirector director = new ComputerDirector(); ComputerBuilder builder1 = new GamingComputerBuilder(); ComputerBuilder builder2 = new OfficeComputerBuilder(); director.setBuilder(builder1); Computer gamingComputer = director.buildComputer(); director.setBuilder(builder2); Computer officeComputer = director.buildComputer(); System.out.println(gamingComputer.getCpu()); // Output: Intel i7 System.out.println(gamingComputer.getGpu()); // Output: NVIDIA GTX System.out.println(officeComputer.getCpu()); // Output: Intel i7 System.out.println(officeComputer.getGpu()); // Output: null } } ``` 以上是关于六种创建设计模式的简要介绍和示例代码。希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值