The document discusses file input/output (I/O) operations in Java. It covers listing files in a directory, checking the number of files and folders, creating/deleting/renaming files, copying files, reading text files, and using interfaces and polymorphism for calculating total volume and weight from a data file. The key classes covered are File, FileReader, BufferedReader, and examples of implementing interfaces Volume and Weight for different item types.
The document discusses character stream classes in Java. Character streams handle data in a human-readable character form, as opposed to byte streams which handle binary data. The main character stream classes are Reader and Writer, which are abstract classes. FileReader and FileWriter are concrete subclasses that allow reading from and writing to files.
This document discusses various iteration techniques in Java including for loops, iterators, and enhanced for loops. It provides examples of iterating over lists, sets, maps, and arrays. It also summarizes common object methods like toString(), equals(), hashCode(), and finalize(). The finalize() method is called by the garbage collector before an object is destroyed to allow for cleanup.
The document discusses Java serialization and exceptions. It provides examples of implementing serialization by having a class implement the Serializable interface, which allows object instances to be written to and read from files. It also discusses exception handling using try-catch blocks and the finally clause, and how exceptions can be declared to be thrown from a method using the throws clause.
The document discusses Java serialization and the Externalizable interface. It provides examples of how to serialize and deserialize objects to allow their state to be written to a stream and restored later. The key points covered include:
- Using the Serializable interface to allow object serialization
- Implementing Externalizable for more control and better performance than Serializable
- Techniques for serializing singletons, enums, and collections for Externalizable
- Performance comparisons showing Externalizable can provide significant speed improvements over Serializable
Công ty thức ăn chăn nuôi ANOVAFEED thành lập năm 2010. Nhà máy với quy mô 5.5 ha, tọa lạc tại khu công nghiệp Long Định – Long Cang, huyện Cần Đước, tỉnh Long An, với công suất thiết kế 200.000 tấn/ năm.
Kế thừa các thành tựu khoa học trong lĩnh vực thức ăn gia súc, ANOVAFEED đã đầu từ công nghệ sản xuất thức ăn gia súc hiện đại và tiên tiến với hai dây chuyền sản xuất hoàn toàn tự động đảm bảo chất lượng thức ăn sản xuất và có chất lượng tối ưu, đáp ứng đầy đủ nhu cầu dinh dưỡng cho heo ở mọi giai đoạn phát triển. Hệ thống quản lý chất lượng QAQC được đầu tư hoàn chỉnh giúp kiểm soát chất lượng nguyên liệu đầu vào và chất lượng thành phẩm luôn luôn ổn định và hoàn toàn có thể truy xuất được nguồn gốc của từng lô thành phẩm, đảm bảo các tiêu chuẩn về an toàn vệ sinh thực phẩm
Với sự hợp tác chặt chẽ giữa ANOVAFEED và các công ty dinh dưỡng hàng đầu trên thế giới, ANOVAFEED cam kết cung ứng cho bà con chăn nuôi các sản phẩm thức ăn gia súc có chất lượng tốt nhất, và luôn ổn định nhằm mục tiêu tối ưu hóa chi phí thức ăn trong chăn nuôi, xứng đánh với thương hiệu "ANOVAFEED Hiệu quả số 1"
This document provides an overview of the MXpressions Common Core math curriculum for 4th and 5th grade teachers in California. It outlines the key elements that align it to the Common Core State Standards, including the mathematical content, practices, learning progressions, and problem types. It also includes sections on pacing guides, unit learning progressions, challenge centers, literature connections, lessons, assessments, math talks, technology resources, and communicating with parents.
This document outlines various areas to consider for a business plan including target markets such as educational sectors, young and elderly customers, and PC repairs. It also discusses unique selling points, promotions, pricing, sales channels, customer service, product design, purchasing strategies, production, delivery, quality control, financing, staffing, training and management.
O documento discute entrada e saída de dados em Java utilizando streams. Ele descreve os tipos de streams, como ler e gravar dados, e classes específicas para caracteres, bytes e serialização de objetos. Ele também fornece exemplos de código para ler do teclado, arquivos e exercícios extras.
1) Sister Crawford faced challenges to her faith through persecution from those who opposed her message, but she stood firm in her beliefs and experiences of salvation, sanctification, and baptism in the Holy Spirit.
2) Her example of standing strong in the face of opposition inspires Christians today to also stand firm in carrying the gospel message forward.
3) The article discusses the need for Christians to run the spiritual race with discipline, starting from a definite experience of salvation, and training effectively through moderation and sacrifice to gain the eternal reward.
The document discusses various input and output stream classes in Java. It defines streams as an abstraction that produces or consumes information linked to an I/O device. There are two types of streams - byte streams that handle binary data using classes like InputStream and OutputStream, and character streams that handle text using classes like Reader and Writer. The Java I/O package defines hierarchies for these stream classes, with subclasses that handle specific I/O devices or add functionality like buffering or filtering.
Serialization allows Java objects to be converted to a byte stream that can be reconstructed later, enabling persistence and network transmission of objects. The document discusses how to make a simple Stack class serializable by implementing the Serializable interface, and how serialization has evolved in Java, such as through the addition of generics and versioning with serialVersionUID.
This chapter discusses various methods of file input and output in Java, including low-level file I/O using FileInputStream and FileOutputStream, high-level file I/O using DataInputStream and DataOutputStream to read and write primitive data types, reading and writing text files using PrintWriter, BufferedReader, and Scanner, and object file I/O using ObjectInputStream and ObjectOutputStream to save and load objects. It also covers using JFileChooser to allow users to select files and applying file filters.
The document discusses file input/output (I/O) operations in Java. It covers listing files in a directory, checking the number of files and folders, creating/deleting/renaming files, copying files, reading text files, and using interfaces and polymorphism for calculating total volume and weight from a data file. The key classes covered are File, FileReader, BufferedReader, and examples of implementing interfaces Volume and Weight for different item types.
The document discusses character stream classes in Java. Character streams handle data in a human-readable character form, as opposed to byte streams which handle binary data. The main character stream classes are Reader and Writer, which are abstract classes. FileReader and FileWriter are concrete subclasses that allow reading from and writing to files.
This document discusses various iteration techniques in Java including for loops, iterators, and enhanced for loops. It provides examples of iterating over lists, sets, maps, and arrays. It also summarizes common object methods like toString(), equals(), hashCode(), and finalize(). The finalize() method is called by the garbage collector before an object is destroyed to allow for cleanup.
The document discusses Java serialization and exceptions. It provides examples of implementing serialization by having a class implement the Serializable interface, which allows object instances to be written to and read from files. It also discusses exception handling using try-catch blocks and the finally clause, and how exceptions can be declared to be thrown from a method using the throws clause.
The document discusses Java serialization and the Externalizable interface. It provides examples of how to serialize and deserialize objects to allow their state to be written to a stream and restored later. The key points covered include:
- Using the Serializable interface to allow object serialization
- Implementing Externalizable for more control and better performance than Serializable
- Techniques for serializing singletons, enums, and collections for Externalizable
- Performance comparisons showing Externalizable can provide significant speed improvements over Serializable
Công ty thức ăn chăn nuôi ANOVAFEED thành lập năm 2010. Nhà máy với quy mô 5.5 ha, tọa lạc tại khu công nghiệp Long Định – Long Cang, huyện Cần Đước, tỉnh Long An, với công suất thiết kế 200.000 tấn/ năm.
Kế thừa các thành tựu khoa học trong lĩnh vực thức ăn gia súc, ANOVAFEED đã đầu từ công nghệ sản xuất thức ăn gia súc hiện đại và tiên tiến với hai dây chuyền sản xuất hoàn toàn tự động đảm bảo chất lượng thức ăn sản xuất và có chất lượng tối ưu, đáp ứng đầy đủ nhu cầu dinh dưỡng cho heo ở mọi giai đoạn phát triển. Hệ thống quản lý chất lượng QAQC được đầu tư hoàn chỉnh giúp kiểm soát chất lượng nguyên liệu đầu vào và chất lượng thành phẩm luôn luôn ổn định và hoàn toàn có thể truy xuất được nguồn gốc của từng lô thành phẩm, đảm bảo các tiêu chuẩn về an toàn vệ sinh thực phẩm
Với sự hợp tác chặt chẽ giữa ANOVAFEED và các công ty dinh dưỡng hàng đầu trên thế giới, ANOVAFEED cam kết cung ứng cho bà con chăn nuôi các sản phẩm thức ăn gia súc có chất lượng tốt nhất, và luôn ổn định nhằm mục tiêu tối ưu hóa chi phí thức ăn trong chăn nuôi, xứng đánh với thương hiệu "ANOVAFEED Hiệu quả số 1"
This document provides an overview of the MXpressions Common Core math curriculum for 4th and 5th grade teachers in California. It outlines the key elements that align it to the Common Core State Standards, including the mathematical content, practices, learning progressions, and problem types. It also includes sections on pacing guides, unit learning progressions, challenge centers, literature connections, lessons, assessments, math talks, technology resources, and communicating with parents.
This document outlines various areas to consider for a business plan including target markets such as educational sectors, young and elderly customers, and PC repairs. It also discusses unique selling points, promotions, pricing, sales channels, customer service, product design, purchasing strategies, production, delivery, quality control, financing, staffing, training and management.
O documento discute entrada e saída de dados em Java utilizando streams. Ele descreve os tipos de streams, como ler e gravar dados, e classes específicas para caracteres, bytes e serialização de objetos. Ele também fornece exemplos de código para ler do teclado, arquivos e exercícios extras.
1) Sister Crawford faced challenges to her faith through persecution from those who opposed her message, but she stood firm in her beliefs and experiences of salvation, sanctification, and baptism in the Holy Spirit.
2) Her example of standing strong in the face of opposition inspires Christians today to also stand firm in carrying the gospel message forward.
3) The article discusses the need for Christians to run the spiritual race with discipline, starting from a definite experience of salvation, and training effectively through moderation and sacrifice to gain the eternal reward.
The document discusses various input and output stream classes in Java. It defines streams as an abstraction that produces or consumes information linked to an I/O device. There are two types of streams - byte streams that handle binary data using classes like InputStream and OutputStream, and character streams that handle text using classes like Reader and Writer. The Java I/O package defines hierarchies for these stream classes, with subclasses that handle specific I/O devices or add functionality like buffering or filtering.
Serialization allows Java objects to be converted to a byte stream that can be reconstructed later, enabling persistence and network transmission of objects. The document discusses how to make a simple Stack class serializable by implementing the Serializable interface, and how serialization has evolved in Java, such as through the addition of generics and versioning with serialVersionUID.
This chapter discusses various methods of file input and output in Java, including low-level file I/O using FileInputStream and FileOutputStream, high-level file I/O using DataInputStream and DataOutputStream to read and write primitive data types, reading and writing text files using PrintWriter, BufferedReader, and Scanner, and object file I/O using ObjectInputStream and ObjectOutputStream to save and load objects. It also covers using JFileChooser to allow users to select files and applying file filters.
6. Java 设计模式(疯狂 Java 联盟版)
4.ConcreteCreator
重定义工厂方法以返回一个 ConcreteProduct 实例。
类图
例子
*roduct
public interface Work {
void doWork();
}
ConcreteProduct
public class StudentWork implements Work {
public void doWork() {
System.out.println("学生*作业!");
}
}
public class TeacherWork implements Work {
public void doWork() {
System.out.println("老师审批作业!");
}
}
Creator
public interface IWorkFactory {
Work get*ork();
}
Concre*eCreator
4
7. Java 设计模式(疯狂 Java 联盟版)
pu*lic class StudentWorkFactory implements IWorkFactory {
public Work getWork() {
*eturn new StudentWork();
}
}
public class TeacherWorkFactory implements IWorkFactory {
public Work getWork() {
return new TeacherWork();
}
}
Test
public class Test {
public static void m*in(Strin*[] args) {
IWorkFactory studentWorkFactory = new StudentWorkFactory();
studentWorkFactory.getWork().d*Work();
IWorkFactory teacherWorkFactory * new TeacherWorkFactory();
teacherWorkFactory.g*tWork().*oWork();
}
}
result
学生做作业!
老师审批作业!
1.1.2 抽象工厂
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
适用性
1.一个系统要独立于它的*品的创建、组合和表示时。
5
9. Java 设计模式(疯狂 Java 联盟版)
ICat createCat();
IDog cre*teDog();
}
ConcreteFactory
p*blic class BlackAnimalFactory implem*nts IAnimalFactory {
public ICat createCat() {
retur* new BlackCat();
}
public IDog createDog() {
return new BlackDog();
}
}
public class WhiteAnimalFac*ory imp*ements IAnimalFactory {
public ICat createCat() {
return new WhiteCat();
}
public IDog cre*teDog() {
return new WhiteDog();
}
}
Abstrac*Product
public interface ICat {
void eat();
}
public interface IDog {
void eat();
}
Concrete*roduct
public class Black*at implements ICat {
7
10. Java 设计模式(疯狂 Java 联盟版)
public void eat() {
System.out.println("The bl*ck cat is eating!");
}
}
public class WhiteCat implements *Cat {
public void eat() {
Sy*tem.out.prin*ln("The w*ite cat is eating!*);
}
}
public class BlackDog implements IDog {
public void eat() {
System.out.println("The black dog is eating");
}
}
public class WhiteDog implements IDog {
public void eat() {
System.out.println("The white dog is eat*ng!");
}
}
Client
public static void main(String[] args) {
IAnimalFactory blackAnimalFa*tory = new BlackAnimalFactory();
ICat blackCat = blackAnimalFactory.createCat();
blackCat.eat();
IDog blackD*g = blackAnimalFactory.createDog();
blackDog.eat();
IAnimalFactory whiteAnimalF*ctory = new WhiteAnimalFactory();
ICat whiteCat = whiteAnimalFactory.createCat();
whiteCat.eat();
IDog *hiteDog = whiteAnimalFactory.createDog();
whiteDog.eat();
}
8
11. Java 设计模式(疯狂 Java 联盟版)
res*lt
The bla*k cat is eating!
Th* black dog is eatin*!
The white cat is eating!
The white dog is *ating!
1.1.3 建造者模式
将一个复杂对象的构*与它的表示分离,使*同样的构建过程可以创建不同的
表示。
适用性
1.当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式
时。
*.当构造过程必须允*被构造的对象有不同*表示时。
参与者
1.Builder
为创建一个 Product 对象的各个部件指定抽象接口。
2.ConcreteBuilder
实现 Buil*er 的接口以构造和装配该产品的各个部件。
定义并明确它所创建的表示*
提供一个检索产品的接口。
3.Director
构造一个使用 Builder 接口的对象。
4.Product
表示被构造的复杂对象。ConcreteBuilder 创建该产品的内部表示并定义它
的装配过程。
包含定义组成部件的类,包括将这些部件装配成最终产品的接口。
类图
9
12. Java 设计模式(疯狂 Java 联盟版)
例子
Buil*er
public interface PersonBuilder {
void buildHead();
v*id buildBody();
void buildFoot()*
Person buildPerson();
}
ConcreteBuilder
public class ManBuilder implements PersonB*ilder {
Person person;
public ManBuilder() {
person = ne* Man();
}
publ*c void build*ody() {
perso*.setBody("建造男人的身体");
10
13. Java 设计模式(疯狂 Java 联盟版)
}
public void buildFoot() {
person.setFo*t("建造男人的脚");
}
public void buildHead() {
pers*n.setHead("建造*人的头");
}
*ublic Person buildPerson() {
retur* person;
}
}
Dir*ctor
public class PersonDirec*or {
public Person constructPerson(PersonBuilder pb) {
pb.buildHead();
pb.buildBody();
pb.buildFoot();
return pb.buildPerson();
}
}
Product
public class Person {
private String head;
private String body;
private String foot;
public String getH*ad() {
return head;
}
public void setHead(String hea*) {
this.head = head;
}
public String getBody() {
11
14. Java 设计模式(疯狂 Java 联盟版)
return body;
}
public void setBody(String body) {
this.b*dy = body;
}
public String getFoot() {
return foot;
}
public void setFoot(String foot) {
t*is.foot = foot;
}
}
public class Man extends Person {
}
Test
publ*c class Test{
public static void main(String[] ar*s) {
PersonDirector pd = new PersonDirector();
Person person = pd.constructPerson(new ManBuilder());
System*out.println(person.getBody());
System.out.println(person.getFoot());
System.out.println(person.getHead());
}
}
result
建造男人*身体
建造男*的脚
建造男人的头
1.1.4 单态模式
保证一个类仅有一个实例,*提供一个访问它的全局访*点。
适用性
12
15. Java 设计模式(疯狂 Java 联盟版)
1.当类只能有一个*例而且客户可以从一个众所周知的访问点访问它时。
2.当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码
就能使用一个扩展的实例时。
参与者
Singleton
定义一个 Instance 操作,允许客户访问它的唯一实例。Instance 是一个类
操作。
可能负*创建它自己的唯一实例。
类图
例子
Singleton
public class Singleton {
private static Singleton sing;
private Singleton() {
}
public st*tic Singleton get*nstance() {
if (sing == null) {
sing = new Singleto*();
}
return sing;
}
}
Test
public class Test {
public static void *ain(*tring[] args) {
Singleton sing = Singleton.getInstance();
Singleton si*g2 = Singleton.getI*stance();
System.out.println(sing);
System.out.pr*ntln(sing2);
}
}
13
22. Java 设计模式(疯狂 Java 联盟版)
例子
Abstr*ction
public abstract class Person {
private Clothing clothing;
pr*vate String type;
public Clothing getClothing() {
return clothing;
}
publi* void setClothing() {
this.clothing = *lothingFactory.getClothing();
}
public void setType(String type) {
t*is.type = type;
}
public String getType() {
return this.ty*e;
}
public abstract void dress();
}
RefinedAbstraction
public class Man extends *erson {
20
23. Java 设计模式(疯狂 Java 联盟版)
public Man() {
setType("男人");
}
public void dress() {
Clothing clothing = get*lothing();
clothing.personDressCloth(this);
}
}
public class Lady extends Person {
public Lady() {
setTyp*("女人");
}
public void dress() {
Cloth*ng clothing = getClothing();
c*othing.personDressCloth(this);
}
}
Implemento*
public abstract class Clothing {
public abstract void personDressC*oth(*erson person);
}
ConcreteImplemento*
public class *ack*t extends Clothing {
public void personDressCloth(Person person) {
System.out.println(person.getType() + "穿马甲");
}
}
public cl*ss Trouser extends Clothing {
public void personDressCloth(Person person) {
System.ou*.println(*erson.getType() + "穿裤子");
}
}
Test
21
24. Java 设计模式(疯狂 Java 联盟版)
public class Te*t {
public s*atic void main(String[] args) {
Person man = new Man();
Person lady = new Lady();
Clothing jacket = new Ja*ket();
Clot*ing trouser = new Trouser();
jacket.personDressCloth(man);
trouser.personDressCloth(man);
j*cket.personDressCloth(lady);
trouser.personDressCloth(lady);
}
}
result
男人穿马甲
男人穿裤子
女人穿马甲
女人穿裤子
1.2.3 组合模式
将对象组合成树形结构以表示"部分-整体"的层次结构。"Composite 使得用户
对单个对象和组合对*的使用具有一致性。"
适用性
1.你想表示对象的部分-整*层次结构。
2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构
中的所有对象。
参与者
1.Component
为组合中的对象声明接口。
22
26. Java 设计模式(疯狂 Java 联盟版)
public abstract void delete(Employer employer);
public List employers;
public void printInfo*) {
System.out.println(name);
}
*ublic List getE*ployers() {
return this.employers;
}
}
Leaf
public class Programmer extends Employer {
public Programmer(String name) {
setNam*(name);
employers = null;//程序员, 表示没有下属了
}
public v*id add(Employer employer) {
}
public void delete(Employer employer) {
}
}
public class Pro*ectAssistant extends Employer {
public ProjectAss*stant(String name) {
setName(name);
employers = *ull;//项目助理, 表示没有下属了
}
public void add(Employer employer) {
}
public void delet*(Employer employer) {
24
27. Java 设计模式(疯狂 Java 联盟版)
}
}
Composite
public class Project*anager extends E*ployer {
public ProjectManager(String name) {
setName(name);
employers = new A*rayList();
}
public void add(Employer employer) {
employers.add(employer);
}
public void delete(Emplo*er employer) {
employers.remove(employer);
}
}
Clie*t
publ*c class Test {
public st*tic void main(String[] args) {
Employer pm = new ProjectManager("项目经理");
Emplo*er pa = new ProjectAssistant("项目助理");
Employer progra*mer1 = new Programmer("程序员一");
Employer programmer2 = new Programmer("程序员二");
pm.add(pa);//为项目经理添加项目助理
pm.add(programmer2);//*项目经理*加程序员
List ems = pm.getEm*loyers();
for (Employer em : ems) {
System.out.println(em.getNam*());
}
*
}
result
项目助理
程序员二
25
32. Java 设计模式(疯狂 Java 联盟版)
ServiceC sc;
public Facade() {
sa = new S*rviceAImpl();
sb = new *erviceBImpl();
sc = new ServiceCImpl();
}
public void methodA() {
sa.methodA();
sb.methodB();
}
publi* void methodB() {
s*.methodB();
sc.methodC();
}
public void methodC() {
sc.methodC();
sa.methodA();
}
}
Subsystemclasse*
public *lass ServiceAImpl implements ServiceA {
public void methodA() {
System.out.println("这是服务 A");
}
}
public class ServiceBImpl implements ServiceB {
public void methodB() {
System.out.println("这是服务 B");
*
}
public class ServiceCImpl implements ServiceC {
public void methodC() {
System.out.println("这是服*C");
30
33. Java 设计模式(疯狂 Java 联盟版)
}
}
Test
public class Test {
public static voi* main(String[] args) {
ServiceA sa = new ServiceAImpl();
Ser*iceB sb = new ServiceBImpl();
sa.metho*A();
sb.methodB();
System.out.println("========");
//facade
Facade facade = new Facade();
facade.methodA();
facade.methodB();
}
}
resu*t
这是服务 A
这是*务 B
========
这是服务 A
这是服务 B
这是服务 B
这是服务 C
1.2.6 享元模式
运用共享技术有效地支持大量细粒度的对象。
适用性
当都具备下列情况时,使用 Flyweight 模式:
1.一个应用程序使用了大量的*象。
2.完全由于使用大量的对象,造成很大的存储开销。
31
49. Java 设计模式(疯狂 Java 联盟版)
Object nex*();
void first();
voi* last();
boolean hasNext();
}
ConcreteIterator
public class IteratorImpl implements It*rator {
private List list;
private int index;
public Ite*atorImpl(List list* {
index = 0;
this.list = list;
}
public void first() {
index = 0;
}
publ*c void last() {
index = list.getSize();
}
public Object next() {
Object obj = list.get(index);
index++;
ret*rn obj;
}
public boolean hasNext() {
return index < list.getSize();
}
}
Aggregate
p*blic interface List {
47
50. Java 设计模式(疯狂 Java 联盟版)
Iterator iterator();
Object get(int index);
int *etSize();
void add(Object ob*);
}
ConcreteAggregate
public class ListImpl implements List {
private Object[] list;
private int index;
private int size;
public ListImpl() {
index = 0;
size = 0;
list = new Object[100];
}
public Iterator iterator() {
return new IteratorImpl(this);
}
public O*ject get(int index) {
return list[index];
}
public int getSize() {
return this.size;
}
public void add(Object obj) {
list[index++] = obj;
size++;
}
}
Test
public class Test {
48
51. Java 设计模式(疯狂 Java 联盟版)
public stati* void main(String[] arg*) {
List list = new ListImpl();
list.add("a");
list.add("b");
list.add("c");
//第一种迭代方式
Iterator it = list.iterator();
while (*t.ha*Next()) {
S*stem.out.println(it.next());
}
Syst*m.out.println("=====");
//第二种迭代方式
for (int i = 0; i < list.getSize(); i++) {
System.out.println(list.get(i));
}
}
}
result
a
b
c
=====
a
b
c
1.3.5 中介者模式
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相
互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
适用性
1.一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构
混乱且难以理解。
2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复*该对象。
3.想定制一个分布在多个类中的行为,*又不想生成太多的子类。
49
52. Java 设计模式(疯狂 Java 联盟版)
参与者
1.Mediator
中介者定义一个接口用于与各同事(Colleague)对象通信。
2.ConcreteMediator
具*中介者通过协调各同事对象实现协作行为*
了解并维护它的各个同事。
3.Colleagueclass
每一个同事类都知道它的中介者对象。
每一个同事对象在需与其他的同事通信的时候*与它的中介者通信
类图
例子
Mediator
public abstract class Mediator {
public abstract void notice(String content);
}
ConcreteMediator
public *lass ConcreteMediator e*tends Mediator {
private ColleagueA ca;
pri*ate ColleagueB cb;
public ConcreteMediator() {
ca = new ColleagueA();
cb = new Col*eagueB();
}
50
53. Java 设计模式(疯狂 Java 联盟版)
public void no*ice(String content) {
if (co*tent.equals("boss")) {
//老板来了, 通知员工 A
ca*action();
}
if (content.equals("client")) {
//客户来了, *知前台 B
cb.action();
}
}
}
Colleagueclass
public class ColleagueA extends *olleague {
public void action(* {
System.out.println("普通员工努力工作");
}
*
public class ColleagueB extends Colleague {
public void action() {
System.out.println("前台注意了!");
}
}
Test
public class Test {
public static void main(String[] args) {
Mediator med = new Concr*teMediator();
*/老板来了
med.notice("boss");
//客户来*
med.n*tice("client");
}
}
result
普通员工努力工作
51
67. Java 设计模式(疯狂 Java 联盟版)
例子
AbstractClass
public abstract class Template {
public abstract void print();
public void update() {
System.out.println("开始打印");
for (int i = 0; i < 10; i++) {
print();
}
}
}
ConcreteClass
public class TemplateConcrete extends Template {
@*verride
public void print() {
System.out.println("这是子类的实现");
}
}
Test
public class Test {
pu*lic static void main(String[] args) {
Te*plate temp = new TemplateConcrete();
temp.update();
}
}
result
开始打印
65