栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java设计模式(1)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java设计模式(1)

1.设计模式简介 1.1定义

设计模式是从许多优秀的软件系统中总结出成功的可复用的设计方案

1.2基本要素
  • 名称
  • 问题
  • 方案
  • 效果
2.面向对象的几个基本原则 2.1面向抽象原则

 设计一个类时,不让该类面向具体的类,而是面向抽象类或接口

2.1.1抽象类
  • 抽象类中可以有abstract方法,也可以有非abstract方法。
  • 抽象类不能用new运算符创建对象。
  • 如果一个非抽象类是某个抽象类的子类,那么它必须重写父类的abstract方法。
  • 作为上转型对象。让抽象类声明的对象成为其子类对象的上转型对象,并调用子类重写的方法。
2.1.2案例:抽象类A中有一个abstract方法add(int x,int y)

A.Java

public  abstract  class  A{
     public  abstract  int add(int x,int y); 
}

B.Java

class  B  extends  A{
     public  int   add(int x, int y){ 
           return x+y;}
}

Application.java

public  class  Application{
  public  static  void  main(String  args[]){
     A  a;
     a=new B();  //a是B类对象的上转型对象
     int m=a.add(3,2);//a调用子类B重写的add()方法
     System.out.println(m); //输出结果为5.
  }
}
2.1.3接口
  •  接口中只可以有public权限的abstract方法,不能有非abstract方法。
  • 接口由类去实现。一个类如果实现一个接口,它必须重写接口中的abstract方法,必须去掉abstract修饰符。
  • 接口回调。接口回调是指可以实现接口的类的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口中的方法。
案例:下列接口Com中有一个abstract方法sub(int x,int y)
//Com.java
public  interface  Com{
    public  abstract int sub(int x,int y); }

//ComImp.java
class  ComImp implements Com{
   public  int sub(int x, int y){
     return  x-y;
   }
}
//Application.java
public  class  Application{
  public  static  void  main(String args[]){
     Com  com;
     com=new ComImp(); //com变量存放ComImp类的对象引用。
     int m=com.sub(8,2);//com回调ComImp类实现的接口方法。
     System.out.println(m);//输出结果为6
   }
}
2.2面向抽象

 面向抽象编程是指当设计一个类时,不让该类面向具体的类,而是面向抽象类或接口,即所设计类中的重要数据是抽象类或接口声明的变量,而不是具体类声明的变量。

案例:设计一程序计算几何图形的体积(长方体,圆柱体或三棱柱)

//Geometry.java
public  abstract class  Geometry{
    public abstract double getArea();
}
//Pillar.java
public  class  Pillar{
    Geometry   bottom;    double   height;
    Pillar(Geometry bottom,double height){
        this.bottom=bottom; this.height=height;}
    public  double  getVolume(){
        return  bottom.getArea()*height;
    }
}
//Circle.java
public class Circle  extends  Geometry{
    double  r;
    Circle(double r){
        this.r=r;  }
    public  double  getArea(){
        return(3.14*r*r);
    }
}
//Rectangle.java
public  class  Rectangle extends Geometry{
    double  a,b;
    Rectangle(double  a, double  b){
        this.a=a;  this.b=b;
    }
    public  double  getArea(){
        return  a*b;
    }
}
//Triangle.java
public class Triangle extends Geometry{
    double m,n;
    Triangle(double m,double n){
        this.m=m;this.n=n;
    }

    @Override
    public double getArea() {
        return (m*n/2);
    }
}
//Application.java
public class Application {
    public static void main(String args[]){
        Pillar  pillar;
        Geometry  bottom;
        bottom=new  Rectangle(12,22);
        pillar=new Pillar(bottom,58);
        System.out.println("矩形底柱体体积:"+pillar.getVolume());
        bottom=new Circle(10);
        pillar=new Pillar(bottom,58);
        System.out.println("圆形底柱体体积:"+ pillar.getVolume());
        bottom=new Triangle(12,13);
        pillar=new Pillar(bottom,58);
        System.out.println("三角形底柱体体积:"+pillar.getVolume());
    }
}
2.3开-闭原则 2.4多用组合少用继承原则

方法复用的两种最常用的技术是类继承和对象组合。

2.5高内聚-低耦合原则 3.抽象工厂模式

抽象工厂模式(别名:配套)      

提供一个创建一系列(相互依赖)对象的接口,而无需指定它们具体的类。

