Adapter, combined mode

Purpose and task

Objective: familiar with the UML, familiar with the adapter, combined mode.

Task: following the experimental content requires the complete adapter, the combination function mode implementation of experiment.

Content preview

UML review the contents of the course, familiar with the use of an adapter, combination mode.

Experiment contents and requirements

  • Adapter mode:

1, the conventional method of a class Square SQU (a float  L) for calculating the area of a square, are now required for calculating the Circle class inscribed circle of a square area, implement the output area of the adapter mode, the multiplexing claim Square , draw the class diagram and coding.

  • Combined mode:

There are the following scene: structure of the university is: University - College - Department, please according to our school instantiate the structure and requirements of college can send a message to the specified line (for example: School of Computer Science and Engineering can send a message to software engineering based), please draw mode with a combination of class and coding FIG.

The results (available Continued)

A: Adapter mode :

 

Target (target abstract class):

Circle.java:

package target;

 

public interface Circle {

    public float inter_squ(float l);

    public float squ(float l);

}

 

Adapter (adapter class):

Adapter.java:

 

The Adaptee (adaptation by category):

Square.java:

 

Main:

Main.java:

II: combined mode :

Component(抽象构件):

univComponent.java:

 

Leaf(叶子构件):

Department.java:

 

Composite(容器构件):

College,java:

 

Univ.java:

package Composite;

import java.util.ArrayList;

import Component.univComponent;

import Composite.College;

public class Univ extends univComponent{

    private String name;

    private java.util.List<College> collegeList=new ArrayList<College>();

    public Univ(String name){

        this.name=name;

    }

    public void add(univComponent univComponent) {

        // TODO Auto-generated method stub

        collegeList.add((College)univComponent);

       

    }

    public java.util.List<College> getCollegeList(){

        return collegeList;

    }

    public void setCollegeList(java.util.List<College> collegeList){

        this.collegeList=collegeList;

    }

    @Override

    public void sendMessage(String msg,univComponent univComponent) {

        College college=(College)univComponent;

        if(collegeList.contains(univComponent)){

           System.out.println(this.name+"发送信息"+college.getName()+":"+msg+"");

 

        }

    }

    public void remove(univComponent univComponent){

        collegeList.remove(univComponent);

    }

    @Override

    public String getName() {

        // TODO Auto-generated method stub

        return name;

    }

    public void setName(String name){

        this.name=name;

    }

 

}

 

Main:

Main.java:

 

 

 

思考题:

  1. 如何区别类适配器和对象适配器?

类适配器需要通过创建自身创建出一个新的Adapter,对象适配器可以通过已有的Adapter对象来装换接口类适配器通过覆写来扩展specificRequest(),对象适配器是包含关系不能扩展类适配器因为是继承所以相对静态,对象适配器是包含,所以相对灵活

2、算术运算:1+2中1、2是运算量,+是运算符,另外1+(2*3)也是算术运算,但是包含了(2*3),本身也是算术运算,请结合组合模式给出该算术运算的类图。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/yszbrzdd/article/details/93521322