1. Trang chủ
  2. » Công Nghệ Thông Tin

[Code Pattern ôn thi]Kiến Trúc & Thiết Kế Phần Mềm PTIT

47 1,4K 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 47
Dung lượng 1,18 MB

Nội dung

1. Abstract Factory…………1 2. Builder…………………………5 3. Factory Method……………7 4. Prototype………………………9 5. Singleton………………………11 6. Adapter…………………………12 7. Bridge……………………………13 8. Composite……………………16 9. Decorator………………………18 10. Façade…………………………21 11. Flyweight………………………23 12. Proxy……………………………24 13. Chain of Responsibility…25 14. Command………………………26 15. Interperter………………………28 16. Iterator…………………………31 17. Mediator…………………………32 18. Memento………………………33 19. Observer…………………………35 20. State……………………………37 21. Strategy………………………39 22. Template Method………40 23. Visitor……………………………45 1. Abstract factory: 1 1 Address.java public abstract class Address { private String street; private String city; private String region; private String portalCode; public Address() { } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getRegion() { return region; } public void setRegion(String region) { this.region = region; } public String getPortalCode() { return portalCode; } public void setPortalCode(String portalCode) { this.portalCode = portalCode; } } AddressfFactory.java public interface AddressfFactory { public Address createAddress(); public PhoneNumber createPhoneNumber(); } 2 2 FrenchAddress.java public class FrenchAddress extends Address { //Lay dia chi FRENCH @Override public String getCity() { return "FRENCH"; } } FrenchAddressFactory.java public class FrenchAddressFactory implements AddressfFactory { @Override public Address createAddress() { return new FrenchAddress(); } @Override public PhoneNumber createPhoneNumber() { return new FrenchPhoneNumber(); } } FrenchPhoneNumber.java public class FrenchPhoneNumber extends PhoneNumber { @Override public int getNumber() { return 543210 + super.getNumber(); } } PhoneNumber.java public abstract class PhoneNumber { private String name; private int number; public PhoneNumber() { } public String getName() { return name; } public void setName(String name) { this.name = name; 3 3 } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } } Test.java public class Test { public static void main(String[] args) { AddressfFactory factory = new USAAddressfactory(); Address usaAddress = factory.createAddress(); System.out.println(usaAddress.getCity()); PhoneNumber usaPhoneNumber = factory.createPhoneNumber(); usaPhoneNumber.setNumber(6789); System.out.println(usaPhoneNumber.getNumber()); } } USAAddress.java public class USAAddress extends Address{ @Override public String getCity() { return "USA"; } } USAAddressfactory.java public class USAAddressfactory implements AddressfFactory{ @Override public Address createAddress() { return new USAAddress(); } @Override public PhoneNumber createPhoneNumber() { return new USAPhoneNumber(); } } USAPhoneNumber.java public class USAPhoneNumber extends PhoneNumber{ @Override 4 4 public int getNumber() { return 012345 + super.getNumber(); } } 2. Builder Address.java public class Address { private String street; private String city; private String region; public Address() { } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } 5 5 public void setCity(String city) { this.city = city; } public String getRegion() { return region; } public void setRegion(String region) { this.region = region; } } AddressDirector.java public class AddressDirector { public void Contruct(USAAddressBuilder builder, String street, String city, String region) { builder.builderCity(city); builder.builderRegion(region); builder.builderStreet(street); } } Builder.java public abstract class Builder { abstract public void builderStreet(String street); abstract public void builderCity(String city); abstract public void builderRegion(String region); } Test.java public class Test { public static void main(String[] args) { AddressDirector director = new AddressDirector(); USAAddressBuilder builder = new USAAddressBuilder(); director.Contruct(builder, "ABC", "CDE", "FGH"); Address address = builder.getAddress(); System.out.println(address.getStreet()); System.out.println(address.getCity()); System.out.println(address.getRegion()); } } 6 6 USAAddressBuilder.java public class USAAddressBuilder extends Builder { private Address add = new Address(); @Override public void builderCity(String city) { add.setCity(city); } @Override public void builderRegion(String region) { add.setRegion(region); } @Override public void builderStreet(String street) { add.setStreet(street); } public Address getAddress() { return add; } } 3. Factory Method Gioi.java public class Gioi e xt en ds XepLoai { public Gioi(String name) { super(name); } } Kha.java public class Kha exte nd s XepLoai { 7 7 public Kha(String name) { super(name); } } TB.java public class TB exten ds XepLoai { public TB(String name) { super(name); } } Yeu.java public class Yeu exte nd s XepLoai { public Yeu(String name) { super(name); } } Test.java public class Test { public static void main(String[] args) { float diem = 8.5f; XepLoai xepLoai = XepLoaiFactory.getInstance(diem); xepLoai.showMessenge(diem); } } XepLoai.java public class XepLoai { private String xepLoai; public XepLoai(String name) { this.xepLoai = name; } // Phương thức thông báo điểm TB va Xep Loai Hoc Sinh public void showMessenge(float diem) { System.out.println("Diem TB cu ban la : " + diem +" -> Xep loai Hoc Sinh :"+ xepLoai ); } } 8 8 XepLoaiFactory.java public class XepLoaiFactory { public static XepLoai getInstance(float diem) { if (diem < 5) { return new Yeu("Yeu"); } else if (diem >= 5 && diem < 6.5) { return new TB("TB"); } else if (diem >= 6.5 && diem < 8) { return new Kha("Kha"); } else return new Gioi("Gioi"); } } 4. Prototype GiaoVien.java public class GiaoVien extends NhanVien { public GiaoVien() { type = "GiaoVien"; } @Override public void info() { System.out.println("Thong tin cua Giao Vien :"); } } HocSinh.java public class HocSinh extends NhanVien { public HocSinh() { type = "HocSinh"; } @Override 9 9 public void info() { System.out.println("Thong tin cua Hoc Sinh :"); } } NhanVien.java public abstract class NhanVien implements Cloneable { private String id; protected String type; abstract void info(); public String getType() { return type; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Object clone() { Object clone = null ; try { clone = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } } PhanLoaiNhanVien.java public class PhanLoaiNhanVien { private static Hashtable<String, NhanVien> NhanVien = new Hashtable<String, NhanVien>(); public static NhanVien getNV(String nvId) { NhanVien phanLoaiNV = NhanVien.get(nvId); return (NhanVien) phanLoaiNV.clone(); } 10 1 0 [...]... public class Test { public static void main(String[] args){ String str; String select = "C"; ArrayList parse = new ArrayList(); Scanner scanIn=new Scanner(System.in); while (select != "K" && select != "k"){ System.out.println("Nhap vao so La Ma : "); str = scanIn.nextLine(); str = str.toUpperCase(); Context context = new Context(str); parse.add(new parse.add(new parse.add(new parse.add(new... 16.Iterator Container.java public interface Container { public Iterator getIterator(); } Iterator.java public interface Iterator { public boolean hasNext(); public Object next(); } IteratorPatternDemo.java public class IteratorPatternDemo { public static void main(String[] args) { NameRepository namesRepository = new NameRepository(); for(Iterator iter = namesRepository.getIterator(); iter.hasNext(); ){ String... public class ChatRoom { public static void showMessage(User user, String message){ System.out.println(new Date().toString() + " [" + user.getName() +"] : " + message); } } 32 3 2 MediatorPatternDemo.java public class MediatorPatternDemo { public static void main(String[] args) { User robert = new User("Robert"); User john = new User("John"); robert.sendMessage("Hi! John!"); john.sendMessage("Hello! Robert!");... "Target" ); Target target2 = Create( "Adapter" ); target1.operation(); target2.operation(); } } Target.java package Adapter; public class Target { public void operation() { System.out.println( "Hello Design Pattern" ); } } 7 Bridge 13 1 3 ConcreteImplementA.java public class ConcreteImplementA implements MyImplementation { @Override public void doMthod1() { System.out.println(" Mthod1 - A"); } @Override public... FileLogger(); } return logger; } } 6.Adapter Adaptee.java package Adapter; import javax.swing.*; public class Adaptee { public void specificRequest() { JOptionPane.showMessageDialog( null, "Hello Design Pattern" , "from Adaptee", JOptionPane.OK_OPTION ); } } Adapter.java package Adapter; public class Adapter extends Target { private Adaptee obj = new Adaptee(); public void operation() { request(); } private . { if (diem < 5) { return new Yeu("Yeu"); } else if (diem >= 5 && diem < 6.5) { return new TB("TB"); } else if (diem >= 6.5 && diem < 8) { return. PhanLoaiNhanVien.getNV("1"); System.out.println("Loai Nhan Vien : " + nv.getType()); NhanVien nv2 = (NhanVien) PhanLoaiNhanVien.getNV("2"); System.out.println("Loai Nhan Vien : ". AddressDirector(); USAAddressBuilder builder = new USAAddressBuilder(); director.Contruct(builder, "ABC", "CDE", "FGH"); Address address = builder.getAddress(); System.out.println(address.getStreet()); System.out.println(address.getCity()); System.out.println(address.getRegion()); } } 6 6 USAAddressBuilder.java public

Ngày đăng: 25/05/2015, 23:19

TỪ KHÓA LIÊN QUAN

w