3.1模式的结构的四种角色: 3.1.1抽象产品(Prodcut)

一个抽象类或接口,负责定义具体产品必须实现的方法。

3.1.2具体产品(ConcreteProduct)

具体产品是一个类,如果Product是一个抽象类,那么具体产品是Product的子类;如果Product是一个接口,那么具体产品是实现Product接口的类。

3.1.3抽象工厂(AbstractFactory)

一个接口或抽象类,负责定义若干个抽象方法。

3.1.4具体工厂(ConcreteFactory)

如果抽象工厂是抽象类,具体工厂是抽象工厂的子类;如果抽象工厂是接口,具体工厂是实现抽象工厂的类。具体工厂重写抽象工厂中的抽象方法,使该方法返回具体产品的实例。

3.2UML类图

3.3案例 3.3.1题目要求 

  (1)建立一个系统,该系统可以为用户提供西服套装和牛仔套装

(2)设计两个抽象类:UpperClothes类(刻画衣服上装),Trousers类(刻画衣服下装)

(3)确定抽象产品角色,具体产品角色,抽象工厂角色,具体工厂角色并编写相应程序

 3.3.2代码
1. 抽象产品 

/ * UpperClothes  上装  */
public abstract class UpperClothes extends Trousers {
    public abstract int getChestSize();
    @Override
    public int getWaistSize() {
        return 0;
    }	
    public abstract int getHeight();
    public abstract String getName();
}

public abstract class Trousers{
   public abstract int getWaistSize();
   public abstract int getHeight();
   public abstract String getName(); 
}

public abstract class Shoes {
    public abstract int getShoesSize();
    public abstract String getName();
}
2. 具体产品



public class WesternUpperClothes extends UpperClothes{
   private int chestSize;
   private int height;
   private String name;
   WesternUpperClothes(String name,int chestSize,int height){
       this.name=name;
       this.chestSize=chestSize;
       this.height=height;
   }
   public int getChestSize(){
       return chestSize;
   }
   public int getHeight(){
       return height;
   }
   public String getName(){
       return name;
   } 
}

public class CowboyUpperClothes extends UpperClothes{
   private int chestSize;
   private int height;
   private String name;
   CowboyUpperClothes(String name,int chestSize,int height){
       this.name=name;
       this.chestSize=chestSize;
       this.height=height;
   }
   public int getChestSize(){
       return chestSize;
   }
   public int getHeight(){
       return height;
   }
   public String getName(){
       return name;
   } 
}

public class WesternTrousers extends Trousers{
   private int waistSize;
   private int height;
   private String name;
   WesternTrousers(String name,int waistSize,int height){
       this.name=name;
       this.waistSize=waistSize;
       this.height=height;
   }
    public int getWaistSize(){
       return waistSize;
   }
   public int getHeight(){
       return height;
   }
   public String getName(){
       return name;
   } 
}


public class CowboyTrousers extends Trousers{  
   private int waistSize;
   private int height;
   private String name;
   CowboyTrousers(String name,int waistSize,int height){
       this.name=name;
       this.waistSize=waistSize;
       this.height=height;
   }
    public int getWaistSize(){
       return waistSize;
   }
   public int getHeight(){
       return height;
   }
   public String getName(){
       return name;
   } 
}

public class LeatherShoes extends Shoes{
    private int shoesSize;
    private String name;
    LeatherShoes(String name,int shoesSize ){
        this.name = name;
        this.shoesSize = shoesSize;
    }
    public int getShoesSize(){
        return shoesSize;
    }
    public String getName(){
        return name;
    }
}

public class Plimsolls extends Shoes{
    private int shoesSize;
    private String name;
    Plimsolls(String name,int shoesSize ){
        this.name = name;
        this.shoesSize = shoesSize;
    }
    public int getShoesSize(){
        return shoesSize;
    }
    public String getName(){
        return name;
    }
}

public class Sneaker extends Shoes{
    private int shoesSize;
    private String name;
    Sneaker(String name,int shoesSize ){
        this.name = name;
        this.shoesSize = shoesSize;
    }
    public int getShoesSize(){
        return shoesSize;
    }
    public String getName(){
        return name;
    }
}
3.抽象工厂

public abstract class ClothesFactory{
    public abstract UpperClothes createUpperClothes(int chestSize,int height);
public abstract Trousers createTrousers(int waistSize,int height);
public abstract Shoes createShoes(int ShoesSize);
}
4.具体工厂


