Design mode finishing (1) Abstract factory

xiaoxiao2021-03-06  58

I. Structural Figure 2, applicability 1, one application is independent of one product creation, combination; 2, an application should be configured with a product in multiple products; 3, when you want to emphasize a series of related The design of the product object is designed to be combined; 4. When you provide a product class library, just want to display their interfaces instead of implementation. Third, the purpose provides an interface that creates a series of related or interdependent objects without specifying their specific classes. Fourth, personal feelings abstract factory model should be used in combination. When you build another object with different objects, you can make these all abstractions to facilitate more sample combinations; this guarantees the diversity of objects, the scalability of the program Enhanced; in addition, some methods can be encapsulated, forming an interface, and performs an implementation of an interface by configuring or otherwise. and many more. 5. Java implementation 1. Roommaker class public class room rarser {

Public Room Createroom (String RoomType) {if ("LivingRoom")) {Return New LivingRoom ();} else.equals ("Bedroom") {Return New Bedroom ();} NEW LIVINGROOM ();}}

Public static void main (string [] args) {roommaker mymaker = new roommaker (); // ----- create living room room mylivingroom = mymaker.createroom ("Livingroom"); // ----- Create a Door in Living Room Door LivingDoor = MyLivingRoom.makedoor (); System.out.println ("Living Room Door Name IS:" LivingDoor.GetName (); // ----- Create A Wall In Living Room Wall Livingwall = MYLIVINGROOM.MAKEWALL (); System.out.println ("Living Room Wall Name IS:" LivingWall.getName ());

// ----- create bed room room mybedroom = mymaker.createroom ("bedroom"); // ----- Create a door in bedroom.makedoor (); system.out.println (" Bed room door name is: " beddoor.getname ()); // ----- Create a wall in bedroom wall bedwall = mybedroom.makewall (); system.out.println (" Bed Room Wall Name IS: " BEDWALL.GETNAME ());

}} 2, Room Class public abstract class Room {public abstract Wall makeWall (); public abstract Door makeDoor ();} 3, Wall class public abstract class Wall {// private Wall wall; public abstract String getName ();} 4 , Door class public abstract class Door {// private Door door; public abstract String getName ();} 5, BedRoom class public class BedRoom extends Room {public BedRoom () {System.out.println ( "! Initiated bedroom a") ;} public Door makeDoor () {return new BedRoomDoor ();} public Wall makeWall () {return new BedRoomWall ();}} 6, LivingRoom class public class LivingRoom extends Room {public LivingRoom () {System.out.println ( "Initiated a living room!");} public Door makeDoor () {return new LivingRoomDoor ();} public Wall makeWall () {return new LivingRoomWall ();}} 7, BedRoomDoor class public class BedRoomDoor extends Door {private String doorName Public bedroomDoor () {doorname = "bedroomdoor";} PUB lic String getName () {return doorName;}} 8, BedRoomWall class public class BedRoomWall extends Wall {private String wallName; public BedRoomWall () {wallName = "BedRoomWall";} public String getName () {return wallName;}} 9, LivingRoomDoor class public class LivingRoomDoor extends Door {private String doorName; public LivingRoomDoor () {doorName = "LivingRoomDoor";} public String getName () {return doorName;}} 10, LivingRoomWall class public class LivingRoomWall extends Wall {private String wallName; public LivingRoomwall () {wallname = "LivingRoomwall";} public string getname () {Return WallName

转载请注明原文地址:https://www.9cbs.com/read-84078.html

New Post(0)