public class BeijingClothesFactory extends ClothesFactory {
    public UpperClothes createUpperClothes(int chestSize,int height){
         return new WesternUpperClothes("北京牌西服上衣",chestSize,height);
    }
    public Trousers createTrousers(int waistSize,int height){
         return new WesternTrousers("北京牌西服裤子",waistSize,height);
}
    public Shoes createShoes(int ShoesSize){
        return new LeatherShoes("北京牌皮鞋",ShoesSize);
    }
}

public class ShanghaiClothesFactory extends ClothesFactory {
    public UpperClothes createUpperClothes(int chestSize,int height){
         return new WesternUpperClothes("上海牌牛仔上衣",chestSize,height);
    }
    public Trousers createTrousers(int waistSize,int height){
         return new WesternTrousers("上海牌牛仔裤",waistSize,height);
}
 public Shoes createShoes(int ShoesSize){
        return new Plimsolls("上海牌帆布鞋",ShoesSize);
    }
}

public class ShenyangClothesFacyory extends ClothesFactory {
    public UpperClothes createUpperClothes(int chestSize,int height){
        return new WesternUpperClothes("沈阳牌休闲上衣",chestSize,height);
    }
    public Trousers createTrousers(int waistSize,int height){
        return new WesternTrousers("沈阳牌休闲裤",waistSize,height);
}
 public Shoes createShoes(int ShoesSize){
        return new Sneaker("沈阳牌运动鞋",ShoesSize);
    }
}

5.应用_1: Shop.java //涉及抽象产品,抽象工厂为用户提供套装
public class Shop{
    UpperClothes cloth;
    Trousers trouser;
    Shoes shoes;
    public void giveSuit(ClothesFactory factory,int chestSize,int waistSize,int height,int shoesSize){
        cloth=factory.createUpperClothes(chestSize,height);
        trouser=factory.createTrousers(waistSize,height);
        shoes = factory.createShoes(shoesSize);
        showMess();
    }
    private void showMess(){
        System.out.println("<套装信息>");
        System.out.println(cloth.getName()+":");
        System.out.print("胸围:"+cloth.getChestSize());
        System.out.println("身高:"+cloth.getHeight());
        System.out.println(trouser.getName()+":");
        System.out.print("腰围:"+trouser.getWaistSize());
        System.out.println("身高:"+trouser.getHeight());
        System.out.println(shoes.getName()+":");
        System.out.print("鞋码:"+shoes.getShoesSize());
    }
}

public class Application{
    public static void main(String args[]){
       Shop shop=new Shop();
       ClothesFactory factory=new BeijingClothesFactory(); 
       shop.giveSuit(factory,110,82,170,40);
       factory=new ShanghaiClothesFactory(); 
       shop.giveSuit(factory,120,88,180,37);
factory=new ShenyangClothesFacyory(); 
       shop.giveSuit(factory,100,89,169,39);

    }
}

 (注意:代码中有文字)

4.生成器模式

生成器模式  _ 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

4.1四种角色 4.1.1产品(Product)

具体生成器要构造的复杂对象。

4.1.2抽象生成器(Builder)

抽象生成器是一个接口,该接口除了为创建一个Product对象的各个组件定义了若干个方法外,还要定义返回Product对象的方法

4.1.3具体生成器(ConcreteBuilder)

实现Builder接口的类,具体生成器将实现Builder接口所定义的方法。

4.1.4指挥者(Director)

指挥者是一个类,该类需含有Builder接口声明的变量。指挥者的职责是负责向用户提供具体生成器,即指挥者将请求具体生成器来构造用户所需要的Product对象,如果所请求的具体生成器成功地构造出Product对象,指挥者就可以让该具体生成器返回所构造的Product对象。

4.2UML类图

4.3案例 4.3.1基本要求

    创建含有按钮、标签和文本框组件的容器。不同用户对容器有不同的要求,比如某些用户希望容器中只含有按钮和标签,某些用户希望容器只含有按钮和文本框等。另外用户对组件在容器中的顺序位置也有不同的要求,比如某些用户要求组件在容器中从左至右的排列顺序是按钮、标签、文本框,而某些用户要求从左至右的排序是标签、文本框、按钮。

4.3.2代码
1.应用 Application.java
import javax.swing.*;
public class Application{
    public static void main(String args[]){
        Builder builder=new ConcreteBuilderOne(); //接口回调
        Director director=new Director(builder); //创建指挥者对象
        JPanel panel=director.constructProduct(); //创建产品
        Jframe frameOne=new Jframe();
        frameOne.add(panel);
        frameOne.setBounds(12,12,200,200);
        frameOne.setDefaultCloseOperation(Jframe.DISPOSE_ON_CLOSE);
        frameOne.setVisible(true);
        builder=new ConcreteBuilderTwo();//接口回调
        director=new Director(builder); //创建指挥者对象
        panel=director.constructProduct();//创建产品
        Jframe frameTwo=new Jframe();
        frameTwo.add(panel);
        frameTwo.setBounds(212,12,250,200);
        frameTwo.setDefaultCloseOperation(Jframe.DISPOSE_ON_CLOSE);
        frameTwo.setVisible(true);
    }
}
2.产品(Product): PanelProduct.java 
import javax.swing.*;
public class PanelProduct extends JPanel{
    JButton button;
    JLabel label;
    JTextField textField;
    JCheckBox checkBox;
    JRadioButton radioButton;
    JPasswordField passwordField;
}
3.抽象生成器(Builder): Builder.java 
import  javax.swing.*;
public interface Builder{
    public abstract void buildButton();
    public abstract void buildLabel();
    public abstract void buildTextField();
    public abstract void buildCheckBox();
    public abstract void buildRadioButton();
    public abstract void buildPasswordField();
    public abstract JPanel getPanel();
}
4.具体生成器(ConcreteBuilder)_1:ConcreteBuilderOne.java
import javax.swing.*;
public  class ConcreteBuilderOne implements Builder{
    private PanelProduct panel;      //声明产品对象
    ConcreteBuilderOne(){
        panel=new PanelProduct();
    }
    public void buildButton(){
        panel.button=new JButton("基本信息表(请如实填写)");
    }
    public void buildLabel(){
        panel.label=new JLabel("姓名:");
    }
    public void buildTextField(){
        panel.textField=new JTextField("           steven                  ");
    }
    public void buildCheckBox(){
        panel.checkBox=new JCheckBox("篮球");
    }
    public  void buildRadioButton(){
        panel.radioButton=new JRadioButton("男");
    }
    public void buildPasswordField(){
        panel.passwordField=new JPasswordField("     学号:2020041366       ");
    }
    public JPanel  getPanel(){
        panel.add(panel.button);
        panel.add(panel.label);
        panel.add(panel.textField);
        panel.add(panel.checkBox);
        panel.add(panel.radioButton);
        panel.add(panel.passwordField);
        return panel;
    }
}
4.具体生成器(ConcreteBuilder)_2:ConcreteBuilderTwo.java
import javax.swing.*;
public  class ConcreteBuilderTwo implements Builder{
    private PanelProduct panel;   //声明产品对象
    ConcreteBuilderTwo(){
        panel=new PanelProduct();
    }
    public void buildTextField(){
        panel.textField=new JTextField("        Eumice              ");
    }
    public void buildButton(){
        panel.button=new JButton("Basic information table(Truthfully fill in)");
    }
    public void buildLabel(){
        panel.label=new JLabel (" The name");
    }

    public void buildCheckBox(){
        panel.checkBox=new JCheckBox("badminton");
    }
    public  void buildRadioButton(){
        panel.radioButton=new JRadioButton("female");
    }
    public void buildPasswordField(){
        panel.passwordField=new JPasswordField("number:2020041366");
    }
    public JPanel  getPanel(){
        panel.add(panel.button);
        panel.add(panel.label);
        panel.add(panel.textField);
        panel.add(panel.checkBox);
        panel.add(panel.radioButton);
        panel.add(panel.passwordField);
        return panel;
    }
}
5.指挥者(Director):Director.java 
import javax.swing.*;
public class Director{
    private Builder builder; //声明接口变量
    Director(Builder builder){
        this.builder=builder;
    }
    public JPanel constructProduct(){
        builder.buildButton();
        builder.buildLabel();
        builder.buildTextField();
        builder.buildCheckBox();
        builder.buildRadioButton();
        builder.buildPasswordField();
        JPanel product=builder.getPanel();
        return product;  //返回产品
    }
4.3.3运行截图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301677.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号